神经网络中,最基本的优化方式就是反向传播 本文介绍了Pytorch中一个简单的例子:
import torch
import torch.nn as nn
from torch.nn import MaxPool2d, ReLU, Sigmoid, Linear, Conv2d, Flatten, Sequential
import torchvision
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter as Sum
root = r'C:\Users\15715\Desktop\Programs\Python\深度学习\pythonProject\dataset_CIFAR10'
dataset = torchvision.datasets.CIFAR10(root=root, train=False, transform=torchvision.transforms.ToTensor(), download=True)
dataloader = DataLoader(dataset, batch_size=1)
class Standard(nn.Module):
def __init__(self):
super(Standard, self).__init__()
# self.conv1 = Conv2d(3, 32, 5, padding=2) # 第一个卷积层 in通道,out通道,kernel,padding stride(default=1)
# self.maxpool1 = MaxPool2d(2) # 第一个池化层 kernel
# self.conv2 = Conv2d(32, 32, 5, padding=2)
# self.maxpool2 = MaxPool2d(2)
# self.conv3 = Conv2d(32, 64, 5, padding=2)
# self.maxpool3 = MaxPool2d(2)
# self.flatten = Flatten() # 展平
# # 实质有2个线性层
# self.linear1 = Linear(1024, 64)
# self.linear2 = Linear(64, 10)
# 上述被替代为了下面的Sequential
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.conv1(x)
# x = self.maxpool1(x)
# x = self.conv2(x)
# x = self.maxpool2(x)
# x = self.conv3(x)
# x = self.maxpool3(x)
# x = self.flatten(x)
# x = self.linear1(x)
# x = self.linear2(x)
x = self.model1(x)
return x
mc = Standard()
# print(mc)
loss = nn.L1Loss()
optim = torch.optim.SGD(mc.parameters(), lr=0.01) # parameters 学习速率
epoch = 10
for x in range(epoch):
# 学习一轮
runloss = 0.0
for data in dataloader:
img, target = data # output是实际输出,target是理想输出,即真实标签值
output = mc(img)
# print(output)
# print(target)
res = loss(output,target)
# print(res)
# 反向传播
optim.zero_grad() # 将之前的梯度置为0,避免影响本次的优化
res.backward() # 获取梯度
optim.step() # 优化
# print(res)
runloss = runloss + res
print('1 epch done')
# 宏观上查看误差的减小
print(runloss)
print('')
运行结果为:
1 epch done
tensor(25982.2188, grad_fn=<AddBackward0>)
1 epch done
tensor(23803.2637, grad_fn=<AddBackward0>)
1 epch done
tensor(23197.8145, grad_fn=<AddBackward0>)
1 epch done
tensor(22575.0508, grad_fn=<AddBackward0>)
备注:数据集来源于官网(cifar10)
模型就是cifar10的模型