265 questions
1
vote
1
answer
81
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....
3
votes
2
answers
146
views
Efficiently compute all multi-dimensional traces for all offsets and store in matrix
I have a $N\times N \times N$ array $a$ and would like to implement the formula $$ b_{ij} = \sum_k a_{i+k,j+k,k} $$ efficiently.
Right now, I'm doing this via
b = np.zeros((N, N))
for i in range(N):
...
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(&...
-1
votes
1
answer
100
views
NumPy successive matrix multiplication vectorization [duplicate]
I have a NumPy array ar of shape (M, L, 2, 2). M is usually in the hundreds or thousands, L is usually <= 5.
I want to multiply the L (N, N) matrices successively (multiplied_ar[m] = ar[m, 0, :, :] ...
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)....
2
votes
2
answers
79
views
Einsum abc,cde->abde. How to make sense of it?
I read and understand all simple cases for einsum, such as ab,bc->ac or batched ones like abc,acd->abd.
But "abc,cde->abde" is giving me a hard time. So far I figure there will be a ...
1
vote
1
answer
167
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 ...
1
vote
1
answer
63
views
Nested indexing in NumPy einsum?
I'm trying to write the following expression using the einsum function in NumPy:
for j in range(100):
p[j] = 0
for i in range(100):
if i!=j:
p[j] += S[i,j]*B[T[i,j], i]
p....
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....
0
votes
1
answer
110
views
Is there a dilated k-nearest neighbour solution available fast execution?
I am implementing the dilated k-nearest neighbors algorithm. The algorithm unfortunately has nested loops. The presence of loops severely hampers the execution speed.
import torch
dilation=3
nbd_size=...
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
197
views
Python: multiply 2 time series of matrices
I have 2 time-series of matrices that I would like to multiply together. I have the data stored as a pandas MultiIndex frame. Both frames share the same first axis, which are k dates. So, the first ...
0
votes
1
answer
279
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 ...
1
vote
1
answer
200
views
Understand the details of einsum application for two tensors
We have two tensors:
a = np.arange(8.).reshape(4,2,1)
b = np.arange(16.).reshape(2,4,2)
We are going to implement
np.einsum('ijk,jil->kl', a, b)
Although we could obtain its results, we were ...
2
votes
1
answer
264
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,...
1
vote
2
answers
114
views
Product of arrays (einsum) of 3D arrays containing only -1 or +1
Let X be an array of shape (M, k, g) and Q be an array of shape (m, k, g), where m, M, k, and g can be "very large". Suppose the entries of X and Y are either -1 or plus +1. I'm interested ...
4
votes
1
answer
108
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 ...
1
vote
0
answers
65
views
numpy einsum for y = (x*A)@A.T
np.einsum seems nice, but it is complicated (for me) to use on more difficult expressions. What would be the equivalent expression here?
import numpy as np
x = np.array([1.,2.,3.])
A = np.array([[3.,4....
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 ...
0
votes
1
answer
34
views
unroll numpy einsum to get indexs
I have a vector with shape [2, 2, 2, 2, 2] and I need to get the indexs "from" and "to" for this numpy einsum operation:
np.einsum(vector,[0, 1, 2, 3, 4], np.conj(vector),
...
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 ...
1
vote
3
answers
720
views
How can I use numpy.einsum for matrix-vector multiplication of an unknown number of operands?
I want to efficiently perform a chain of matrix-vector multiplication in Python and the numpy.einsum function seems to be the best choice. However, I do NOT know the number of matrix operands N in the ...
6
votes
3
answers
834
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.
...
1
vote
1
answer
202
views
Counting zeros in large numpy arrays without creating them
To illustrate the problem I am facing, here is some example code:
a = np.round(np.random.rand(10, 15))
counta = np.count_nonzero(a, axis=-1)
print(counta)
A = np.einsum('im,mj->ijm', a, a.T)
...
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,...
1
vote
1
answer
119
views
Vectorizing Mahalanobis distance - numpy
I have been looking at the answer from @Danita's answer (Vectorizing code to calculate (squared) Mahalanobis Distiance), which uses np.einsum to calculate the squared Mahalanobis distance. In that ...
0
votes
0
answers
250
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 ...
1
vote
1
answer
220
views
Optimizing np.einsum calls in Python
I have two numpy arrays: X of dimension (N,N,N,N) and Y of dimension (N,N). My goal is to evaluate the following einsum call as fast as possible:
Z = np.einsum('iiii,ij,ik,il,im->jklm', X, Y, Y, Y, ...
0
votes
1
answer
836
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
1
answer
294
views
How to use numpy.einsum to add redundant indices
Suppose I have an N x N x N dimensional numpy array X with entries X[i,j,k]. I want to use X to define an N x N x N x N dimensional numpy array Y defined as follows:
Y[i,j,k,k] = X[i,j,k]
Y[i,j,k,l] = ...
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)...
1
vote
1
answer
94
views
How can I flip matrix elements in triple-wise rows in numpy?
I have a matrix with the shape (3*k, 3*l) (e.g.: k=2, l=1):
A = np.arange(18).reshape(6, 3)
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14],
...
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 ...
5
votes
2
answers
1k
views
Write numpy einsum operation as eigen tensors
I want to write the following numpy einsum as a an Eigen Tensor op
import numpy as np
L = np.random.rand(2, 2, 136)
U = np.random.rand(2, 2, 136)
result = np.einsum('ijl,jkl->ikl', U, L)
I can ...
1
vote
1
answer
418
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,...
0
votes
1
answer
160
views
Mapping timeseries sequence input shape to desired output shape using EinsumDense
Can anyone help me understand how to handle compressing/expanding the dimension of a tensor using EinsumDense?
I have a timeseries (not NLP) input tensor of the shape (batch, horizon, features) ...
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,...
-1
votes
1
answer
97
views
Einsum multiply each row with every one for 3X3X3 array
Hello could someone please help me figure out how to use np.einsum to produce the below code's result. I have a (3,3,3) tensor and I will like to get this results which I got from using two for loops. ...
0
votes
0
answers
232
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=...
2
votes
2
answers
398
views
Translating np.einsum to something more performant
Using python/numpy, I have the following np.einsum:
np.einsum('abde,abc->bcde', X, Y)
Y is sparse: for each [a,b], only one c == 1; all others := 0.
For an example of relative size of the axes, X....
1
vote
0
answers
811
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))...
0
votes
2
answers
200
views
numpy.einsum with ellipses of different dimensionality
I often find that I'd like like to do an operation between the last few dimensions of two arrays, where the first dimensions don't necessarily match. As an example I'd like to do something like:
a = ...
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:
...
1
vote
1
answer
66
views
How to multiply Tensorflow arrays across specified indicies
I would like to multiply two Tensorflow Arrays in a certain way as shown in the code below:
import tensorflow as tf
from tensorflow.keras import mixed_precision
policy = mixed_precision.Policy('...
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 ...
0
votes
1
answer
152
views
numpy einsum for multiplication looped along axis
I have an 2 x 2 matrix yy
yy = np.array([[0.5, 0], [0, 2]])
print(yy)
array([[0.5, 0. ],
[0. , 2. ]])
and n=3 x 4 x 2 matrix xy
xy = np.array([
[[1, 0.1], [2, 0.2], [3, 0.3], [4, 0.4]],
...
1
vote
1
answer
136
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 ...
-1
votes
1
answer
857
views
What does np.einsum('mk,nk', D, D) do?
I'm reading over someone else's code and am unsure what np.einsum does in this case.
print(np.einsum('mk,nk', D, D)) # D is an np array with shape (3, 100)
This code outputs an array with shape (3, 3)...
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 ...
1
vote
0
answers
69
views
Can a numpy.prod array reduction be replaced by numpy.einsum?
I have an huge 8D array view that i want to reduce to 2D by multiplying the elements together over 4 axes and summing them over 2 axes. I didn´t find any example in the numpy.einsum documentation ...