A:
A_sum_axis0 = A.sum(axis=0)
A_sum_axis0, A_sum_axis0.shapeout:
(tensor([[20., 22., 24., 26.], [28., 30., 32., 34.], [36., 38., 40., 42.], [44., 46., 48., 50.], [52., 54., 56., 58.]]), torch.Size([5, 4]))
A_sum_axis0 = A.sum(axis=1)
A_sum_axis0, A_sum_axis0.shapeout:
(tensor([[ 40., 45., 50., 55.], [140., 145., 150., 155.]]), torch.Size([2, 4]))
A_sum_axis0 = A.sum(axis=2)
A_sum_axis0, A_sum_axis0.shapeout:
(tensor([[ 6., 22., 38., 54., 70.], [ 86., 102., 118., 134., 150.]]), torch.Size([2, 5]))
A.sum(axis=[0, 1]) # SameasA.sum()
out:
tensor([180., 190., 200., 210.])
sum_A = A.sum(axis=1, keepdims=True) #保持维度方便使用广播
sum_A
torch.mv:矩阵-向量积,不支持广播
torch.mm:矩阵乘法
样例:
好用的公式: