pytorch python学习(三)

简单实现lenet-5:

#coding=utf-8
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        # 建立了两个卷积层,第一层1 个通道输入, 6个输出通道, 5x5 卷积核
        self.conv1 = nn.Conv2d(1, 6, 5)
        self.conv2 = nn.Conv2d(6, 16, 5)
        #三个全连接层,y = Wx + b 这里没有做激活/非线性操作
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x): #2D卷积层的输入data维数是 batchsize*channel*height*width
        # 最大池化
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        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:]  # all dimensions except the batch dimension
        num_features = 1
        for s in size:
            num_features *= s
        return num_features

net =Net()
print net

#网络的可学习参数通过net.parameters()返回
params = list(net.parameters())
print len(params)
print params[0].size()

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

#将网络中所有的参数梯度清零
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 '---' * 10
print 'loss:',loss

#反向传播
net.zero_grad() #将之前的梯度清零
print 'net.conv1.bias.grad brfore backward:'
print net.conv1.bias.grad

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


import  torch.optim as optim

#新建一个优化器,指定要调整的参数和学习率
optimizer = optim.SGD(net.parameters(),lr = 0.01)
optimizer.zero_grad()
output = net(input)
loss = criterion(output,target)
loss.backward()
optimizer.step() #更新参数

结果:

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)
)
10
(6L, 1L, 5L, 5L)
Variable containing:
(0 ,0 ,.,.) = 
  0.5187 -0.7634 -0.7736  ...   0.7010  0.7504  0.4978
  1.0905  0.7255 -1.4743  ...   1.8054 -1.3483  0.5112
 -0.0973  0.3014  0.7019  ...   0.3816 -0.2391 -1.6550
           ...             ⋱             ...          
 -0.4778  0.3382 -0.0205  ...   0.1119 -0.1873  0.3986
  0.4438 -0.3040 -2.1828  ...  -0.4498 -0.5209 -1.2561
 -1.5232  1.4791  0.1453  ...   0.5937  0.1218 -0.1614
[torch.FloatTensor of size 1x1x32x32]

Variable containing:
-0.0099  0.0765 -0.1277  0.1134 -0.0274 -0.0051 -0.0470 -0.0740 -0.1531  0.1007
[torch.FloatTensor of size 1x10]

------------------------------
loss: Variable containing:
 38.7571
[torch.FloatTensor of size 1]

net.conv1.bias.grad brfore backward:
Variable containing:
 0
 0
 0
 0
 0
 0
[torch.FloatTensor of size 6]

net.conv1.bias.grad after backward:
Variable containing:
-0.0227
-0.0524
-0.0884
 0.0287
 0.1252
-0.1181
[torch.FloatTensor of size 6]

猜你喜欢

转载自blog.csdn.net/nanxiaoting/article/details/81056807
今日推荐