学习使用 Pytorch 进行 NLP 项目实践
1. 基本数据模块 张量(Tensor)
N维矩阵
python的数据类型转为tensor
1 | torch.tensor() |
一维 tensor, 通过索引取到值
二维 tensor,通过索引取到向量
三维 tensor,通过索引取到矩阵
张量的拼接
1 | torch.cat([]) ## 默认按照第一个维度进行拼接,可以用axis指定维度,默认axis=0 |
Tensor变形
1 | x.view() ## 改变tensor的形状 |
2. 计算图以及自动微分
Pytorch 1.0 中只需要在构建 tensor 时加入 required_grad = True
, 这个tensor上的操作都会构造一个计算图,从而实现自动求导。
1 | eg: |
- 本文作者: Kelly Liu
- 本文链接: http://tiantianliu2018.github.io/2019/08/11/Learn-NLP-using-Pytorch/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!