目录
一、简介
1.哑标
2.自由标
二、torch实现
1.计算迹
2.取矩阵对角线
3.计算外积
4.batch矩阵乘法
5.带有子列表和省略号
6.变换维度
7.双线性变换,类似于torch.nn.functional.bilinear
1.计算迹
没有显式的输出就是求和在输出。
torch.einsum('ii', torch.randn(4, 4))
# tensor(-1.2104)
2.取矩阵对角线
torch.einsum('ii->i', torch.randn(4, 4))
# tensor([-0.1034, 0.7952, -0.2433, 0.4545])
3.计算外积
x = torch.randn(5)
y = torch.randn(4)
torch.einsum('i,j->ij', x, y)
# tensor([[ 0.1156, -0.2897, -0.3918, 0.4963],
# [-0.3744, 0.9381, 1.2685, -1.6070],
# [ 0.7208, -1.8058, -2.4419, 3.0936],
# [ 0.1713, -0.4291, -0.5802, 0.7350],
# [ 0.5704, -1.4290, -1.9323, 2.4480]])
4.batch矩阵乘法
一行代码,将转置和乘法放在一起,确实很方便。
As = torch.randn(3,2,5)
Bs = torch.randn(3,5,4)
torch.einsum('bij,bjk->bik', As, Bs)
# tensor([[[-1.0564, -1.5904, 3.2023, 3.1271],
# [-1.6706, -0.8097, -0.8025, -2.1183]],
#
# [[ 4.2239, 0.3107, -0.5756, -0.2354],
# [-1.4558, -0.3460, 1.5087, -0.8530]],
#
# [[ 2.8153, 1.8787, -4.3839, -1.2112],
# [ 0.3728, -2.1131, 0.0921, 0.8305]]])
5.带有子列表和省略号
As = torch.randn(3,2,5)
Bs = torch.randn(3,5,4)
torch.einsum(As, [..., 0, 1], Bs, [..., 1, 2], [..., 0, 2])
# tensor([[[-1.0564, -1.5904, 3.2023, 3.1271],
# [-1.6706, -0.8097, -0.8025, -2.1183]],
#
# [[ 4.2239, 0.3107, -0.5756, -0.2354],
# [-1.4558, -0.3460, 1.5087, -0.8530]],
#
# [[ 2.8153, 1.8787, -4.3839, -1.2112],
# [ 0.3728, -2.1131, 0.0921, 0.8305]]])
6.变换维度
A = torch.randn(2, 3, 4, 5)
torch.einsum('...ij->...ji', A).shape
# torch.Size([2, 3, 5, 4])
7.双线性变换,类似于torch.nn.functional.bilinear
l = torch.randn(2,5)
A = torch.randn(3,5,4)
r = torch.randn(2,4)
torch.einsum('bn,anm,bm->ba', l, A, r)
# tensor([[-0.3430, -5.2405, 0.4494],
# [ 0.3311, 5.5201, -3.0356]])