定义神经网络

from __future__ import print_function
import torch as t
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
class Net(nn.Module):
    def __init__(self):#构造函数
        #nn.Module子类的函数必须在构造函数中执行父类的构造函数
        #下式等价于nn.Module.__init__(self)
        super(Net,self).__init__()
        #卷积层'1'表示输入图片为单通道,6表示输出通道数
        #'5'表示卷积核为5*5
        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)
    #定义好forward函数,backward会被自动实现
    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)
        #reshape,'-1'表示自适应
        x=x.view(x.size()[0],-1)
        x=F.relu(self.fc1(x))
        x=F.relu(self.fc2(x))
        x=self.fc3(x)
        return x
net=Net()
print(net)
params=list(net.parameters())
print(len(params))
for name,parameters in net.named_parameters():
   print(name,':',parameters.size())
input=Variable(t.randn(1,1,32,32))
out=net(input)
print(out.size())
output=net(input)
target=Variable(t.arange(0,10))
criterion=nn.MSELoss()
loss=criterion(output,target.float())
print(loss)
net.zero_grad()  #把net中所有可学习参数的梯度清零
print('反向传播之前conv1.bias的梯度')
print(net.conv1.bias.grad)
loss.backward()
print('反向传播之后的梯度')
print(net.conv1.bias.grad)
#优化器
import torch.optim as optim
#建立一个优化器,指定要调整的参数和学习率
optimizer =optim.SGD(net.parameters(),lr=0.01)
#在训练过程中
#先梯度清零(与net.zero_grad()效果一样)
optimizer.zero_grad()
#计算损失
output=net(input)
loss=criterion(output,target)
#反向传播
loss.backward()
#更新参数
optimizer.step()

你可能感兴趣的:(python学习)