0

I am facing with a problem!

How does np.einsum act on these tensors?

a = np.random.rand(2, 2, 5, 5)
b = np.random.rand(4, 5, 5, 1)
c = np.einsum('aijb,qwei->qweaj', b, a)

the output shape is: (2, 2, 5, 4, 5)

a = np.random.rand(2, 2, 5, 4, 5)
b = np.random.rand(1, 2, 2, 4)
c = np.einsum('fqnd,qlkjd->nlkj', b, a)

for this output is: (2, 5, 2, 5)

I don't know what operation(s) is performed?

Michael Szczesny
  • 4,911
  • 5
  • 15
  • 32
Kak Milad
  • 1
  • 2
  • [Understanding NumPy's einsum](https://stackoverflow.com/questions/26089893/understanding-numpys-einsum) explains 'bhwi,bhwj->bij' but about 'aijb,qwei->qweaj' or 'fqnd,qlkjd->nlkj' no!! – Kak Milad Jun 02 '22 at 11:51
  • Your examples are combinations of the three operations of `np.einsum` that can be expressed in the signature (each explained with examples in the link). There's nothing special about them. Except for a really confusing choice of letters for the axes – Michael Szczesny Jun 02 '22 at 12:00
  • This is because the axis can be swapped. Consider also reading [the documentation](https://numpy.org/doc/stable/reference/generated/numpy.einsum.html). – Jérôme Richard Jun 02 '22 at 12:00
  • I cannot understand how they operate? I mean how to implement my examples using some for-loops? What do you mean they are combinations of the three ops? which ops? – Kak Milad Jun 02 '22 at 12:17
  • The first example as a [for-loop](https://colab.research.google.com/drive/1QjkttfQZ5y4Rc62jFwZoQ9fL9qzDzZzV?usp=sharing). I recommend not to think in terms of for-loops when using `np.einsum`, but in the three operations multiply, sum and transpose. – Michael Szczesny Jun 02 '22 at 13:00
  • `'aijb,qwei->qweaj'` - `b` only appears once, so it just does a `sum` of that array on that axis. `i` appears on both arguments, but not the output, so it's the `dot` sum-of-products dimension. The rest carry over to the output in the order specified. Except for the `b` sum, this could be reduced to one `dot` (with some reshaping and transposing to make a `Xi,iY->XY` pairing). – hpaulj Jun 02 '22 at 14:56
  • `'fqnd,qlkjd->nlkj'` - again `f` is the sum-on-one array; the `dot` sum-of-products involves 2 dimensions, `q` and `d`. In effect `(f) (n(qd),(qd)(lkj)->n(lkj)`, using () to group dimensions that are used together. – hpaulj Jun 02 '22 at 15:01

0 Answers0