您的位置:首页 > 其它

基于pytorch的NLP实例讲解(包括pytorch入门讲解)

2018-10-07 15:08 555 查看

本教程会让你对使用pytorch进行深度学习编程有较为详细的认识,许多概念(比如计算图和自动求导)并不是pytorch特有,许多深度学习框架都有此特性。

本教程针对的是没有用过任何深度学习框架的人,比如TF、KERAS等。

[code]import torch
import torch.autograd as autograd
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

torch.manual_seed(1)

1.tensor简介

所有深度学习的计算都是在tensor上进行的,它是对矩阵的推广,不仅仅限于2维,可以更多维度,首先让我们看看我们可以怎么 操作tensor。

创建tensors

我们可以使用torch.Tensor()方法来创建

[code]# Create a torch.Tensor object with the given data.  It is a 1D vector
V_data = [1., 2., 3.]
V = torch.Tensor(V_data)
print V

# Creates a matrix
M_data = [[1., 2., 3.], [4., 5., 6]]
M = torch.Tensor(M_data)
print M

# Create a 3D tensor of size 2x2x2.
T_data = [[[1.,2.], [3.,4.]],
[[5.,6.], [7.,8.]]]
T = torch.Tensor(T_data)
print T
[code] 1
2
3
[torch.FloatTensor of size 3]

1  2  3
4  5  6
[torch.FloatTensor of size 2x3]

(0 ,.,.) =
1  2
3  4

(1 ,.,.) =
5  6
7  8
[torch.FloatTensor of size 2x2x2]
[code]# Index into V and get a scalar
print V[0]

# Index into M and get a vector
print M[0]

# Index into T and get a matrix
print T[0]
[code]1.0

1
2
3
[torch.FloatTensor of size 3]

1  2
3  4
[torch.FloatTensor of size 2x2]
[code]x = torch.randn((3, 4, 5))
print x

(0 ,.,.) =
-2.9718  1.7070 -0.4305 -2.2820  0.5237
0.0004 -1.2039  3.5283  0.4434  0.5848
0.8407  0.5510  0.3863  0.9124 -0.8410
1.2282 -1.8661  1.4146 -1.8781 -0.4674

(1 ,.,.) =
-0.7576  0.4215 -0.4827 -1.1198  0.3056
1.0386  0.5206 -0.5006  1.2182  0.2117
-1.0613 -1.9441 -0.9596  0.5489 -0.9901
-0.3826  1.5037  1.8267  0.5561  1.6445

(2 ,.,.) =
0.4973 -1.5067  1.7661 -0.3569 -0.1713
0.4068 -0.4284 -1.1299  1.4274 -1.4027
1.4825 -1.1559  1.6190  0.9581  0.7747
0.1940  0.1687  0.3061  1.0743 -1.0327
[torch.FloatTensor of size 3x4x5]

2.计算图和自动微分

计算图的概念对于高效的深度学习编程是十分必要的,因为它让你不再需要去编写反向传播微分的部分,计算图包含了足够的计算derivative (导数)的信息 ,这听起来很抽象,让我们来看看

 

https://github.com/rguthrie3/DeepLearningForNLPInPytorch/blob/master/Deep%20Learning%20for%20Natural%20Language%20Processing%20with%20Pytorch.ipynb

 

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: