【PyTorch】Optim 优化器

文章目录

  • 五、Optim 优化器
    • 1、SGD

五、Optim 优化器

参考文档:https://pytorch.org/docs/stable/optim.html

【PyTorch】Optim 优化器_第1张图片

1、SGD

参考文档:https://pytorch.org/docs/stable/generated/torch.optim.SGD.html#torch.optim.SGD

【PyTorch】Optim 优化器_第2张图片

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

dataset = torchvision.datasets.CIFAR10("../data", train=False, transform=torchvision.transforms.ToTensor(),
                                       download=True)

dataloader = DataLoader(dataset, batch_size=1)


class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, 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 = CrossEntropyLoss()

tudui = Tudui()

optim = torch.optim.SGD(tudui.parameters(), lr=0.01)  # 优化器

For循环1:

for data in dataloader:
    imgs, targets = data
    output = tudui(imgs)
    result_loss = loss(output, targets)
    optim.zero_grad()  # 清零
    result_loss.backward()  # 反向传播
    optim.step()  # 调优
    print(result_loss)
Files already downloaded and verified
tensor(2.3287, grad_fn=<NllLossBackward0>)
tensor(2.3879, grad_fn=<NllLossBackward0>)
tensor(2.2987, grad_fn=<NllLossBackward0>)
...

For循环2:

for epoch in range(20):
    running_loss = 0.0
    for data in dataloader:
        imgs, targets = data
        output = tudui(imgs)
        result_loss = loss(output, targets)
        optim.zero_grad()
        result_loss.backward()
        optim.step()
        running_loss += result_loss
    print(running_loss)
Files already downloaded and verified
tensor(18592.4395, grad_fn=<AddBackward0>)
tensor(16118.4756, grad_fn=<AddBackward0>)
tensor(15450.5898, grad_fn=<AddBackward0>)
...

你可能感兴趣的:(pytorch,深度学习,人工智能,python)