一、软件安装篇
pytorch安装目前仅支持linux和mac安装,windows下的小伙伴目前暂不提供安装。我的电脑是Ubuntu 16.04 版本系统,安装pytorch的过程参考官网给出的步骤:

在命令窗口下运行以下两步
___第一步:pip install https://s3.amazonaws.com/pytorch/whl/cu75/torch-0.1.9.post2-cp27-none-linux_x86_64.whl ___
第二步:pip install torchvision
安装过程,如果提示没有权限的话,直接在pip前面加上管理员权限sudo。第二步中torchvision中主要包含了一些内置数据集。
二、资料整理篇
参考资料来源知乎回答:新手如何入门pytorch?
三、软件学习篇
该部分的参考资料绝大部分来源于pytorch官方文档
1. torch.Tensor
torch.Tensor是一个类似于numpy中的多维矩阵,其中包含的元素类型只能有一种。
-
tensor(张量)创建
tensor可以通过python中的list或者sequence来创建,例如
#torch.Tensor默认等价于torch.FloatTensor
>>>torch.FloatTensor([[1, 2, 3], [4, 5, 6]])
1 2 3
4 5 6
[torch.FloatTensor of size 2x3]
#除了列表;数组,矩阵这些都可以创建Tensor
当然一个空的张量empty tensor可以通过指定其大小来构建,例如
>>> torch.IntTensor(2, 4).zero_()
0 0 0 0
0 0 0 0
[torch.IntTensor of size 2x4]
#zero函数后面带的_什么作用等会马上有讲到
刚才提到过,tensor和numpy中的多维矩阵和类似的,因此同样可以采用索引和切片对其进行操作,例如
>>> x = torch.FloatTensor([[1, 2, 3], [4, 5, 6]])
>>> print(x[1][2])
6.0
>>> x[0][1] = 8
>>> print(x)
1 8 3
4 5 6
[torch.FloatTensor of size 2x3]
每一个tensor都会对应一个storage,storage中包含了该张量所有的数据,例如
>>>torch.FloatTensor([[1, 2, 3], [4, 5, 6]])
1 2 3
4 5 6
[torch.FloatTensor of size 2x3]
>>>print x.storage()
1.0
2.0
3.0
4.0
5.0
6.0
[torch.FloatStorage of size 6]
-
tensor(张量)修改
构建完tensor以后可以利用函数等对其进行修改,在pytorch中凡是带有下划线(underscore)的处理操作都是直接对原始tensor进行操作,并且返回修改后的tensor,没有带下划线的操作,是返回新的tensor,例如
#首先仍然创建一个tensor
>>>x = torch.FloatTensor([[1, 2, -3], [4, -5, 6]])
>>> print x
1 2 -3
4 -5 6
[torch.FloatTensor of size 2x3]
#我们利用tensor自带的属性abs和abs_进行对比处理
a1 = x.abs() #生成新的a1张量,x并没有改变
a2 = x.abs_() #生成新的a2张量的同时,x也已经发生改变
当然除了绝对值函数,pytorch中还提供了非常多的处理张量的函数,比如add,acos等等,详情见官网说明。
2. torch.autograd
torch.autograd能够在尽可能少改动现有代码的前提下,对所有函数实现自动微分功能,当然需要做的仅仅是将张量tensor包装成为Varibale,例如
#首先仍然创建一个tensor x
>>>x = torch.FloatTensor([[1, 2, -3], [4, -5, 6]])
#然后将其转化为Variable
from torch.autograd import Variable
x_trans = Variable(x)
print x_trans
autograd中的三大利器:torch.autograd.backward,torch.autograd.Variable和torch.autograd.Function,下面分别说下三个类的功能是什么。
-
torch.autograd.backward
torch.autograd.backward(variables, grad_variables, retain_variables=False)
其中, -
torch.autograd.Variable
官方文档解析:Wraps a tensor and records the operations applied to it.其实就是对tensor进行封装,而且能够记住对其做的所有操作(这个厉害了。。。。)。 -
torch.autograd.Function
3. torch.nn
包含了目前最著名的三个网络,分别是RNN,LSTM和GRU
4. torch.nn.functional
import torch.nn.functional as F
其中主要包含了卷积函数,下采样Pooling 函数,Non-linear activation functions,损失函数(loss function),dropout,batch-Normalization 等,对卷积和采样函数举例如下
- __Convolution __
>>> import torch.nn.functional as F
>>> filters = autograd.Variable(torch.randn(33, 16, 3))
>>> inputs = autograd.Variable(torch.randn(20, 16, 50))
>>> F.conv1d(inputs, filters)
#最终输出结果为度为20*33*48;要理解这个值是怎么计算出来,需要对卷积神经网络中的卷积计算有了解,
#理解卷积计算层的三条秘诀:
##(1)局部关联,每个神经元看做一个filter
##(2)窗口滑动,filter对局部进行计算
##(3)涉及概念:窗口、长度和步长值
- pooling
>>> import torch.nn.functional as F
>>> input = Variable(torch.Tensor([[[1,2,3,4,5,6,7]]]))
#kernel_size是窗口长度,stride是步长,采用均值方式
>>> print F.avg_pool1d(input,kernel_size=3,stride=2)
#输出结果为
Variable containing:
(0 ,.,.) =
2 4 6
[torch.FloatTensor of size 1x1x3]
- 正则化和dropout
>>> from torch import nn
>>> m = nn.Dropout(p=0.2)
>>> input = autograd.Variable(torch.randn(20, 16))
>>> output = m(input)
#输出结果为20*16,其中有些值被随机的置为0(按列还是按行?)
网友评论