【Pytorch】入门 ①:60-minute blitz

2020年1月是那么的特别,新型冠状病毒的发生和影响出乎所有人的意料。在家度过25天后,来到单位还要隔离15天,正巧用这时间学习一下Pytorch。
       在Pytorchg官网有丰富的学习资源,结合以前的深度学习基础,我从最简单的Get Start开始。Pytorch的安装比较简单,我电脑已安装ananconda,在此基础上,跟随官网指导“START LOCALLY”很快安装完毕Pycharm,我选择CPU版本,不涉及到GPU,就不用去折腾cuda、conda这些。


首先,跟随tutoria—> 60-minute blitz中的几个教程:

① what is pytorch?

② AUTOGRAD

③ neural network

④ Training a Classifier

⑤ optional:Data Paralelism

我用的是12年高中毕业买的老电脑,GPU太老,就不跑⑤ optional:Data Paralelism了。敲代码用的事Pycharm.

① what is pytorch?

主要介绍了用Pytorch的意义和Tensor概念。

It’s a Python-based scientific computing package targeted at two sets of audiences:

· A replacement for NumPy to use the power of GPUs

· a deep learning research platform that provides maximum flexibility and speed

利用tourch创建变量和常量,以及对其的操作,涉及到rand、empty、zeros、zeros等。

② AUTOGRAD

介绍Pytorch自动求导问题。

③ neural network

  • 教你搭建一个经典的Lenet网络,诞生于1994年,由Yann LeCun提出,代码也很简洁。

import torch
import torch.nnas nn
import torch.nn.functionalas F
import torch.optimas optim
class Net(nn.Module):
    def __init__(self):
        super(Net,self).__init__()
# 1 input image channel, 6 output channels, 3x3 square convolution
        self.conv1= nn.Conv2d(1,6,3)
self.conv2= nn.Conv2d(6,16,3)
# an affine operation: y = Wx + b
        self.fc1= nn.Linear(16 * 6 * 6,120)# 6*6 from image dimension
        self.fc2= nn.Linear(120,84)
self.fc3= nn.Linear(84,10)
def forward(self,x):
        # Max pooling over a (2, 2) window
        x= F.max_pool2d(F.relu(self.conv1(x)), (2,2))
# If the size is a square you can only specify a single number
        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 sin size:
            num_features*= s
return num_features
net= Net()
print(net)

④ Training a Classifier

训练一个基于Cifar10数据集的分类网络,也相对较简单。

作者:张清博

-------------------------------------------

个性签名:半途而废

本文如有帮助,记得在右下角点个“推荐”哦,在此感谢!

原文地址:https://www.cnblogs.com/Ireland/p/12383321.html