PyTorch中的Tensor基本操作总结

Tensor是PyTorch 存储数据的基本单元, 数据都是存储在tensor中然后进行运算的. 本文总结 PyTorch中tensor的一些基本操作. 包括: 1, 创建tensor. 2, 改变Tensor中数据的type和shape. 3, tensor间的基本运算.

创建tensor

In [1]: import torch
In [2]: import numpy as np

从 int list中创建
In [3]: a = [1,2,3]
In [4]: t_a = torch.tensor(a)
In [5]: print(t_a)
tensor([1, 2, 3])

从numpy array中创建
In [6]: b = np.array([1, 2, 3, 4], dtype=np.int32)
In [7]: t_b = torch.from_numpy(b)
In [8]: print(t_b)
tensor([1, 2, 3, 4], dtype=torch.int32)

直接创建ones矩阵
In [9]: t_c = torch.ones(3,3)
In [10]: print(t_c)
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])

直接创建随机矩阵范围 [0,1)
In [11]: t_d = torch.rand(2,3)
In [12]: print(t_d)
tensor([[0.6955, 0.8926, 0.9778],
        [0.3022, 0.4747, 0.9094]])

改变tensor的形状与数据type 

改变tensor内数据的type
In [13]: t_a_new = t_a.to(torch.int64)

In [14]: print(t_a_new.dtype)
torch.int64


改变tensor shape 
In [15]: t = torch.zeros(20)

In [16]: t_reshape = t.reshape(4, 5)

In [17]: print(t_reshape.shape)
torch.Size([4, 5])


tensor转置
In [18]: t = torch.rand(4, 5)

In [19]: t_transpose = torch.transpose(t, 0, 1)

In [23]: print(t.shape, "-->",t_transpose.shape)
torch.Size([4, 5]) --> torch.Size([5, 4])


维度压缩, 消除多余的维度.
In [25]: t = torch.ones(1, 3, 4, 5)

In [26]: t_squeeze = torch.squeeze(t, 0)

In [27]: print(t_squeeze.shape)
torch.Size([3, 4, 5])

tensor中的数学运算


In [30]: t1 = 5 + torch.rand(3, 4) * 2

In [31]: t2 = torch.normal(mean=0, std=1, size=(3, 4))



t1 与t2 点乘(element wise)
In [32]: t3 = torch.multiply(t1, t2)

In [33]: print(t3)
tensor([[ 3.7544,  1.0344, -6.6131,  2.6453],
        [ 0.0394,  8.2370, 12.3229,  3.4751],
        [ 4.4234, 12.7690, -0.9023, -3.5027]])



X乘
In [35]: t4 = torch.torch.matmul(t1, torch.transpose(t2, 0, 1))

In [36]: print(t4)
tensor([[ 0.8210, 23.2943, 11.9209],
        [-0.1248, 24.0745, 13.7630],
        [-1.4852, 26.2885, 12.7874]])




按照某轴的 mean, sum,std运算. 
如: t1是个3行4列 的矩阵
In [37]: print(t1)
tensor([[6.6838, 5.7557, 5.2833, 6.8798],
        [5.5056, 6.8812, 5.3308, 5.7699],
        [5.3363, 6.5976, 6.3740, 6.0077]])

按t1的列计算平均值
In [38]: t5 = torch.mean(t1, axis=0)
In [39]: print(t5)
tensor([5.8419, 6.4115, 5.6627, 6.2191])

按行计算总和:
In [40]: t6 = torch.sum(t1, axis=1)
In [41]: print(t6)
tensor([24.6026, 23.4875, 24.3156])

Tensor的拆分与合并,

拆分

pytorch中常用的tensor拆分方法有:troch.chunk()

In [7]: import torch

In [8]: t_split = torch.chunk(t, 3)

In [9]: [item.numpy() for item in t_split]
Out[9]: 
[array([0.70972687, 0.27158302, 0.60073674], dtype=float32),
 array([0.7532348 , 0.4176404 , 0.36205906], dtype=float32),
 array([0.12663347, 0.7341193 , 0.13570935], dtype=float32)]

torch.chunk(tensor, 整数参数), tensor 中沿着要切的维度的size必须能被后面的参数整除。 如果不能整除,需要用到torch.split() 如: 

In [10]: t = torch.rand(7)

In [11]: t_split = torch.split(t, split_size_or_sections=[4, 3])

In [13]: [item.numpy() for item in t_split]
Out[13]: 
[array([0.8252777 , 0.02960074, 0.1536448 , 0.7550541 ], dtype=float32),
 array([0.29480588, 0.89575726, 0.26591474], dtype=float32)]

合并

两个tensor 沿着某轴的合并,

In [19]: A  = torch.zeros(3, 3)

In [20]: B = torch.ones(3, 3)

In [21]: C = torch.cat([A, B], axis=0)

In [22]: print(C)
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])

In [23]: C = torch.cat([A, B], axis=1)

In [24]: print(C)
tensor([[0., 0., 0., 1., 1., 1.],
        [0., 0., 0., 1., 1., 1.],
        [0., 0., 0., 1., 1., 1.]])

参考自:  Machine Learning with PyTorch and Scikit-Learn Book

猜你喜欢

转载自blog.csdn.net/bo17244504/article/details/124771412
今日推荐