赞
踩
安装pytorch
pip install torch torchvision torchaudio
# 长度为12的向量
x = torch.arange(12)
print(x)
# tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
# 查看元素总数
x = torch.arange(12)
print(x.numel()) # number of element
# 12
# 改变张量形状
# .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]])
# 查看张量形状
x = torch.arange(12).reshape(3, 4)
print(x.shape)
# torch.Size([3, 4])
# 全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.]]])
# 全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.]]])
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]])
# 张量元素运算
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.])
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.]])
# 张量元素比较
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]])
# 张量元素求和
# 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.]])
# 张量元素求均值
# 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]])
# 广播机制
# 维度不同的张量操作时会自动扩充成相同维度
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.]])
x = torch.tensor([[3.0, 4.0], [2.0, 6.0]])
e21 = x[1, 0] # 第二行第一列
print(e21)
# tensor(2.)
x = torch.tensor([[3.0, 4.0], [2.0, 6.0]])
x[0, :] = 12.0 # 第一行的值全部设为12
print(x)
# tensor([[12., 12.],
# [ 2., 6.]])
# 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
# tensor 转 numpy
x = torch.tensor([[2.0, 1.0]])
y = x.numpy()
print(type(x), type(y))
# <class 'torch.Tensor'> <class 'numpy.ndarray'>
# numpy 转 tensor
x = numpy.array([[2.0, 1.0]])
y = torch.tensor(x)
print(type(x), type(y))
# <class 'numpy.ndarray'> <class 'torch.Tensor'>
# tensor大小为1的张量转标量
x = numpy.array([2.0])
y = x.item()
print(y)
# 2.0
# 矩阵转置
x = torch.tensor([[3.0, 4.0], [2.0, 6.0]])
print(x.T)
# tensor([[3., 2.],
# [4., 6.]])
# 判定矩阵是否是对称矩阵
x = torch.tensor([[3.0, 2.0], [2.0, 6.0]])
# x等于x的转置,那么就是对称矩阵
print(x == x.T)
# tensor([[True, True],
# [True, True]])
# 向量点积
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)
# 矩阵向量积
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.])
# 矩阵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.]])
# 任意维度张量乘法
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.]]])
# 求L1范数
# L1范数 = 向量元素的绝对值之和
x = torch.tensor([3.0, 4.0, 2.0, 6.0])
x_l2 = abs(x).sum()
print(x_l2)
# tensor(15.)
# 求L2范数【向量长度】
# L2范数 = 向量元素平方和的平方根
x = torch.tensor([3.0, 4.0, 2.0, 6.0])
x_l1 = torch.norm(x)
print(x_l1)
# tensor(8.0623)
# 求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)
# 将图片的通道维提前,用于满足网络训练时的特殊需求
img = torch.randn(size=(224, 224, 3))
img = img.permute(2, 0, 1)
print(img.shape)
# torch.Size([3, 224, 224])
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。