[pytorch] Pytorch入门

Pytorch入门

简单容易上手,感觉比keras好理解多了,和mxnet很像(似乎mxnet有点借鉴pytorch),记一记。

直接从例子开始学,基础知识咱已经看了很多论文了。。。

import torch
import torch.nn as nn
import torch.nn.functional as F
# Linear 层 就是全连接层
class Net(nn.Module): # 继承nn.Module,只用定义forward,反向传播会自动生成
    def __init__(self): # 初始化方法,这里的初始化是为了forward函数可以直接调过来
        super(Net,self).__init__() # 调用父类初始化方法
        # (input_channel,output_channel,kernel_size)
        self.conv1 = nn.Conv2d(1,6,5) # 第一层卷积
        self.conv2 = nn.Conv2d(6,16,5)# 第二层卷积
        self.fc1 = nn.Linear(16*5*5,120) # 这里16*5*5是前向算的
        self.fc2 = nn.Linear(120,84) # 第二层全连接
        self.fc3 = nn.Linear(84,10) # 第三层全连接->分类
    def forward(self,x):
        x = F.max_pool2d(F.relu(self.conv1(x)),(2,2)) # 卷积一次激活一次然后2*2池化一次
        x = F.max_pool2d(F.relu(self.conv2(x)),2) # (2,2)与直接写 2 等价
        x = x.view(-1,self.num_flatten_features(x)) # 将x展开成向量
        x = F.relu(self.fc1(x)) # 全连接 + 激活
        x = F.relu(self.fc2(x)) # 全连接+ 激活
        x = self.fc3(x) # 最后再全连接
        return x
    def num_flatten_features(self,x):
        size = x.size()[1:] # 除了batch_size以外的维度,(batch_size,channel,h,w)
        num_features = 1
        for s in size:
            num_features*=s
        return num_features
# ok,模型定义完毕。
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(in_features=400, out_features=120, bias=True)
  (fc2): Linear(in_features=120, out_features=84, bias=True)
  (fc3): Linear(in_features=84, out_features=10, bias=True)
)
'''
params = list(net.parameters())
print(len(params))
print(params[0].size())
'''
10
torch.Size([6, 1, 5, 5])
'''
inpt = torch.randn(1,1,32,32)
out = net(inpt)
print(out)
'''
tensor([[-0.0265, -0.1246, -0.0796,  0.1028, -0.0595,  0.0383,  0.0038, -0.0019,
          0.1181,  0.1373]], grad_fn=<AddmmBackward>)
'''
target = torch.randn(10)
criterion = nn.MSELoss()
loss = criterion(out,target)
print(loss)
'''
tensor(0.5742, grad_fn=<MseLossBackward>)
'''
net.zero_grad()# 梯度归零
print(net.conv1.bias.grad)
loss.backward()
print(net.conv1.bias.grad)
'''
None
tensor([-0.0039,  0.0052,  0.0034, -0.0002,  0.0018,  0.0096])
'''
import torch.optim as optim
optimizer = optim.SGD(net.parameters(),lr = 0.01)
optimizer.zero_grad()
output = net(inpt)
loss = criterion(output,target)
loss.backward()
optimizer.step()
# 一个step完成,多个step就写在循环里

pytorch简直太好理解了。。继续蓄力!!

    原文作者:pytorch
    原文地址: https://www.cnblogs.com/aoru45/p/10623046.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞