简体   繁体   English

张量矩阵乘法返回向量 einsum

[英]Tensor matrix multiplication returning vector einsum

I am confused about the following example of a matrix tensor multiplication that returns a vector.我对以下返回向量的矩阵张量乘法示例感到困惑。 At first glance I thought that it would mean multiplying the first dimension of the tensor dydx by the matrix dLdy but I don't get the expected results as depicted below.乍一看,我认为这意味着将张量 dydx 的第一维乘以矩阵 dLdy,但我没有得到如下所示的预期结果。 So what is the meaning of this einsum?那么这个einsum是什么意思呢?

import torch
import numpy as np

dLdy = torch.randn(2,2)
dydx = torch.randn(2,2,2)

torch.einsum('jk,jki->i', dLdy, dydx)
tensor([0.3115, 3.7255])

dLdy
tensor([[-0.4845,  0.6838],
    [-1.1723,  1.4914]])

dydx
tensor([[[ 1.5496, -1.2722],
     [ 0.1221,  1.0495]],

    [[-1.4882,  0.0307],
     [-0.5134,  1.6276]]])

(dLdy * dydx[0]).sum()
-0.1985

For A and B this is contraction (sum) over the first two dimensions jk, so res(i) = sum_{j,k} A(j,k)B(j,k,i)对于 A 和 B,这是前两个维度 jk 上的收缩(总和),所以 res(i) = sum_{j,k} A(j,k)B(j,k,i)

for example:例如:

import torch
import numpy as np


dLdy = torch.randn(2,2)
dydx = torch.randn(2,2,2)

print(torch.einsum('jk,jki->i', dLdy, dydx))
print((dLdy * dydx[:,:,0]).sum())
print((dLdy * dydx[:,:,1]).sum())

produces生产

tensor([4.6025, 1.8987])
tensor(4.6025)
tensor(1.8987)

ie (dLdy * dydx[:,:,0]).sum() is the first element of the resulting vector, etc(dLdy * dydx[:,:,0]).sum()是结果向量的第一个元素,等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM