《nlp入门+实战:第二章:pytorch的入门使用 》


上一篇: 《nlp入门+实战:第一章:深度学习和神经网络》
本章代码链接: https://gitee.com/ninesuntec/nlp-entry-practice/blob/master/code/2.pytorch.py

1.pytorch的介绍

Pytorch是一款facebook发布的深度学习框架,由其易用性,友好性。深受广大用户青睐。

2.pytorch的版本

在这里插入图片描述

详情请参考pytorch官网:https://pytorch.org/get-started/locally/
上面有详细的安装步骤,当然,你也可以基于anaconda的环境去安装pytorch的环境

当然,你的电脑如果没有gpu的话,前期的简单计算依赖于cpu也完全没问题,安装命令如下:
在这里插入图片描述

3.pytorch的入门使用

3.1 张量Tensor

张量是一个统称,其中包含很多类型:

  • 0阶张量:标量、常数,0-D Tensor
  • 1阶张量:向量,1-D Tensor
  • 2阶张量:矩阵,2-D Tensor
  • 3阶张量
  • 5…
  • N阶张量

有了numpy,序列,数组等等,为啥还需要张量呢,在我们pytorch框架中,它只认识张量,,所以我们需要将数据转为张量类型,pytorch才能去解析。

3.2 在Pytorch中创建张量

在学习本小节之前,请确保自己已经安装好了pytorch以及numpy
我是通过anaconda安装的,不会的可以自行百度一下,教程很多。

1.使用python中的列表或者序列创建张量

import torch
import numpy as np
t1=torch.tensor([[1, -1], [1, -1]])
print(t1)

本文只在第一代码段中显示了torch和numpy的导入,后面的默认都是加了的,请大家自行补上。同时本文不没有给出运行结果的截图,旨在确保大家自己手动敲一遍代码,否则就是囫囵吞枣,收获不大的。

2.使用Numpy中的数组创建张量

t2 = torch.tensor(np.array([[1, 2, 3], [4, 5, 6]]))
print(t2)

3.使用torch中的api创建tensor

t3_0 = torch.empty(3, 4)  # 创建3行4列的空tensor,会用无用数据去填充
t3_1 = torch.ones([3, 4])  # 创建3行4列全为1的tensor
t3_2 = torch.zeros([3, 4])  # 创建3行4列全为0的tensor
t3_3 = torch.rand([3, 4])  # 创建3行4列随机值的tensor,随机区间为(0,1)
t3_4 = torch.randint(low=0, high=10, size=[3, 4])  # 创建3行4列随机整数的tensor,随机区间为(low,high)
t3_5 = torch.randn([3, 4])  # 创建3行4列随机数的tensor,随机值的均值为0,方差为1

3.3 Pytorch中的tensor常用方法

1.获取tensor中的数据(当tensor中只有一个元素可用):tensor.item()

t4 = torch.tensor(np.arange(1))
print(t4)
print(t4.item())

在这里插入图片描述

2.转化为numpy数组

t5_1 = torch.randint(low=0, high=10, size=[3, 4])
t5_2=t5_1.numpy()
print(t5_1)
print(t5_2)

在这里插入图片描述

3.获取形状:tensor.size()

t6 = torch.randint(low=0, high=10, size=[3, 4])
print(t6)
print(t6.size())

在这里插入图片描述

4.形状改变:tensor.view((3,4))。类似numpy中的reshape,是一种浅拷贝,仅仅是形状发生改变

t7 = torch.randint(low=0, high=10, size=[3, 4])
print(t7)
print(t7.view(2, 6))

在这里插入图片描述

注意,无论怎么转换都要保证最终的乘积不变,如3* 4=2*6
5.获取维数:tensor.dim()

t8 = torch.randint(low=0, high=10, size=[3, 4])
print(t8)
print(t8.dim())

在这里插入图片描述

6.获取最大值:tensor.max()

t9 = torch.randint(low=0, high=10, size=[3, 4])
print(t9)
print(t9.max())

在这里插入图片描述

7.转置:tensor.t()

t10 = torch.randint(low=0, high=10, size=[3, 4])
print(t10)
print(t10.t())

在这里插入图片描述

8.获取tensor第m行n列的值:tensor[m,n]

t11 = torch.randint(low=0, high=10, size=[3, 4])
print(t11)
print(t11[1, 2])  # 获取第二行第3列的值

在这里插入图片描述

9.赋值:tensor[m,n]=x,将第m行n列的值赋值为x

t12 = torch.randint(low=0, high=10, size=[3, 4])
print(t12)
t12[1,2]=1000
print(t12)

在这里插入图片描述

10.交换矩阵的两个维度:torch.transpose(x, 0, 1)

t13 = torch.randn(2, 3);  # 2行3列
print(t13)
t13=torch.transpose(t13, 0, 1)
print(t13)  # 将t13变为3行2列

在这里插入图片描述

在这里插入图片描述

如果我们遇到这种类型的数据,里面的[1,2,3]分别代表是什么意思呢?
意思是该张量是个三维张量,其中

  • 1:里面只有一块数据
  • 2,3:代表每一块数据都是一个2行3列的数据

3.4 tensor的数据类型

在这里插入图片描述

1.获取tensor的数据类型:x.dtype

t15 = torch.randn([3, 4])
print(t15.dtype)

在这里插入图片描述

2.在创建张量时,也可通过dtype来指定数据类型

t16 = torch.randn([3, 4],dtype=torch.double)
print(t16.dtype)

在这里插入图片描述

当然我们也可通过FloatTensor、LongTensor等来创建我们需要的制定数据类型

t17 = torch.LongTensor([1, 2])
print(t17)

在这里插入图片描述

3.5 tensor的其他操作

1.tensor与tensor的相加

t18 = t17.new_ones(5, 3, dtype=torch.float)
t18_1 = torch.randn(5, 3)
print(t18)
print(t18_1)
print(t18 + t18_1)
print(torch.add(t18, t18_1))
print(t18.add(t18_1))
t18.add_(t18_1)  # 带下划线的方法会对t18进行就地修改
print(t18)

在这里插入图片描述

在这里插入图片描述

2.tensor和数字操作

t19 = torch.randn(3, 3)
print("t19:", t19)
print("t19+10=", t19 + 10)

在这里插入图片描述

3.CUDA中的tensor
CUDA (Compute Unified Device Architecture),是NVIDIA推出的运算平台。

CUDATM是一种由NVIDIA推出的通用并行计算架构,该架构使GPU能够解决复杂的计算问题。

torch.cuda这个模块增加了对CUDA tensor的支持,能够在cpu和gpu上使用相同的方法操作tensor

通过.to方法能够把一个tensor转移到另外一个设备(比如从CPU转到GPU)

if torch.cuda.is_available():
    device = torch.device("cuda")  # cuda device对象
    y = torch.ones_like(t19, device=device)  # 创建一个cuda的tensor
    x = t19.to(device)  # 使用方法把t19转化为cuda的tensor
    z = x + y
    print(z)
    print(z.to("cpu", torch.double))  # .to方法也能够同时设置类型
else:
    print("您的设备不支持gpu运算")

下一篇:《nlp入门+实战:第三章:梯度下降和反向传播 》

猜你喜欢

转载自blog.csdn.net/zhiyikeji/article/details/125954292
今日推荐