I wasn't sure if I should post this on stackoverflow rather than here, but because I have to construct a specific tensor I think here is more suitable.
I have 2 tensors, $x \in \mathbb{R}^{M \times N \times L}$, and $d\in \mathbb{R}^{M \times L}$ my goal is to construct a new tensor defined as $(\cdot,\cdot) : \mathbb{R}^{M \times N \times L} \times \mathbb{R}^{M \times L} \to \mathbb{R}^{M\times N}$
defined as
$$ (x,d)_{ij} = \sum_{1 \leq l \leq L} x_{ijl}d_{il} $$
the closest thing I've found is the pytorch function tensordot
However given the following example
x = torch.tensor([[[ 1, 2, 3],[ 4, 5, 6],[ 7, 8, 9]],[[ 2, 4, 6],[ 8, 10, 12],[14, 16, 18]]])
d = torch.tensor([[1, 1, 1],[2, 2, 2]])
z = torch.tensordot(x,d,([2],[1]))
gives me the tensor
z =
tensor([[[ 6, 12],
[15, 30],
[24, 48]],
[[12, 24],
[30, 60],
[48, 96]]])
Which corresponds to the formula
$$ z_{ijk} = \sum_{1 \leq l \leq L} x_{ijl}d_{kl} $$
to get the result I want I just have to take
$$ (x,d)_{ij} = z_{iji} $$
But I struggle to find an API that does this in pytorch, but I am sure there is one, I just don't know what to search for.
I've tried "trace" but I find only the trace of a square matrix. Does therefore anybody know if this operation has a name or how I can implement it using pytorch primitives without maybe resorting to for loops?