摘要
'''
Neural Networks
= = = = = = = = = = = = = = =
神经网络可以用'torch.nn'包构建。现在您已经大致了解了'autograd','nn'依赖于'autograd'来定义模型并区分它们。
一个"nn.Module"包含层,以及一个方法' ' forward(input) ' ' 它返回“输出”。
例如,看看这个分类数字图像的网络:
.. figure:: /_static/img/mnist.png
:alt: convnet
convnet(事先)
它是一个简单的前馈网络。它接受输入,一个接一个地通过几个隐藏层,最后给出输出。
神经网络的典型训练过程如下:
-定义具有一些可学习参数(或权重)的神经网络
-遍历输入数据集
-通过网络处理输入
-计算损失(输出距离正确有多远)
-传播梯度回网络的参数
-更新网络的权重,通常使用一个简单的更新规则:
' ' weight = weight - learning_rate * gradient ' '
定义网络
------------------
让我们来定义这个网络:
'''
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个输入图像通道,6个输出通道,3x3平方卷积核
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):
# Max pooling o