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

PyTorch由4个主要包装组成：

1. Torch：类似于Numpy的通用数组库，可以在将张量类型转换为（torch.cuda.TensorFloat）并在GPU上进行计算。
3. torch.nn：具有共同层和成本函数的神经网络库

1.导入工具

`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支持下提供线性代数

`# 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]`

• 使用x.data访问其值。
• 在.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层（张量行的线性映射）+（非线性）-->

`# 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]`

• 定义自定义层时，需要实现2个功能：
• _ init_函数必须始终被继承，然后层的所有参数必须在这里定义为类变量（self.x）
`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也可以做优化—>

`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`

0 条评论

• ### 【学习】Python可视化工具概述-外文编译

本文由 PPV课 - korobas 翻译，未经许可，禁止转载！ 原文翻译链接：http://pbpython.com/visualization-tools...

• ### “数学之美”系列七：信息论在信息处理中的应用

我们已经介绍了信息熵，它是信息论的基础，我们这次谈谈信息论在自然语言处理中的应用。 先看看信息熵和语言模型的关系。我们在系列一中谈到语言模型时，没有讲如何定量地...

• ### 数据挖掘比赛通用框架

作者|穆文 报名啦CDA数据分析师认证培训Level 1 国内权威的数据分析师系统 培养学员超过上千人理论结合实际 更有多重福利提供 点击文末“阅读原文”...

• ### Python|神经网络框架torch包[1]

torch包主要是用于多维张量的数据结构和用于张量的数学操作。除此之外，还提供了许多用于张量有效序列化和任意类型的工具，还有一些其他相关的工具。

• ### 编写高效的PyTorch代码技巧（上）

原文：https://github.com/vahidk/EffectivePyTorch

• ### Pytorch 1.2.0 来了！

此次升级有超过1900个commit，包括在JIT、ONNX、分布式、性能以及前端等方面的大量改进。

由于stackgan项目需要，来安装个torch http://torch.ch/docs/getting-started.html 官网教程 执行下面三步，...

• ### 【专知国庆特刊-PyTorch手把手深度学习教程系列01】一文带你入门优雅的PyTorch

【导读】主题链路知识是我们专知的核心功能之一，为用户提供AI领域系统性的知识学习服务，一站式学习人工智能的知识，包含人工智能（ 机器学习、自然语言处理、计算机视...