PyTorch 基础(3) 神经网络

使用torch.nn包构建神经网络, nn.Module包括网络的层, 前向传播forward(input)返回output

PyTorch 基础(3) 神经网络_第1张图片
卷积网络

训练神经网络的流程有

  • 定义神经网络模型(有可学习的参数)
  • 输入训练数据集迭代
  • 将数据输入网络(前向传播)
  • 计算损失函数
  • 误差反向传播
  • 更新网络参数 weight = weight - learning_rate * gradient

定义网络

from __future__ import print_function 
import torch 
from torch.autograd import Variable
import torch.nn as nn

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 6, 5) # 1 input image channel, 6 output channels, 5x5 square convolution kernel
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16*5*5, 120) # an affine operation: y = Wx + b
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)
        self.pool = nn.MaxPool2d((2,2)) # Max pooling over a (2, 2) window
        self.relu = nn.ReLU() 

    def forward(self, x):
        x = self.pool(self.relu(self.conv1(x)))
        x = self.pool(self.relu(self.conv2(x)))
        x = x.view(-1, self.num_flat_features(x))
        x = self.relu(self.fc1(x))
        x = self.relu(self.fc2(x))
        x = self.fc3(x)
        return x

    def num_flat_features(self,x):
        size = x.size()[1:] # all dimensions except the batch dimension
        num_features = 1
        for s in size:
            num_features *= s
        return num_features

net = Net()
print(net)

Net (
(conv1): Conv2d(1, 6, kernel_size=(5, 5), stride=(1, 1))
(conv2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1))
(fc1): Linear (400 -> 120)
(fc2): Linear (120 -> 84)
(fc3): Linear (84 -> 10)
(pool): MaxPool2d (size=(2, 2), stride=(2, 2), dilation=(1, 1))
(relu): ReLU ()
)

查看网络参数

params = list(net.parameters())
print(len(params))
print(params[0].size()) # conv1's weight

10
(6L, 1L, 5L, 5L)

输入一个随机生成的数据

input = Variable(torch.randn(1, 1, 32, 32))
out = net(input)
print(out)

Variable containing:
-0.0514 -0.0893 0.0555 -0.0256 0.0736 -0.0938 0.1239 0.0804 0.0735 0.0040

反向传播

net.zero_grad()
out.backward(torch.randn(1, 10))

误差函数

output = net(input)
target = Variable(torch.arange(1, 11))
criterion = nn.MSELoss()
loss = criterion(output, target)
print(loss)

反向传播计算梯度

net.zero_grad()     # zeroes the gradient buffers of all parameters

print('conv1.bias.grad before backward')
print(net.conv1.bias.grad)

loss.backward()

print('conv1.bias.grad after backward')
print(net.conv1.bias.grad)

更新参数

learning_rate = 0.01
for f in net.parameters():
    f.data.sub_(f.grad.data * learning_rate)

使用torch.optim优化网络

import torch.optim as optim

# create your optimizer
optimizer = optim.SGD(net.parameters(), lr=0.01)

# in your training loop:
optimizer.zero_grad()   # zero the gradient buffers
output = net(input)
loss = criterion(output, target)
loss.backward()
optimizer.step()    # Does the update

你可能感兴趣的:(PyTorch 基础(3) 神经网络)