1小时快速入门PyTorch
python
1 | #二话不说,先把包导入进来~ |
tensor初始化
python
1 | #定义一个tensor |
tensor([[1, 2, 3],
[4, 5, 6]])
python
1 | #指定tensor的数据类型 |
tensor([[1., 2., 3.],
[4., 5., 6.]])
python
1 | #指定device |
tensor([[1., 2., 3.],
[4., 5., 6.]], device='cuda:0')
python
1 | #如果有gpu则使用gpu,此时device='cuda',否则使用cpu |
cuda
python
1 | #requires_grad:是否可被求导 |
tensor([[1., 2., 3.],
[4., 5., 6.]], device='cuda:0', requires_grad=True)
python
1 | print(my_tensor.device) |
cuda:0
True
torch.float32
torch.Size([2, 3])
python
1 | x=torch.empty(size=(3,3)) |
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
python
1 | #全0张量 |
python
1 | #全1张良 |
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
python
1 | #元素为随机数的张量 |
tensor([[0.4704, 0.6278, 0.2294],
[0.1838, 0.4951, 0.8452],
[0.7331, 0.8264, 0.9475]])
python
1 | #从0到9之间随机采样整数组成张量的元素 |
tensor([[7, 8],
[9, 3]])
python
1 | #从标准正态分布中采样随机数 |
tensor([[ 0.7711, -1.2354, -0.6476],
[ 1.2426, -0.8406, -1.1450]])
python
1 | #单位阵 |
tensor([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])
python
1 | #整数序列 |
tensor([0, 1, 2, 3, 4])
python
1 | #将从start到end之间的steps个数字作为张量x的元素 |
tensor([0.1000, 0.2000, 0.3000, 0.4000, 0.5000, 0.6000, 0.7000, 0.8000, 0.9000,
1.0000])
python
1 | #从标准正态分布中随机采样 |
tensor([[ 0.3601, -1.2583, -0.1399, 0.9521, 1.1094]])
python
1 | #从均匀分布中随机采样 |
tensor([[0.7726, 0.3465, 0.3890, 0.4156, 0.4586]])
python
1 | #输入是一个向量,返回一个以输入向量为对角线元素的对角阵 |
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
tensor([1., 1., 1.])
tensor数据类型转换
python
1 | #tensor数据类型转换 |
tensor: tensor([0, 1, 2, 3])
tensor.bool: tensor([False, True, True, True])
tensor.short(): tensor([0, 1, 2, 3], dtype=torch.int16)
tensor.long(): tensor([0, 1, 2, 3])
tensor.long().dtype: torch.int64
tensor.half(): tensor([0., 1., 2., 3.], dtype=torch.float16)
tensor.float(): tensor([0., 1., 2., 3.])
tensor.float().dtype: torch.float32
tensor.double(): tensor([0., 1., 2., 3.], dtype=torch.float64)
tensor与np array 互转
python
1 | import numpy as np |
python
1 | #从np.array转tensor |
tensor([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]], dtype=torch.float64)
python
1 | #tensor转np.array |
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
tensor的数学运算
python
1 | x=torch.tensor([1,2,3]) |
python
1 | z1=torch.empty(3) |
tensor([10., 10., 10.])
tensor([10, 10, 10])
tensor([10, 10, 10])
python
1 | #tensor减法 |
tensor([-8, -6, -4])
python
1 | #逐点除法 |
tensor([0.1111, 0.2500, 0.4286])
python
1 | #inplace operations |
tensor([1., 2., 3.])
python
1 | #高阶运算 |
tensor([1, 4, 9])
tensor([1, 4, 9])
python
1 | #比较 |
tensor([ True, False, True])
python
1 | #矩阵乘法 |
torch.Size([2, 3])
python
1 | #高阶矩阵操作 |
tensor([[ 37., 54.],
[ 81., 118.]])
tensor([[ 37., 54.],
[ 81., 118.]])
python
1 | # 向量点乘(相当于把其中一个向量转置,然后两个向量做矩阵乘法) |
tensor(46)
python
1 | #点对点矩阵乘法 |
tensor([ 9, 16, 21])
python
1 | # 批量矩阵乘法 |
torch.Size([32, 10, 10])
python
1 | #广播 |
tensor([[-1, -1, 2],
[ 2, 2, 5]])
tensor([[ 1, 8, 3],
[ 16, 125, 6]])
python
1 | #Other useful tensor operations |
su_x: tensor([9, 7, 2])
values:tensor([5, 5, 3]),indices:tensor([0, 1, 0])
abs_x: tensor([[5, 2, 3],
[4, 5, 1]])
argmax: tensor([0, 1, 0])
argmin: tensor([1, 0, 1])
mean_x: tensor([4.5000, 3.5000, 1.0000])
eq: tensor([[ True, True, True],
[ True, False, False]])
sort: torch.return_types.sort(
values=tensor([[4, 2, 3],
[5, 7, 6]]),
indices=tensor([[1, 0, 0],
[0, 1, 1]]))
x after clamp: tensor([[5, 2, 3],
[4, 5, 0]])
python
1 | x=torch.tensor([1,0,1,1,1],dtype=torch.bool) |
tensor(True)
tensor(False)
tensor索引
python
1 | batch_size=10 |
python
1 | print(x.shape) |
torch.Size([10, 25])
torch.Size([25])
python
1 | print(x[:,0].shape) |
torch.Size([10])
python
1 | print(x[2,0:10].shape) |
torch.Size([10])
python
1 | x[0,0]=10 |
tensor(10.)
python
1 | #fancy indexing |
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
tensor([2, 5, 8])
python
1 | x=torch.tensor([[1,2,3,4],[5,6,7,8],[9,8,7,6]]) |
tensor([7, 1])
python
1 | x=torch.arange(10) |
tensor([0, 1, 9])
tensor([], dtype=torch.int64)
tensor([0, 2, 4, 6, 8])
python
1 | #uselful operations |
tensor([ 0, 2, 4, 6, 8, 10, 6, 7, 8, 9])
python
1 | #去重 |
tensor([0, 1, 2, 3])
python
1 | x=torch.rand((4,5,3)) |
3
60
tesnor reshape
python
1 | x=torch.arange(9) |
tensor([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
tensor([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
python
1 | x=torch.arange(9) |
tensor([[0, 3, 6],
[1, 4, 7],
[2, 5, 8]])
tensor([[0, 3, 6],
[1, 4, 7],
[2, 5, 8]])
python
1 | x1=torch.rand(2,5) |
torch.Size([4, 5])
torch.Size([2, 10])
python
1 | #相当于flatten |
torch.Size([10])
python
1 | #相当于做批量flatten |
torch.Size([64, 10])
python
1 | #改变维度所在axis,类似于二维的转置 |
torch.Size([64, 5, 2])
python
1 | #维度压缩 |
torch.Size([1, 10])
torch.Size([10, 1])
torch.Size([10])
python
1 | #维度扩增 |
torch.Size([1, 1, 10])
torch.Size([1, 10])
以上内容大部分总结自一个Youtube小哥的视频,大家可以去看一下:
https://www.youtube.com/watch?v=x9JiIFvlUwk
当然,为了照顾无法科学上网的小伙伴,我已经将其搬运到了万能的B站:
https://www.bilibili.com/video/BV19v411j7Xd/
多年以后,
在一个风和日丽的午后,
一个老人躺在摇椅上晒着太阳,
有一个小孩大叫道:“爷爷,爷爷,你的推文有人点赞啦!”