PyTorch深度学习——优化函数

直接参考官方文档就可以了:
Optim

import torch
import torchvision
from torch import nn
from torch.nn import Sequential, Conv2d, MaxPool2d, Flatten, Linear
from torch.utils.data import DataLoader

dataset = torchvision.datasets.CIFAR10(root="./dataset",train=False,transform=torchvision.transforms.ToTensor(),download=True)
dataLoader = DataLoader(dataset,batch_size=1)

class Siri(nn.Module):
    def __init__(self):
        super(Siri, self).__init__()
        self.model1 = Sequential(
            Conv2d(3, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32,64,5,padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024,64),
            Linear(64,10)
        )

    def forward(self,x):
        x = self.model1(x)
        return x

loss = nn.CrossEntropyLoss()
siri = Siri()
optim = torch.optim.SGD(siri.parameters(),lr=0.01)
for epoch in range(20):     #训练的次数
    running_loss = 0.0
    for data in dataLoader:
        imgs,targets = data
        outputs = siri(imgs)
        result_loss = loss(outputs,targets)
        optim.zero_grad()
        result_loss.backward()
        optim.step()
        running_loss += result_loss
    print(running_loss)



tensor(18657.1797, grad_fn=<AddBackward0>)
tensor(16082.5547, grad_fn=<AddBackward0>)
tensor(15319.9521, grad_fn=<AddBackward0>)
tensor(15886.4248, grad_fn=<AddBackward0>)
tensor(17661.3438, grad_fn=<AddBackward0>)
tensor(20235.9531, grad_fn=<AddBackward0>)

你可能感兴趣的:(PyTorch)