文章目录
前言
你好,我是醉墨居士,今天分享一下PyTorch的基本使用的快速入门教程,希望能够帮助各位快速掌握PyTorch的使用
简介
PyTorch 是一个开源的深度学习框架,由 Facebook 的人工智能研究团队(FAIR)开发。它在学术界和工业界都被广泛使用,为深度学习的研究和应用提供了强大的支持
软件包导入
本教程中我们使用torch和numpy这两个包
如果是ubuntu系统,可以看这个教程配置环境:https://blog.csdn.net/qq_67733273/article/details/144787375
py
import torch
import numpy as np
创建张量
- form list
py
print(torch.tensor([1, 2]), torch.tensor([[1, 2], [3, 4]]))
bash
tensor([1, 2]) tensor([[1, 2],
[3, 4]])
- form numpy
py
print(torch.from_numpy(np.array([1, 2])))
bash
tensor([1, 2])
- 全0填充
py
print(torch.zeros(2, 3))
bash
tensor([[0., 0., 0.],
[0., 0., 0.]])
- 全1填充
py
print(torch.ones(2, 3))
bash
tensor([[1., 1., 1.],
[1., 1., 1.]])
- 自定义数值全部填充
py
print(torch.full([2, 3], 6))
bash
tensor([[6, 6, 6],
[6, 6, 6]])
- 对角矩阵
py
print(torch.eye(3, 3))
bash
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
- 未初始化
py
print(torch.empty(2, 3))
bash
tensor([[5.1981e-06, 0.0000e+00, 0.0000e+00],
[0.0000e+00, 1.5134e-21, 4.2892e-41]])
- 标准正态分布
py
print(torch.randn(2, 3))
bash
tensor([[ 0.0711, 0.2728, -0.4199],
[ 1.0625, 0.9611, -2.0447]])
- 0 - 1浮点均匀分布
py
print(torch.rand(2, 3))
bash
tensor([[0.2974, 0.6033, 0.7126],
[0.1599, 0.5974, 0.8983]])
- 1 - 9整数均匀分布
py
print(torch.randint(1, 10, [2, 3]))
bash
tensor([[1, 2, 8],
[9, 3, 4]])
- 递增数列, 0 ~ 9,步长是2
py
print(torch.arange(0, 10, 2))
bash
tensor([0, 2, 4, 6, 8])
- 等差数列,0 ~ 10, 共计4个数
py
print(torch.linspace(0, 10, 4))
bash
tensor([ 0.0000, 3.3333, 6.6667, 10.0000])
- 指定类型创建
py
print(torch.FloatTensor([1, 2]), torch.LongTensor([1, 2]))
bash
tensor([1., 2.]) tensor([1, 2])
类型操作
- 获取数据类型
py
print(torch.randn(2, 3).dtype)
bash
torch.float32
- 转换数据类型
py
print(torch.randn(2, 3).to(torch.float64).dtype)
bash
torch.float64
- 张量形状
py
print(torch.randn(2, 3).shape)
bash
torch.Size([2, 3])
索引
- 测试数据
4张图片,3通道,28*28像素
py
a = torch.randn(4, 3, 28, 28)
print(a.shape)
torch.Size([4, 3, 28, 28])
直接索引
- 查看第0张图片
py
print(a[0].shape)
torch.Size([3, 28, 28])
- 查看第0张图片的第0个通道
py
print(a[0, 0].shape)
bash
torch.Size([28, 28])
- 查看第0张图片的第0个通道的第2行
py
print(a[0, 0, 2].shape)
bash
torch.Size([28])
- 查看第0张图片的第0个通道的第2行第4列
py
print(a[0, 0, 2, 4].shape)
bash
torch.Size([])
切片索引
- 查看0 - 1张图片, 其中 :2 表示 0 - 1
py
print(a[:2].shape)
bash
torch.Size([2, 3, 28, 28])
- 查看0 - 1张图片的0 - 1通道
py
print(a[:2, :2].shape)
bash
torch.Size([2, 2, 28, 28])
- 查看0 - 1张图片的所有通道的倒数5 - 正数24行, 其中 : 表示所有
py
print(a[:2, :, -5:25].shape)
bash
torch.Size([2, 3, 2, 28])
- 有间隔的索引,其中 ::2 表示每次间隔2个取值
py
print(a[:, :, :, ::2].shape)
bash
torch.Size([4, 3, 28, 14])
- 查看所有图片的第0 - 1列, 其中 ... 表示省略
py
print(a[..., :2].shape)
bash
torch.Size([4, 3, 28, 2])
- 查看所有图片的第0 - 1行
py
print(a[..., :2, :].shape)
bash
torch.Size([4, 3, 2, 28])
- 查看第1张图片
py
print(a[1, ...].shape)
bash
torch.Size([3, 28, 28])
维度变换
- 测试数据
4张图片, 单通道, 28*28像素
py
a = torch.randn(4, 1, 28, 28)
print(a.shape)
bash
torch.Size([4, 1, 28, 28])
- 转换为4, 784维度
py
print(a.reshape(4, 784).shape)
bash
torch.Size([4, 784])
- 转换成4, 28, 28维度
py
print(a.reshape(4, 28, 28).shape)
bash
torch.Size([4, 28, 28])
- view的基本用法和reshape一致
py
print(a.view(4, 784).shape)
bash
torch.Size([4, 784])
增加维度
- 测试数据
2*2的tensor
py
a = torch.randn(2, 2)
print(a.shape)
bash
torch.Size([2, 2])
- 插入维度到第0维
py
print(a.unsqueeze(0).shape)
bash
torch.Size([1, 2, 2])
- 插入维度在倒数第1维
py
print(a.unsqueeze(-1).shape)
bash
torch.Size([2, 2, 1])
删除维度
- 测试数据
122*1的tensor
py
a = torch.randn(1, 2, 2, 1)
print(a.shape)
bash
torch.Size([1, 2, 2, 1])
- 删除第0维
py
print(a.squeeze(0).shape)
bash
torch.Size([2, 2, 1])
- 删除倒数第1维
py
print(a.squeeze(-1).shape)
bash
torch.Size([1, 2, 2])
- 删除所有为1的维度
py
print(a.squeeze().shape)
bash
torch.Size([2, 2])
维度重复
- 分别在第1个维度和第2个维度重复2和3次
py
print(torch.randn(2, 2).repeat(2, 3).shape)
bash
torch.Size([4, 6])
维度交换
- t转置,只能操作2维tensor
py
print(torch.randn(1, 2).t().shape)
bash
torch.Size([2, 1])
- 维度交换,指定交换的维度,只能两两交换
py
print(torch.randn(1, 2, 3).transpose(0, 1).shape)
bash
- 维度交换,输入维度的顺序
py
print(torch.rand(1, 2, 3).permute(2, 1, 0).shape)
bash
torch.Size([2, 1, 3])
broadcast
- 测试数据
3个张量
py
a = torch.randn(2, 3)
b = torch.randn(1, 3)
c = torch.randn(1)
print(a.shape)
print(b.shape)
print(c.shape)
bash
torch.Size([2, 3])
torch.Size([1, 3])
torch.Size([1])
- 自动boradcast
py
print((a + b).shape)
print((a + c).shape)
bash
torch.Size([2, 3])
torch.Size([2, 3])
- 手动boradcast
py
print(b.expand_as(a).shape)
print(c.expand_as(a).shape)
bash
torch.Size([2, 3])
torch.Size([2, 3])
合并张量
- 拼接,dim=0是指定要拼接的维度
py
a = torch.rand(4, 32, 8)
b = torch.rand(5, 32, 8)
print(a.shape)
print(b.shape)
print(torch.cat([a, b], dim=0).shape)
bash
torch.Size([4, 32, 8])
torch.Size([5, 32, 8])
torch.Size([9, 32, 8])
- 组合,创建一个新的维度,用于区分组合后的两个tensor
py
a = torch.rand(4, 32, 8)
b = torch.rand(4, 32, 8)
print(a.shape)
print(b.shape)
print(torch.stack([a, b], dim=0).shape)
bash
torch.Size([4, 32, 8])
torch.Size([4, 32, 8])
torch.Size([2, 4, 32, 8])
拆分张量
- 测试数据
1个张量
py
a = torch.rand(4, 32, 8)
print(a.shape)
bash
torch.Size([4, 32, 8])
- split拆分,在0维度上拆分,每2个元素1拆
py
b, c = a.split(2, dim=0)
print(b.shape)
print(c.shape)
bash
torch.Size([2, 32, 8])
torch.Size([2, 32, 8])
- split拆分,在0维度上拆分,拆分后长度分别为1,2,1
py
b, c, d = a.split([1, 2, 1], dim=0)
print(b.shape)
print(c.shape)
print(d.shape)
bash
torch.Size([1, 32, 8])
torch.Size([2, 32, 8])
torch.Size([1, 32, 8])
- chunk拆分,在0维度上拆分,拆成2个
py
b, c = a.chunk(2, dim=0)
print(b.shape)
print(c.shape)
bash
torch.Size([2, 32, 8])
torch.Size([2, 32, 8])
运算
测试数据
2个张量
py
a = torch.FloatTensor([[0, 1.1, 2.2], [3.3, 4.4, 5.5]])
b = torch.FloatTensor([0, 1.1, 2.2])
print(a.shape)
print(b.shape)
bash
torch.Size([2, 3])
torch.Size([3])
- 矩阵乘法
py
print(a @ b)
print(a.matmul(b))
bash
tensor([ 6.0500, 16.9400])
tensor([ 6.0500, 16.9400])
- 四则运算,因为两个tensor的维度不同,会进行自动boradcast,然后计算
py
print(a + b)
print(a - b)
print(a * b)
print(a / b)
bash
tensor([[0.0000, 2.2000, 4.4000],
[3.3000, 5.5000, 7.7000]])
tensor([[0.0000, 0.0000, 0.0000],
[3.3000, 3.3000, 3.3000]])
tensor([[ 0.0000, 1.2100, 4.8400],
[ 0.0000, 4.8400, 12.1000]])
tensor([[ nan, 1.0000, 1.0000],
[ inf, 4.0000, 2.5000]])
- 求指数
py
print(a**2)
bash
tensor([[ 0.0000, 1.2100, 4.8400],
[10.8900, 19.3600, 30.2500]])
- 开根号
py
print(a**0.5)
bash
tensor([[0.0000, 1.0488, 1.4832],
[1.8166, 2.0976, 2.3452]])
- 求e的n次方
py
print(a.exp())
bash
tensor([[ 1.0000, 3.0042, 9.0250],
[ 27.1126, 81.4509, 244.6919]])
- 以e为底,求对数
py
print(a.log())
bash
tensor([[ -inf, 0.0953, 0.7885],
[1.1939, 1.4816, 1.7047]])
- 以2为底,求对数
py
print(a.log2())
bash
tensor([[ -inf, 0.1375, 1.1375],
[1.7225, 2.1375, 2.4594]])
- 大于
py
print(a > b)
bash
tensor([[False, False, False],
[ True, True, True]])
- 大于等于
py
print(a >= b)
bash
tensor([[True, True, True],
[True, True, True]])
- 小于
py
print(a < b)
bash
tensor([[False, False, False],
[False, False, False]])
- 小于等于
py
print(a <= b)
bash
tensor([[ True, True, True],
[False, False, False]])
- 等于
py
print(a == b)
bash
tensor([[ True, True, True],
[False, False, False]])
- 不等于
py
print(a != b)
bash
tensor([[False, False, False],
[ True, True, True]])
- 限制数据的上下限
py
print(a.clamp(2, 4))
bash
tensor([[2.0000, 2.0000, 2.2000],
[3.3000, 4.0000, 4.0000]])
- 向下取整
py
print(a.floor())
bash
tensor([[0., 1., 2.],
[3., 4., 5.]])
- 向上取整
py
print(a.ceil())
bash
tensor([[0., 2., 3.],
[4., 5., 6.]])
- 四舍五入
py
print(a.round())
bash
tensor([[0., 1., 2.],
[3., 4., 6.]])
- 求最小
py
print(a.min())
bash
tensor(0.)
- 求最大
py
print(a.max())
bash
tensor(5.5000)
- 求平均
py
print(a.mean())
bash
tensor(2.7500)
- 求积
py
print(a.prod())
bash
tensor(0.)
- 求和
py
print(a.sum())
bash
tensor(16.5000)
- 求最大值下标
py
print(a.argmax())
bash
tensor(5)
- 求最小值下标
py
print(a.argmin())
bash
tensor(0)
- 分维度求最大
py
print(a.max(dim=0))
bash
torch.return_types.max(
values=tensor([3.3000, 4.4000, 5.5000]),
indices=tensor([1, 1, 1]))
- 分维度求最大值下标
py
print(a.argmax(dim=0))
bash
tensor([1, 1, 1])
- 求1范数
py
print(a.norm(1))
print(a.norm(1, dim=0))
bash
tensor(16.5000)
tensor([3.3000, 5.5000, 7.7000])
- 求2范数
py
print(a.norm(2))
print(a.norm(2, dim=0))
bash
tensor(8.1578)
tensor([3.3000, 4.5354, 5.9237])
- 求前2个最小值
py
print(a.topk(2, dim=1, largest=False))
bash
torch.return_types.topk(
values=tensor([[0.0000, 1.1000],
[3.3000, 4.4000]]),
indices=tensor([[0, 1],
[0, 1]]))
- 求第2个小的值
py
a.kthvalue(2, dim=1)
bash
torch.return_types.kthvalue(
values=tensor([1.1000, 4.4000]),
indices=tensor([1, 1]))
最后
感谢您的阅读,我是醉墨居士,希望对你有所帮助,也希望你能够使用PyTorch为你的未来开创更多可能性