线性代数-张量算法的基本性质
给定任何相同形状的任意两个张量,任何按元素二元运算的结果都将是相同形状的张量
A = torch.arange(20, dtype=torch.float32).reshape(5, 4)
B = A.clone() # 通过分配新内存,将A的一个副本分配给B
A, A + B
#输出结果
(tensor([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[12., 13., 14., 15.],
[16., 17., 18., 19.]]),
tensor([[ 0., 2., 4., 6.],
[ 8., 10., 12., 14.],
[16., 18., 20., 22.],
[24., 26., 28., 30.],
[32., 34., 36., 38.]]))
1、哈达玛积:两个矩阵按元素乘法
数学符号: ⊙
# 张量的 哈达玛积
A * B
# 输出结果:
tensor([[ 0., 1., 4., 9.],
[ 16., 25., 36., 49.],
[ 64., 81., 100., 121.],
[144., 169., 196., 225.],
[256., 289., 324., 361.]])
2、将张量乘以或加上一个标量不会改变张量的形状,其中张量的每个元素都将与标量相加或相乘。
a = 2
X = torch.arange(24).reshape(2, 3, 4)
a + X, (a * X).shape
#输出结果
(tensor([[[ 2, 3, 4, 5],
[ 6, 7, 8, 9],
[10, 11, 12, 13]],
[[14, 15, 16, 17],
[18, 19, 20, 21],
[22, 23, 24, 25]]]),
torch.Size([2, 3, 4]))
二、降维
1、调用求和函数沿着所有的轴降低张量的维度,使它变为一个标量(也就是降维)
x = torch.arange(4, dtype=torch.float32) # 可以发现x和x.sum()是开辟了两个不同的内存空间 print(id(x)) print(id(x.sum())) x, x.sum() #输出结果 1854494777472 1854494778496 (tensor([0., 1., 2., 3.]), tensor(6.))
#sum()函数可以表示任意形状张量的元素和
A.shape, A.sum()
#输出结果
(torch.Size([5, 4]), tensor(190.))
2、指定张量沿哪个轴来通过求和降低维度(按行、按列来生成输出向量)
print(A)
# axis=0:按行相加
# axis=1:按列相加
A_sum_axis0 = A.sum(axis=0)
A_sum_axis0, A_sum_axis0.shape
#输出结果
tensor([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[12., 13., 14., 15.],
[16., 17., 18., 19.]])
(tensor([40., 45., 50., 55.]), torch.Size([4]))
A_sum_axis1 = A.sum(axis=1)
A_sum_axis1, A_sum_axis1.shape
#输出结果
(tensor([ 6., 22., 38., 54., 70.]), torch.Size([5]))
3、沿着行和列矩阵求和,等价于对矩阵的所有元素进行求和
A.sum(axis=[0, 1]) # Same as `A.sum()` #输出结果 tensor(190.)
4、求平均值
可以通过将总和除以元素总数来计算平均值,也可以直接调用函数
# A.mean():求平均值 # A.sum():求所有元素和 # A.numel():求元素个数 A.mean(), A.sum() / A.numel() print(A.mean()) print(A.sum()) print(A.numel()) print(A.sum()/A.numel()==A.mean()) #输出结果 tensor(9.5000) tensor(190.) 20 tensor(True)
5、可以沿着指定轴求平均值
print(A)
print(A.sum(axis=0))
print(A.shape[0])
A.mean(axis=0), A.sum(axis=0) / A.shape[0]
#输出结果
tensor([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[12., 13., 14., 15.],
[16., 17., 18., 19.]])
tensor([40., 45., 50., 55.])
5
(tensor([ 8., 9., 10., 11.]), tensor([ 8., 9., 10., 11.]))
三、非降维求和
1、在调用函数计算总和和均值时保持轴数不变
# axis=1:按列计算(左右关系)
# axis=0:按行计算(上下关系)
# 降维求和
print(A.sum(axis=1)) #通过输出可以发现,数据是在一行内
# 通过keepdims=true,可以实现非降维求和
sum_A = A.sum(axis=1, keepdims=True)#通过输出可以发现,求和后任然保持两个轴
sum_A
#输出结果
tensor([ 6., 22., 38., 54., 70.])
tensor([[ 6.],
[22.],
[38.],
[54.],
[70.]])
#可以发现,axis是列向求和,结果没有降维
2、沿着某个轴计算元素的累计总和
调用consum函数,此函数不会沿任何轴降低输入张量的维度
print(A)
# 每个轴计算A元素的累加和
print(A.cumsum(axis=0))
print(A.cumsum(axis=1))
A.cumsum(axis=0)
#输出结果
tensor([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[12., 13., 14., 15.],
[16., 17., 18., 19.]])
tensor([[ 0., 1., 2., 3.],
[ 4., 6., 8., 10.],
[12., 15., 18., 21.],
[24., 28., 32., 36.],
[40., 45., 50., 55.]])
tensor([[ 0., 1., 3., 6.],
[ 4., 9., 15., 22.],
[ 8., 17., 27., 38.],
[12., 25., 39., 54.],
[16., 33., 51., 70.]])
tensor([[ 0., 1., 2., 3.],
[ 4., 6., 8., 10.],
[12., 15., 18., 21.],
[24., 28., 32., 36.],
[40., 45., 50., 55.]])
浙公网安备 33010602011771号