『PyTorch』第四弹_通过LeNet初识pytorch神经网络_下

『PyTorch』第四弹_通过LeNet初识pytorch神经网络_上

# Author : Hellcat
# Time   : 2018/2/11

import torch as t
import torch.nn as nn
import torch.nn.functional as F

class LeNet(nn.Module):
    def __init__(self):
        super(LeNet,self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.conv2 = nn.Conv2d(6,16,5)
        self.fc1 = nn.Linear(16*5*5,120)
        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))
        x = F.max_pool2d(F.relu(self.conv2(x)),2)
        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

if __name__ == "__main__":
    net = LeNet()

    # #########训练网络#########
    from torch import optim
    # 初始化Loss函数 & 优化器
    loss_fn = nn.CrossEntropyLoss()
    optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

    for epoch in range(2):
        running_loss = 0.0
        for step, data in enumerate(trainloader, 0):  # step为训练次数, trainloader包含batch的数据和标签
            inputs, labels = data
            inputs, labels = t.autograd.Variable(inputs), t.autograd.Variable(labels)

            # 梯度清零
            optimizer.zero_grad()

            # forward
            outputs = net(inputs)
            # backward
            loss = loss_fn(outputs, labels)
            loss.backward()
            # update
            optimizer.step()

            running_loss += loss.data[0]
            if step % 2000 == 1999:
                print("[{0:d}, {1:5d}] loss: {2:3f}".format(epoch+1, step+1, running_loss/2000))
                running_loss = 0.
    print("Finished Training")

 这是使用LeNet分类cifar_10的例子,数据处理部分由于不是重点,没有列上来,主要是对使用torch分类有一个直观理解,

初始化网络

初始化Loss函数 & 优化器

进入step循环:

  梯度清零

  向前传播

  计算本次Loss

  向后传播

  更新参数

由于pytorch的网络是class,所以在不考虑持久化的情况下,后续处理都不是太难,值得一提的是预测函数,我们直接net(Variable(test_data))即可,输出是概率分布的Variable,我们只要调用:

_, predict = t.max(test_out, 1)

即可,这是因为当指定了dim时,torch.max会融合max和argmax的功能,

>> a = torch.randn(4, 4)
>> a
    
0.0692  0.3142  1.2513 -0.5428
0.9288  0.8552 -0.2073  0.6409
1.0695 -0.0101 -2.4507 -1.2230
0.7426 -0.7666  0.4862 -0.6628
torch.FloatTensor of size 4×4]
    
>>> torch.max(a, 1)
(
1.2513
0.9288
1.0695
0.7426
[torch.FloatTensor of size 4]
,
2
0
0
0
[torch.LongTensor of size 4]
)

其他torch的高级功能没有使用到,本篇的目的是对于torch神经网络基本的使用有个理解。

 

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