Pytorch张量操作详解

文章目录

  • 一、前置条件
  • 二、操作
    • 1.指定长度的顺序向量【arange】
    • 2.查看元素总数【numel】
    • 3.改变张量形状【reshape】
    • 4.查看张量形状【shape】
    • 5.全0张量【zeros】
    • 6.全1张量【ones】
    • 7.声明时指定张量的每一个值【tensor】
    • 8.张量元素运算【+ - \* / \*\*】
    • 9.张量拼接【cat】
    • 10.张量元素比较【==】
    • 11.张量元素求和【sum】
    • 12.张量元素求均值【mean】
    • 13.广播机制
    • 14.取出张量中的元素
    • 15.张量中的元素赋值
    • 16.深拷贝与浅拷贝【clone】
    • 17.tensor 转 numpy【numpy】
    • 18.numpy 转 tensor【tensor】
    • 19.tensor大小为1的张量转标量【item】
    • 20.矩阵转置【T】
    • 21.判定矩阵是否是对称矩阵【x == x.T】
    • 22.向量点积【dot】
    • 23.矩阵向量积【mv】
    • 24.矩阵X矩阵【mm】
    • 25.任意维度张量乘法【matmul】
    • 26.求L1范数【abs(x).sum()】
    • 27.求L2范数【向量长度】【torch.norm(x)】
    • 28.求F范数【矩阵佛罗贝尼乌斯范数】【torch.norm(x)】
    • 29.张量维度交换,用于图片通道维度的交换【permute】


一、前置条件

安装pytorch

pip install torch torchvision torchaudio

二、操作

1.指定长度的顺序向量【arange】

# 长度为12的向量
x = torch.arange(12)
print(x)
# tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])

2.查看元素总数【numel】

# 查看元素总数
x = torch.arange(12)
print(x.numel())   # number of element
# 12

3.改变张量形状【reshape】

# 改变张量形状
# .reshape()这个方法是浅拷贝!!切记
x = torch.arange(12).reshape(3, 4)    # 12维向量改为3行4列矩阵
print(x)
# tensor([[ 0,  1,  2,  3],
#         [ 4,  5,  6,  7],
#         [ 8,  9, 10, 11]])

4.查看张量形状【shape】

# 查看张量形状
x = torch.arange(12).reshape(3, 4)
print(x.shape)
# torch.Size([3, 4])

5.全0张量【zeros】

# 全0张量
x = torch.zeros((2, 3, 4))
print(x)
# 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.]]])

6.全1张量【ones】

# 全1张量
x = torch.ones((2, 3, 4))
print(x)
# tensor([[[1., 1., 1., 1.],
#          [1., 1., 1., 1.],
#          [1., 1., 1., 1.]],
#
#         [[1., 1., 1., 1.],
#          [1., 1., 1., 1.],
#          [1., 1., 1., 1.]]])

7.声明时指定张量的每一个值【tensor】

x = torch.tensor([[2.0, 1.0, 6.5], [6.1, 6.0, 4.5]])
print(x)
# tensor([[2.0000, 1.0000, 6.5000],
#         [6.1000, 6.0000, 4.5000]])

8.张量元素运算【+ - * / **】

# 张量元素运算
x = torch.tensor([3.0, 4.0, 5.0, 6.0])
y = torch.tensor([2.0, 2.0, 2.0, 2.0])
# 加
z = x + y
print(z)
# tensor([5., 6., 7., 8.])
# 减
z = x - y
print(z)
# tensor([1., 2., 3., 4.])
# 乘
z = x * y
print(z)
# tensor([ 6.,  8., 10., 12.])
# 除
z = x / y
print(z)
# tensor([1.5000, 2.0000, 2.5000, 3.0000])
# 幂
z = x ** y
print(z)
# tensor([ 9., 16., 25., 36.])

9.张量拼接【cat】

x = torch.tensor([[3.0, 4.0], [5.0, 6.0]])
y = torch.tensor([[2.0, 2.0], [2.0, 2.0]])
# 纵向拼接【特征堆叠】
z0 = torch.cat((x, y), dim=0)
print(z0)
# tensor([[3., 4.],
#         [5., 6.],
#         [2., 2.],
#         [2., 2.]])
# 横向拼接【特征拼接】
z1 = torch.cat((x, y), dim=1)
print(z1)
# tensor([[3., 4., 2., 2.],
#         [5., 6., 2., 2.]])

10.张量元素比较【==】

# 张量元素比较
x = torch.tensor([[3.0, 4.0], [2.0, 6.0]])
y = torch.tensor([[2.0, 2.0], [2.0, 2.0]])
print(x == y)
# tensor([[False, False],
#         [ True, False]])

11.张量元素求和【sum】

# 张量元素求和
# axis指定求和方法,对哪一维求和就会消除掉那一维
x = torch.tensor([[3.0, 4.0], [2.0, 6.0]])
print(x.sum())
# tensor(15.)
print(x.sum(axis=0))   # 纵向求和,消除第一个维度,由 [2, 2] 变为 [2]
# tensor([ 5., 10.])
print(x.sum(axis=1))   # 横向求和,消除第二个维度,由 [2, 2] 变为 [2]
# tensor([7., 8.])
print(x.sum(axis=0, keepdims=True))   # 纵向求和保持维度,保留第一个维度,由 [2, 2] 变为 [1, 2]
# tensor([[ 5., 10.]])
print(x.sum(axis=1, keepdims=True))   # 横向求和保持维度,保留第二个维度,由 [2, 2] 变为 [2, 1]
# tensor([[7.],
#         [8.]])

12.张量元素求均值【mean】

# 张量元素求均值
# axis指定求均值方法,对哪一维求均值就会消除掉那一维
x = torch.tensor([[3.0, 4.0], [2.0, 6.0]])
print(x.mean())
# tensor(3.7500)
print(x.mean(axis=0))   # 纵向求均值,消除第一个维度,由 [2, 2] 变为 [2]
# tensor([2.5000, 5.0000])
print(x.mean(axis=1))   # 横向求均值,消除第二个维度,由 [2, 2] 变为 [2]
# tensor([3.5000, 4.0000])
print(x.mean(axis=0, keepdims=True))   # 纵向求均值保持维度,保留第一个维度,由 [2, 2] 变为 [1, 2]
# tensor([[2.5000, 5.0000]])
print(x.mean(axis=1, keepdims=True))   # 横向求均值保持维度,保留第二个维度,由 [2, 2] 变为 [2, 1]
# tensor([[3.5000],
#         [4.0000]])

13.广播机制

# 广播机制
# 维度不同的张量操作时会自动扩充成相同维度
x = torch.tensor([[3.0], [2.0]])
y = torch.tensor([[3.0, 4.0]])
print(x)
# tensor([[3.],
#         [2.]])
print(y)
# tensor([[3., 4.]])
z = x + y   # 两行一列 + 一行两列 = 两行两列
print(z)
# tensor([[6., 7.],
#         [5., 6.]])

14.取出张量中的元素

x = torch.tensor([[3.0, 4.0], [2.0, 6.0]])
e21 = x[1, 0]       # 第二行第一列
print(e21)
# tensor(2.)

15.张量中的元素赋值

x = torch.tensor([[3.0, 4.0], [2.0, 6.0]])
x[0, :] = 12.0  # 第一行的值全部设为12
print(x)
# tensor([[12., 12.],
#         [ 2.,  6.]])

16.深拷贝与浅拷贝【clone】

# ps
# 内存使用技巧
# 尽量使用 x += y 而不是 x = x + y
# x += y 会在 原来的 x 上进行赋值,而 x = x + y 会为 x 新创建新内存空间然后赋值
x = torch.tensor([[2.0, 1.0]])
y = torch.tensor([[3.0, 4.0]])

# 浅拷贝
x += y
before = id(x)
x += y
print(id(x) == before)
# True

# 深拷贝
before = id(x)
x = x + y
print(id(x) == before)
# False

# 深拷贝的另一种方式
x = torch.tensor([[3.0, 2.0], [2.0, 6.0]])
y = x.clone()
print(id(y) == id(x))
# False

17.tensor 转 numpy【numpy】

# tensor 转 numpy
x = torch.tensor([[2.0, 1.0]])
y = x.numpy()
print(type(x), type(y))
#  

18.numpy 转 tensor【tensor】

# numpy 转 tensor
x = numpy.array([[2.0, 1.0]])
y = torch.tensor(x)
print(type(x), type(y))
#  

19.tensor大小为1的张量转标量【item】

# tensor大小为1的张量转标量
x = numpy.array([2.0])
y = x.item()
print(y)
# 2.0

20.矩阵转置【T】

# 矩阵转置
x = torch.tensor([[3.0, 4.0], [2.0, 6.0]])
print(x.T)
# tensor([[3., 2.],
#         [4., 6.]])

21.判定矩阵是否是对称矩阵【x == x.T】

# 判定矩阵是否是对称矩阵
x = torch.tensor([[3.0, 2.0], [2.0, 6.0]])
# x等于x的转置,那么就是对称矩阵
print(x == x.T)
# tensor([[True, True],
#         [True, True]])

22.向量点积【dot】

# 向量点积
x = torch.tensor([3.0, 4.0, 2.0, 6.0])
y = torch.tensor([2.0, 2.0, 2.0, 2.0])
# 第一种写法
z = torch.dot(x, y)
print(z)
# 第二种写法
z = torch.sum(x * y)
print(z)

23.矩阵向量积【mv】

# 矩阵向量积
x = torch.tensor([[3.0, 4.0], [2.0, 6.0]])      # 2x2 矩阵
y = torch.tensor([2.0, 2.0])                    # 2x1 向量
z = torch.mv(x, y)
print(z)                                        # 2x1 向量
# tensor([14., 16.])

24.矩阵X矩阵【mm】

# 矩阵X矩阵【可以理解为执行m次矩阵向量积,并将结果拼接在一起,形成 nxm 的矩阵】
x = torch.tensor([[3.0, 4.0], [2.0, 6.0], [2.0, 6.0]])      # 3x2 矩阵
y = torch.tensor([[2.0, 2.0, 6.0], [2.0, 2.0, 2.0]])        # 2x3 矩阵
z = torch.mm(x, y)
print(z)                                                    # 3x3 矩阵
# tensor([[14., 14., 26.],
#         [16., 16., 24.],
#         [16., 16., 24.]])

25.任意维度张量乘法【matmul】

# 任意维度张量乘法
x = torch.tensor([[[3.0, 4.0], [2.0, 6.0]], [[3.0, 4.0], [2.0, 6.0]]])      # 2x2x2 矩阵
y = torch.tensor([[[3.0, 4.0], [2.0, 6.0]], [[3.0, 4.0], [2.0, 6.0]]])      # 2x2x2 矩阵
z = torch.matmul(x, y)
print(z)                                                    # 2x2x2 矩阵
# tensor([[[17., 36.],
#          [18., 44.]],
#
#         [[17., 36.],
#          [18., 44.]]])

26.求L1范数【abs(x).sum()】

# 求L1范数
# L1范数 = 向量元素的绝对值之和
x = torch.tensor([3.0, 4.0, 2.0, 6.0])
x_l2 = abs(x).sum()
print(x_l2)
# tensor(15.)

27.求L2范数【向量长度】【torch.norm(x)】

# 求L2范数【向量长度】
# L2范数 = 向量元素平方和的平方根
x = torch.tensor([3.0, 4.0, 2.0, 6.0])
x_l1 = torch.norm(x)
print(x_l1)
# tensor(8.0623)

28.求F范数【矩阵佛罗贝尼乌斯范数】【torch.norm(x)】

# 求F范数【矩阵佛罗贝尼乌斯范数】
# 矩阵元素平方和的平方根
x = torch.tensor([[2.0, 2.0, 6.0], [2.0, 2.0, 2.0]])
x_f = torch.norm(x)
print(x_f)
# tensor(7.4833)

29.张量维度交换,用于图片通道维度的交换【permute】

# 将图片的通道维提前,用于满足网络训练时的特殊需求
img = torch.randn(size=(224, 224, 3))

img = img.permute(2, 0, 1)
print(img.shape)
# torch.Size([3, 224, 224])

你可能感兴趣的:(NLP,CV,Python,pytorch,python,深度学习)