torch.einsum(‘i,j->ij’, x, y)
Example
- 初始化两个Tensor,计算torch.einsum
x = torch.randn(5)
y = torch.randn(4)
z = torch.einsum('i,j->ij', x, y)
- x, y, z的值
x = tensor([ 0.3231, -1.5957, -1.0785, -3.3953, 0.8070])
y = tensor([ 0.3149, 0.1090, 0.1193, -0.4916])
z = tensor([[ 0.1018, 0.0352, 0.0385, -0.1589],
[-0.5025, -0.1739, -0.1903, 0.7845],
[-0.3396, -0.1176, -0.1286, 0.5302],
[-1.0692, -0.3701, -0.4050, 1.6692],
[ 0.2541, 0.0880, 0.0963, -0.3968]])
- x, y, z的shape
x.shape = torch.Size([5])
y.shape = torch.Size([4])
z.shape = torch.Size([5, 4])
理解
实现outer product功能
x
=
t
e
n
s
o
r
(
[
x
0
,
x
1
,
x
2
,
x
3
,
x
4
]
)
y
=
t
e
n
s
o
r
(
[
y
0
,
y
1
,
y
2
,
y
3
]
)
x = tensor([x_0, x_1, x_2, x_3, x_4]) \\ y = tensor([y_0, y_1, y_2, y_3])\\
x=tensor([x0,x1,x2,x3,x4])y=tensor([y0,y1,y2,y3])
z = [ x 0 y 0 x 0 y 1 x 0 y 2 x 0 y 2 x 0 y 3 x 1 y 0 x 1 y 1 x 1 y 2 x 1 y 2 x 1 y 3 x 2 y 0 x 2 y 1 x 2 y 2 x 2 y 2 x 2 y 3 x 3 y 0 x 3 y 1 x 3 y 2 x 3 y 2 x 3 y 3 x 4 y 0 x 4 y 1 x 4 y 2 x 4 y 2 x 4 y 3 ] z = \begin{bmatrix} x_0y_0 & x_0y_1 & x_0y_2 & x_0y_2 & x_0y_3 \\ x_1y_0 & x_1y_1 & x_1y_2 & x_1y_2 & x_1y_3 \\ x_2y_0 & x_2y_1 & x_2y_2 & x_2y_2 & x_2y_3 \\ x_3y_0 & x_3y_1 & x_3y_2 & x_3y_2 & x_3y_3 \\ x_4y_0 & x_4y_1 & x_4y_2 & x_4y_2 & x_4y_3 \\ \end{bmatrix} z= x0y0x1y0x2y0x3y0x4y0x0y1x1y1x2y1x3y1x4y1x0y2x1y2x2y2x3y2x4y2x0y2x1y2x2y2x3y2x4y2x0y3x1y3x2y3x3y3x4y3