Questions tagged [numpy-einsum]

NumPy's `einsum` function implements the Einstein summation convention for multidimensional array objects. Use this tag for questions about how `einsum` can be applied to a particular problem in NumPy, or more questions about how the function works.

NumPy's einsum function implements the Einstein summation convention for multidimensional array objects. This allows many operations involving the multiplication or summation of values along particular axes to be expressed succinctly.

249 questions
349
votes
8 answers

Understanding NumPy's einsum

How does np.einsum work? Given arrays A and B, their matrix multiplication followed by transpose is computed using (A @ B).T, or equivalently, using: np.einsum("ij, jk -> ki", A, B)
Lance Strait
  • 4,001
  • 4
  • 17
  • 18
33
votes
1 answer

Understanding PyTorch einsum

I'm familiar with how einsum works in NumPy. A similar functionality is also offered by PyTorch: torch.einsum(). What are the similarities and differences, either in terms of functionality or performance? The information available at PyTorch…
kmario23
  • 57,311
  • 13
  • 161
  • 150
24
votes
1 answer

tensorflow einsum vs. matmul vs. tensordot

In tensorflow, the functions tf.einsum, tf.matmul, and tf.tensordot can all be used for the same tasks. (I realize that tf.einsum and tf.tensordot have more general definitions; I also realize that tf.matmul has batch functionality.) In a situation…
John Kleve
  • 499
  • 1
  • 4
  • 12
11
votes
3 answers

Can I use more than 26 letters in `numpy.einsum`?

I am using np.einsum to multiply probability tables like: np.einsum('ijk,jklm->ijklm', A, B) The issue is that I am dealing with more than 26 random variables (axes) overall, so if I assign each random variable a letter I run out of letters. Is…
akxlr
  • 1,142
  • 9
  • 23
9
votes
2 answers

Lazy evaluations of numpy.einsum to avoid storing intermediate large dimensional arrays in memory

Imagine that I have integers, n,q and vectors/arrays with these dimensions: import numpy as np n = 100 q = 102 A = np.random.normal(size=(n,n)) B = np.random.normal(size=(q, )) C = np.einsum("i, jk -> ijk", B, A) D = np.einsum('ijk, ikj -> k', C,…
8
votes
4 answers

removing loops with numpy.einsum

I have a some nested loops (three total) where I'm trying to use numpy.einsum to speed up the calculations, but I'm struggling to get the notation correct. I managed to get rid of one loop, but I can't figure out the other two. Here's what I've got…
tomerg
  • 353
  • 2
  • 12
8
votes
2 answers

How to use numpy einsum_path result?

I'm performing a decently complex operation on some 3- and 4-dimensional tensor using numpy einsum. My actual code is np.einsum('oij,imj,mjkn,lnk,plk->op',phi,B,Suu,B,phi) This does what I want it to do. Using einsum_path, the result is: >>> path =…
Luca
  • 1,610
  • 1
  • 19
  • 30
7
votes
1 answer

numpy.einsum for Julia? (2)

Coming from this question, I wonder if a more generalized einsum was possible. Let us assume, I had the problem using PyCall @pyimport numpy as np a = rand(10,10,10) b = rand(10,10) c = rand(10,10,10) Q = np.einsum("imk,ml,lkj->ij", a,b,c) Or…
varantir
  • 6,624
  • 6
  • 36
  • 57
7
votes
1 answer

numpy einsum to get axes permutation

What I understood in the documentation of ‘np.einsum‘ is that a permutation string, would give a permutation of the axis in a vector. This is confirmed by the following experiment: >>> M = np.arange(24).reshape(2,3,4) >>> M.shape (2, 3, 4) >>>…
Emanuele Paolini
  • 9,912
  • 3
  • 38
  • 64
6
votes
1 answer

Making a np.einsum faster when inputs are many identical arrays? (Or any other faster method)

I have a piece of code of type: nnt = np.real(np.einsum('xa,xb,yc,yd,abcde->exy',evec,evec,evec,evec,quartic)) where evec is (say) an L x L np.float32 array, and quartic is a L x L x L x L x T np.complex64 array. I found that this routine is rather…
semmo
  • 63
  • 4
6
votes
3 answers

Matrix Multiplication: Multiply each row of matrix by another 2D matrix in Python

I am trying to remove the loop from this matrix multiplication (and learn more about optimizing code in general), and I think I need some form of np.broadcasting or np.einsum, but after reading up on them, I'm still not sure how to use them for my…
LED
  • 129
  • 3
  • 6
6
votes
1 answer

Numpy einsum broadcasting

Can someone please explain how broadcasting (ellipsis) works in the numpy.einsum() function? Some examples to show how and when it can be used would be greatly appreciated. I've checked the following official documentation page but there are only 2…
Allen Qin
  • 19,507
  • 8
  • 51
  • 67
6
votes
1 answer

How do I calculate all pairs of vector differences in numpy?

I know I can do np.subtract.outer(x, x). If x has shape (n,), then I end up with an array with shape (n, n). However, I have an x with shape (n, 3). I want to output something with shape (n, n, 3). How do I do this? Maybe np.einsum?
Neil G
  • 32,138
  • 39
  • 156
  • 257
6
votes
1 answer

Numpy einsum() for rotation of meshgrid

I have a set of 3d coordinates that was generated using meshgrid(). I want to be able to rotate these about the 3 axes. I tried unraveling the meshgrid and doing a rotation on each point but the meshgrid is large and I run out of memory. This…
Chris
  • 431
  • 5
  • 16
5
votes
2 answers

Numpy matmul and einsum 6 to 7 times slower than MATLAB

I am trying to port some code from MATLAB to Python and I am getting much slower performance from Python. I am not very good at Python coding, so any advise to speed these up will be much appreciated. I tried an einsum one-liner (takes 7.5 seconds…
Rushi
  • 155
  • 4
1
2 3
16 17