Skip to main content
Filter by
Sorted by
Tagged with
1 vote
1 answer
81 views

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....
jnava1612's user avatar
3 votes
2 answers
146 views

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): ...
Uroc327's user avatar
  • 1,471
1 vote
1 answer
79 views

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(&...
13byte's user avatar
  • 13
-1 votes
1 answer
100 views

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, :, :] ...
VaNa's user avatar
  • 397
0 votes
1 answer
99 views

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)....
Danny Duberstein's user avatar
2 votes
2 answers
79 views

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 ...
Xin Jin's user avatar
  • 23
1 vote
1 answer
167 views

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 ...
Gummy bears's user avatar
1 vote
1 answer
63 views

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....
FatPanda01's user avatar
0 votes
1 answer
132 views

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....
John 's user avatar
  • 29
0 votes
1 answer
110 views

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=...
Aleph's user avatar
  • 207
1 vote
0 answers
157 views

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 ...
DPurple's user avatar
  • 31
0 votes
1 answer
197 views

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 ...
The User's user avatar
0 votes
1 answer
279 views

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 ...
clueless's user avatar
  • 261
1 vote
1 answer
200 views

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 ...
unbelievable's user avatar
2 votes
1 answer
264 views

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,...
ir0098's user avatar
  • 149
1 vote
2 answers
114 views

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 ...
dohmatob's user avatar
  • 318
4 votes
1 answer
108 views

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 ...
RKLS's user avatar
  • 41
1 vote
0 answers
65 views

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....
kampfkoloss's user avatar
0 votes
2 answers
87 views

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 ...
Devarapalli Vamsi's user avatar
0 votes
1 answer
34 views

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), ...
Luis ALberto's user avatar
1 vote
1 answer
136 views

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 ...
Ali Khalili's user avatar
1 vote
3 answers
720 views

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 ...
SimoneGasperini's user avatar
6 votes
3 answers
834 views

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. ...
Rushi's user avatar
  • 165
1 vote
1 answer
202 views

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) ...
Wolpertinger's user avatar
  • 1,321
2 votes
1 answer
113 views

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,...
Accelerator's user avatar
1 vote
1 answer
119 views

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 ...
Filippo Bentivoglio's user avatar
0 votes
0 answers
250 views

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 ...
J.Agusti's user avatar
  • 173
1 vote
1 answer
220 views

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, ...
Solarflare0's user avatar
0 votes
1 answer
836 views

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 ...
Indiano's user avatar
  • 712
0 votes
1 answer
294 views

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] = ...
Solarflare0's user avatar
0 votes
2 answers
167 views

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)...
Luis ALberto's user avatar
1 vote
1 answer
94 views

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], ...
Olivér Palotás's user avatar
3 votes
1 answer
535 views

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 ...
Josh.F's user avatar
  • 3,806
5 votes
2 answers
1k views

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 ...
Niteya Shah's user avatar
  • 1,824
1 vote
1 answer
418 views

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,...
Celso França's user avatar
0 votes
1 answer
160 views

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) ...
SnakeWasTheNameTheyGaveMe's user avatar
1 vote
0 answers
78 views

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,...
cgreen's user avatar
  • 361
-1 votes
1 answer
97 views

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. ...
mike talker's user avatar
0 votes
0 answers
232 views

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=...
Luis ALberto's user avatar
2 votes
2 answers
398 views

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....
Faydey's user avatar
  • 737
1 vote
0 answers
811 views

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))...
Victor Roos's user avatar
0 votes
2 answers
200 views

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 = ...
Linus's user avatar
  • 450
1 vote
1 answer
1k views

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: ...
MScottWaller's user avatar
  • 3,603
1 vote
1 answer
66 views

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('...
freak11's user avatar
  • 391
0 votes
1 answer
200 views

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 ...
Will's user avatar
  • 369
0 votes
1 answer
152 views

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]], ...
Sengiley's user avatar
  • 289
1 vote
1 answer
136 views

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 ...
Miko Miko's user avatar
-1 votes
1 answer
857 views

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)...
1_million_bugs's user avatar
3 votes
1 answer
1k views

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 ...
RocketSocks22's user avatar
1 vote
0 answers
69 views

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 ...
dragonwing11's user avatar

1
2 3 4 5 6