基于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 (导数)的信息 ,这听起来很抽象,让我们来看看
阅读更多
相关文章推荐
- Logo 首页 下载App 深度学习从小白到入门 —— 基于keras的深度学习基本概念讲解 96 作者 shikanon 2017.02.18 20:27 字数 2551 阅读
- 基于tcpdump实例讲解TCP/IP协议
- SpringMVC详解(三)------基于注解的入门实例
- 基于PyTorch的深度学习入门教程(一)——PyTorch安装和配置
- mybatis 详解(二)------入门实例(基于XML)
- zookeeper 入门讲解实例 转
- Spring mvc整合mybatis基于mysql数据库实现用户增删改查及其分页显示的完整入门实例【转】
- 实例讲解基于Backup Exec 2012完全备份+差异备份恢复Exchange数据库
- Spring MVC入门实例及详细讲解
- Spring mvc整合mybatis基于mysql数据库实现用户增删改查及其分页显示的完整入门实例【转】
- Spring MVC入门级实例-基于xml的配置
- mybatis 详解(二)------入门实例(基于XML)
- SpringMVC入门实例及详细讲解 (一)
- [webGL学习]基于three.js构建WebGL实例讲解大纲
- mybatis 详解(二)------入门实例(基于XML)
- SpringMVC入门实例及详细讲解
- Spring mvc整合mybatis基于mysql数据库实现用户增删改查及其分页显示的完整入门实例【转】
- SpringMVC入门实例及详细讲解
- Oracle存储过程实例入门分享讲解
- mybatis 详解(二)------入门实例(基于XML)