本文是pytorch官方的一篇教程,加入其它学习过程中的一些东西,持续更新:https://pytorch.org/tutorials/beginner/blitz/neural_networks_tutorial.html
主要记录一些在Pytorch 中构建神经网络的必要知识点。
Contents [hide]
神经网络的典型训练过程如下:
- 定义一些可学习的参数(或权重)的神经网络
- 遍历输入数据集
- 通过网络处理输入
- 计算损失(输出与正确(ground truth, correct, target)的距离有多远)
- 将梯度传播回网络参数
- 通常使用简单的更新规则来更新网络的权重:
weights =weights - learning_rate * gradient
定义一些可学习的参数(或权重)的神经网络
Pytorch中的Parameter 和Variable之间的比较
定义网络
import torch
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
# 1 input image channel, 6 output channels, 3x3 square convolution
# kernel
self.conv1 = nn.Conv2d(1, 6, 3)
self.conv2 = nn.Conv2d(6, 16, 3)
# an affine operation: y = Wx + b
self.fc1 = nn.Linear(16 * 6 * 6, 120) # 6*6 from image dimension
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):