265 questions
1
vote
1
answer
80
views
Outer Product and Partial Trace of large Vectors exceeds Memory
I'm trying to find the outer product of a large complex-valued vector (of size 91204) to later on find the it's partial trace using np.einsum. However I get the following error:
numpy._core....
1
vote
1
answer
79
views
Numpy Einsum - Why did this happen?
Can you explain why this happened?
import numpy as np
a = np.array([[1,2],
[3,4],
[5,6]
])
b = np.array([[2,2,2],
[2,2,2]])
print(np.einsum(&...
0
votes
1
answer
99
views
Most efficient way to index a numpy array by the indices of another numpy array
I have a numpy array A with shape (a, b, c), and another integer array L with shape (a, b). I want to make an array B such that B[i, j, k] = A[L[i, j],j, k] (assume shapes and values of L permit this)....
1
vote
1
answer
166
views
Converting an expression into an einsum
I have the following expression that I need to calculate for some matrices:
I could of course do this using a for loop, but I'm attempting to use the torch.einsum function to calculate this in a ...
0
votes
1
answer
132
views
Optimize tensor contraction in C++ compared to Python
I have 3 vectors(numpy arrays in Python) in C++ and in Python, I wish to do the following tensor contraction:
import numpy as np
import time
N_t, N = 400, 400
a = np.random.rand(N_t, 2, 2, N)
b = np....
1
vote
0
answers
157
views
Algorithm complexity of optimal path in pytorch einsum
I want to perform the following contraction
np.einsum('ijk,kl,jlm', x, y, z, optimize = 'optimal')
Testing performance with numpy I know that for my data, the optimal path is almost allways (if this ...
0
votes
1
answer
278
views
Clarification on einsum equation
I came across some code on Huggingface (in a self-attention module) that uses torch.einsum, which I'm not too familiar with and would like some help interpreting. I've looked through this list of ...
2
votes
1
answer
263
views
replacing einsum with normal operations
I need to replace einsum operation with standard numpy operations in the following code:
import numpy as np
a = np.random.rand(128, 16, 8, 32)
b = np.random.rand(256, 8, 32)
output = np.einsum('aijb,...
4
votes
1
answer
104
views
General use of ellipsis in np.einsum
I am trying to implement a tensor network kind of calculation using np.einsum.
The goal of the calculation is to have a local 2x2 matrix act sequentially on a tensor of size (2,2,2,...,2,2,2) where ...
0
votes
2
answers
87
views
How to get dot product of each array in (n,1,3) numpy array with all arrays of (n,3) array and get the resultant array of shape (n,n)?
I tried einsum np.einsum but this is giving me error that the output cannot have same letters repeated.
np.einsum('aij,aj->aa', vector1, vector2)
Also tried np.dot method but that attempt is also ...
1
vote
1
answer
136
views
How to add another dimension to an einsum operation?
Suppose the tensor and tensor1 are some calculated transformations of an input with the shapes provided in the code snippet. The einsum operation performs Einstein's summation to aggregate the results ...
6
votes
3
answers
824
views
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.
...
2
votes
1
answer
113
views
Comparison of Looping and Einsum Operations on a List of Arrays for Speed Optimization
I want to make use of the einsum to speed up a code as following:
As simple example using a list of 2 (3,3) arrays called dcx:
That i created:
In [113]: dcx = [np.arange(9).reshape(3,3), np.arange(10,...
0
votes
0
answers
249
views
Python - How to optimize einsum?
I am trying to optimize my code and I don't know if I am already at the limit.
Here is my problem: I am solving the equation of motion for multiple trajectories. What this means is that I have an ...
0
votes
1
answer
834
views
Cupy Code Optimization: How to speed up nested for loops
I would like to optimize the python code between the 2 perf_counter functions.
By using cupy I already obtained substantial improvement compared to numpy.
I was asking myself if there is some ...
0
votes
2
answers
167
views
Is it possible to invert this numpy einsum operation?
Is it possible to invert this einsum operation so I get back the input psi4d from it's output psi1 and psi2?
psi1 = np.einsum('jqik->ij', psi4d)
psi2= np.einsum('kiqj->ij', psi4d)...
3
votes
1
answer
535
views
In PyTorch, how can I avoid an expensive broadcast when adding two tensors then immediately collapsing?
I have two 2-d tensors, which align via broadcasting, so if I add/subtract them, I incur a huge 3-d tensor. I don't really need that though, since I'll be performing a mean on one dimension. In this ...
1
vote
1
answer
417
views
How to perform the MaxSim operator leveraging torch procedures?
Let T and L be two batches of matrices (MxN) and a function f(ti,lj) that calculates a score for matrices ti and lj. For instance, if
T, L= torch.rand(4,3,2), torch.rand(4,3,2)
# T = tensor([[[0.0017,...
1
vote
0
answers
78
views
Einsum matrix multiplication with missing dimensions
I want to modify this einsum to be more flexible. Right now it's doing a matrix multiplication of the last two dimensions of A against the last 3 of B:
tf.einsum("...xp,...pyz->...xyz", A,...
0
votes
0
answers
229
views
convert python Einsum to fast C++
I have converted this python eimsum expression
psi_p = np.einsum('ij...,j...->i...', exp_p, psi_p)
to c++ like this:
int io=0;
`for (i=0; i < 4; i++){
ikauxop=i*nd;
for (j=...
1
vote
0
answers
808
views
How to get numpy einsum to ignore nan's?
Let's say you use einsum to calculate the slope and intercept in a simple regression as follows:
slope = (np.einsum('ij,ij->i', y_norm, x_norm) /
np.einsum('ij,ij->i', x_norm, x_norm))...
1
vote
1
answer
1k
views
Einsum for shapes of different sizes or ranks
I have two PyTorch tensors. One is rank three and the other is rank four. Is there a way to get it so that it produce the rank and shape of the first tensor? For instance in this cross-attention bit:
...
0
votes
1
answer
200
views
More efficient nested sum in numpy
I am trying to calculate a vectorised nested sum
(so effectively doing a separate calculation for each row k)
The fastest way I have come up with is to define a lower triangular matrix of ones to ...
1
vote
1
answer
134
views
Standard operations equivalent of einsum expression
I have the following einsum expressions:
np.einsum("abc,ab->ac",a,b)
np.einsum("abc,abd->dc", a, b)
That I would need to convert to standard numpy operations. Can anyone help ...
3
votes
1
answer
1k
views
Einsum is slow for tensor multiplication
I'm trying to optimize a particular piece of code to calculate the mahalanobis distance in a vectorized manner. I have a standard implementation which used traditional python multiplication, and ...