自动求导:
https://zhuanlan.zhihu.com/p/84812085
Pytorch入门教程:
https://github.com/fendouai/PyTorchDocs/blob/master/SecondSection/training_a_classifier.md
Pytorch中文手册:
https://ptorch.com/docs/1/optim
卷积神经网络模版
class Net(nn.Module): def __init__(self): super(Net, self).__init__() # 设置卷积层 self.conv1 = nn.Conv2d(3, 6, 3) self.conv2 = nn.Conv2d(6, 16, 5) # 设置全连接层 self.fc1 = nn.Linear(16 * 5 * 5, 120) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, 10) def forward(self, x): x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2)) x = F.max_pool2d(F.relu(self.conv2(x)), 2) # 将多行的Tensor拼接成一行 x = x.view(-1, self.num_flat_features(x)) x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = self.fc3(x) return x def num_flat_features(self, x): size = x.size()[1:] num_features = 1 for s in size: num_features *= s return num_features
训练的迭代中执行的代码:
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
for epoch in range(2): running_loss = 0.0 for i, data in enumerate(trainloader, 0): inputs, labels = data
# 初始化梯度 optimizer.zero_grad()
# 得到网络输出 outputs = net(inputs)
#计算损失函数 loss = criterion(outputs, labels)
#计算梯度(自动求导) loss.backward()
#反向传播 optimizer.step() running_loss += loss.item() if i % 2000 == 1999: print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 2000)) running_loss = 0.0