1

I have the following inputs:

  • T: a (H x W x C) tensor, or if needed (H x W x C x 1).
  • M: (C x C) matrix.

I need to compute a (H x W x C) tensor, in which each "slice" is the matrix product between M and the corresponding (1 x 1 x C) slice from T, like this:

result = np.zeros_like(T)
for row in range(H):
    for col in range(W):
        result[row, col, ...] = M @ T[row, col, ...]

enter image description here

Can this be done more efficiently with numpy, numpy.einsum or einops?

Milo
  • 2,062
  • 16
  • 38
  • 3
    You can try this : ```result = np.einsum("ijk,mk->ijm",T,M,optimize=True)``` –  Sep 06 '22 at 12:22
  • 2
    Or `T @ M.T`, IIUC. – Michael Szczesny Sep 06 '22 at 12:25
  • @MichaelSzczesny. Of course this works, thank you! Would you mind to elaborate a bit more in an answer (for example, is broadcasting involved?) so I can get it accepted? Maybe in hindsight the question might seem silly, maybe that's why it got downvoted, but I couldn't find an answer anywhere else and I really appreciate your help. – Milo Sep 06 '22 at 14:25

0 Answers0