强大的PyTorch:10分钟让你了解深度学习领域新流行的框架

来源:互联网 发布:淘宝盗图不被投诉 编辑:程序博客网 时间:2024/03/29 20:53

原文链接

PyTorch由于使用了强大的GPU加速的Tensor计算(类似numpy)和基于tape的autograd系统的深度神经网络。这使得今年一月份被开源的PyTorch成为了深度学习领域新流行框架,许多新的论文在发表过程中都加入了大多数人不理解的PyTorch代码。这篇文章我们就来讲述一下我对PyTorch代码的理解,希望能帮助你阅读PyTorch代码。整个过程是基于贾斯汀·约翰逊的伟大教程。如果你想了解更多或者有超过10分钟的时间,建议你去读下整篇代码。

PyTorch由4个主要包装组成:

  1. Torch:类似于Numpy的通用数组库,可以在将张量类型转换为(torch.cuda.TensorFloat)并在GPU上进行计算。
  2. torch.autograd:用于构建计算图形并自动获取渐变的包
  3. torch.nn:具有共同层和成本函数的神经网络库
  4. torch.optim:具有通用优化算法(如SGD,Adam等)的优化包

1.导入工具

你可以这样导入PyTorch:

import torch # arrays on GPUimport torch.autograd as autograd #build a computational graphimport torch.nn as nn # neural net libraryimport torch.nn.functional as F # most non-linearities are hereimport torch.optim as optim # optimization package

2.torch数组取代了numpy ndarray - >在GPU支持下提供线性代数

第一个特色,PyTorch提供了一个像Numpy数组一样的多维数组,当数据类型被转换为(torch.cuda.TensorFloat)时,可以在GPU上进行处理。这个数组和它的关联函数是一般的科学计算工具。

从下面的代码中,我们可以发现,PyTorch提供的这个包的功能可以将我们常用的二维数组变成GPU可以处理的三维数组。这极大的提高了GPU的利用效率,提升了计算速度。

大家可以自己比较 Torch和numpy ,从而发现他们的优缺点。

# 2 matrices of size 2x3 into a 3d tensor 2x2x3d=[[[1., 2.,3.],[4.,5.,6.]],[[7.,8.,9.],[11.,12.,13.]]]d=torch.Tensor(d) # array from python listprint "shape of the tensor:",d.size()# the first index is the depthz=d[0]+d[1]print "adding up the two matrices of the 3d tensor:",zshape of the tensor: torch.Size([2, 2, 3])adding up the two matrices of the 3d tensor:   8  10  12 15  17  19[torch.FloatTensor of size 2x3]# a heavily used operation is reshaping of tensors using .view()print d.view(2,-1) #-1 makes torch infer the second dim  1   2   3   4   5   6  7   8   9  11  12  13[torch.FloatTensor of size 2x6]

3.torch.autograd可以生成一个计算图 - >自动计算梯度

第二个特色是autograd包,其提供了定义计算图的能力,以便我们可以自动计算渐变梯度。在计算图中,一个节点是一个数组,边(edge)是on数组的一个操作。要做一个计算图,我们需要在(torch.aurograd.Variable())函数中通过包装数组来创建一个节点。那么我们在这个节点上所做的所有操作都将被定义为边,它们将是计算图中新的节点。图中的每个节点都有一个(node.data)属性,它是一个多维数组和一个(node.grad)属性,这是相对于一些标量值的渐变(node.grad也是一个.Variable()) 。在定义计算图之后,我们可以使用单个命令(loss.backward())来计算图中所有节点的损耗梯度。

  • 使用torch.autograd.Variable()将张量转换为计算图中的节点。
    • 使用x.data访问其值。
    • 使用x.grad访问其渐变。
  • 在.Variable()上执行操作,绘制图形的边缘。
# d is a tensor not a node, to create a node based on it:x= autograd.Variable(d, requires_grad=True)print "the node's data is the tensor:", x.data.size()print "the node's gradient is empty at creation:", x.grad # the grad is empty right nowthe node's data is the tensor: torch.Size([2, 2, 3])the node's gradient is empty at creation: None# do operation on the node to make a computational graphy= x+1z=x+ys=z.sum()print s.creator<torch.autograd._functions.reduce.Sum object at 0x7f1e59988790># calculate gradientss.backward()print "the variable now has gradients:",x.gradthe variable now has gradients: Variable containing:(0 ,.,.) =   2  2  2  2  2  2(1 ,.,.) =   2  2  2  2  2  2[torch.FloatTensor of size 2x2x3]

4.torch.nn包含各种NN层(张量行的线性映射)+(非线性)-->

其作用是有助于构建神经网络计算图,而无需手动操纵张量和参数,减少不必要的麻烦。

第三个特色是高级神经网络库(torch.nn),其抽象出了神经网络层中的所有参数处理,以便于在通过几个命令(例如torch.nn.conv)就很容易地定义NN。这个包也带有流行的损失函数的功能(例如torch.nn.MSEloss)。我们首先定义一个模型容器,例如使用(torch.nn.Sequential)的层序列的模型,然后在序列中列出我们期望的层。这个高级神经网络库也可以处理其他的事情,我们可以使用(model.parameters())访问参数(Variable())

# linear transformation of a 2x5 matrix into a 2x3 matrixlinear_map=nn.Linear(5,3)print "using randomly initialized params:", linear_map.parametersusing randomly initialized params: <bound method Linear.parameters of Linear (5 -> 3)># data has 2 examples with 5 features and 3 targetdata=torch.randn(2,5) # trainingy=autograd.Variable(torch.randn(2,3)) # target# make a nodex=autograd.Variable(data, requires_grad=True)# apply transformation to a node creates a computational grapha=linear_map(x)z=F.relu(a)o=F.softmax(z)print "output of softmax as a probability distribution:", o.data.view(1,-1)# loss functionloss_func=nn.MSELoss() #instantiate loss functionL=loss_func(z,y) # calculateMSE loss between output and targetprint "Loss:", Loutput of softmax as a probability distribution:  0.2092  0.1979  0.5929  0.4343  0.3038  0.2619[torch.FloatTensor of size 1x6]Loss: Variable containing: 2.9838[torch.FloatTensor of size 1]

我们还可以通过子类(torch.nn.Module)定义自定义层,并实现接受(Variable())作为输入的(forward())函数,并产生(Variable())作为输出。我们也可以通过定义一个时间变化的层来做一个动态网络。

  • 定义自定义层时,需要实现2个功能:
    • init_函数必须始终被继承,然后层的所有参数必须在这里定义为类变量(self.x)
    • 正向函数是我们通过层传递输入的函数,使用参数对输入进行操作并返回输出。输入需要是一个autograd.Variable(),以便pytorch可以构建图层的计算图。
class Log_reg_classifier(nn.Module):    def __init__(self, in_size,out_size):        super(Log_reg_classifier,self).__init__() #always call parent's init         self.linear=nn.Linear(in_size, out_size) #layer parameters    def forward(self,vect):        return F.log_softmax(self.linear(vect)) # 

5.torch.optim也可以做优化—>

我们使用torch.nn构建一个nn计算图,使用torch.autograd来计算梯度,然后将它们提供给torch.optim来更新网络参数。

第四个特色是与NN库一起工作的优化软件包(torch.optim)。该库包含复杂的优化器,如AdamRMSprop等。我们定义一个优化器并传递网络参数和学习率(opt = torch.optim.Adammodel.parameters(),lr = learning_rate)),然后我们调用(opt.step())对我们的参数进行近一步更新。

optimizer=optim.SGD(linear_map.parameters(),lr=1e-2) # instantiate optimizer with model params + learning rate# epoch loop: we run following until convergenceoptimizer.zero_grad() # make gradients zeroL.backward(retain_variables=True)optimizer.step()print LVariable containing: 2.9838[torch.FloatTensor of size 1]

建立神经网络很容易,但是如何协同工作并不容易。这是一个示例显示如何协同工作:


# define modelmodel = Log_reg_classifier(10,2)# define loss functionloss_func=nn.MSELoss() # define optimizeroptimizer=optim.SGD(model.parameters(),lr=1e-1)# send data through model in minibatches for 10 epochsfor epoch in range(10):    for minibatch, target in data:        model.zero_grad() # pytorch accumulates gradients, making them zero for each minibatch        #forward pass        out=model(autograd.Variable(minibatch))        #backward pass         L=loss_func(out,target) #calculate loss        L.backward() # calculate gradients        optimizer.step() # make an update step

希望上述的介绍能够帮你更好的阅读PyTorch代码。  

本文由北邮@爱可可-爱生活老师推荐,阿里云云栖社区组织翻译。

文章原标题《Understand PyTorch code in 10 minutes》,

作者: Hamidreza Saghir机器学习研究员 - 多伦多大学博士生 译者:袁虎 审阅:阿福

文章为简译,更为详细的内容,请查看原文



原文链接



阅读全文
0 0
原创粉丝点击