栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Pytorch张量操作详解

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Pytorch张量操作详解

文章目录
  • 一、前置条件
  • 二、操作
    • 1.指定长度的顺序向量
    • 2.查看元素总数
    • 3.改变张量形状
    • 4.查看张量形状
    • 5.全0张量
    • 6.全1张量
    • 7.声明时指定张量的每一个值
    • 8.张量元素运算
    • 9.张量拼接
    • 10.张量元素比较
    • 11.张量元素求和
    • 12.张量元素求均值
    • 13.广播机制
    • 14.取出张量中的元素
    • 15.张量中的元素赋值
    • 16.深拷贝与浅拷贝
    • 17.tensor 转 numpy
    • 18.numpy 转 tensor
    • 19.tensor大小为1的张量转标量
    • 20.矩阵转置
    • 21.判定矩阵是否是对称矩阵
    • 22.向量点积
    • 23.矩阵向量积
    • 24.矩阵X矩阵
    • 25.任意维度张量乘法
    • 26.求L1范数
    • 27.求L2范数【向量长度】
    • 28.求F范数【矩阵佛罗贝尼乌斯范数】


一、前置条件

安装pytorch

pip install torch torchvision torchaudio

二、操作 1.指定长度的顺序向量
# 长度为12的向量
x = torch.arange(12)
print(x)
# tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
2.查看元素总数
# 查看元素总数
x = torch.arange(12)
print(x.numel())   # number of element
# 12
3.改变张量形状
# 改变张量形状
# .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.查看张量形状
# 查看张量形状
x = torch.arange(12).reshape(3, 4)
print(x.shape)
# torch.Size([3, 4])
5.全0张量
# 全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张量
# 全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.声明时指定张量的每一个值
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.张量拼接
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.张量元素求和
# 张量元素求和
# 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.张量元素求均值
# 张量元素求均值
# 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.深拷贝与浅拷贝
# 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
# tensor 转 numpy
x = torch.tensor([[2.0, 1.0]])
y = x.numpy()
print(type(x), type(y))
#  
18.numpy 转 tensor
# numpy 转 tensor
x = numpy.array([[2.0, 1.0]])
y = torch.tensor(x)
print(type(x), type(y))
#  
19.tensor大小为1的张量转标量
# tensor大小为1的张量转标量
x = numpy.array([2.0])
y = x.item()
print(y)
# 2.0
20.矩阵转置
# 矩阵转置
x = torch.tensor([[3.0, 4.0], [2.0, 6.0]])
print(x.T)
# tensor([[3., 2.],
#         [4., 6.]])
21.判定矩阵是否是对称矩阵
# 判定矩阵是否是对称矩阵
x = torch.tensor([[3.0, 2.0], [2.0, 6.0]])
# x等于x的转置,那么就是对称矩阵
print(x == x.T)
# tensor([[True, True],
#         [True, True]])
22.向量点积
# 向量点积
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.矩阵向量积
# 矩阵向量积
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矩阵
# 矩阵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.任意维度张量乘法
# 任意维度张量乘法
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范数
# 求L1范数
# L1范数 = 向量元素的绝对值之和
x = torch.tensor([3.0, 4.0, 2.0, 6.0])
x_l2 = torch.abs(x).sum()
print(x_l2)
# tensor(15.)
27.求L2范数【向量长度】
# 求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范数【矩阵佛罗贝尼乌斯范数】
# 求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)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1011686.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号