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
1
vote
2 answers

Generating np.einsum evaluation graph

I was planning to teach np.einsum to colleagues, by hoping to show how it would be reduced to multiplications and summations. So, instead of numerical data, I thought to use alphabet chars. in the arrays. Say, we have A (2X2) as [['a', 'b'], ['c',…
Nipun Batra
  • 11,007
  • 11
  • 52
  • 77
1
vote
1 answer

numpy.einsum 'ij,kl->ik' how to do this by numpy.tensordot

I have two matrix , 5x4 and 3x2. I want to get a 5x3 matrix from them. >>>theta_ic = np.random.randint(5,size=(5,4)) >>>psi_tr = np.random.randint(5,size=(3,2)) I can do this by >>>np.einsum('ij,kl->ik',theta_ic,psi_tr).shape (5,3) But I don't…
Li Jeff
  • 43
  • 3
1
vote
1 answer

Replace sequential product and sum with a faster matrix operation in 3D

In my current theano script the bottleneck is the following code: import numpy as np axis = 0 prob = np.random.random( ( 1, 1000, 50 ) ) cases = np.random.random( ( 1000, 1000, 50 ) ) start = time.time( ) for i in xrange( 1000 ): result = (…
1
vote
1 answer

What is the efficient way of multiplying chain of tensors in tensorflow

I have 3 sparse tensors of dimensions A = P*N, B = Q*N and C = R*N. What is the efficient way to compute the product matrix A*B*C such that dimension of the product matrix is P*Q*R in tensorflow.? I have tried with tf.matmul and followed by…
1
vote
0 answers

Tensor multiplication in tensorflow (with indetermined number of axes)

I have a tensor a with an unknown number of axes (but at least one) and a square matrix M such that a.get_shape()[0] == M.get_shape()[0]==M.get_shape()[1]. What I would like to do is R = tf.einsum("i...,ij->j...",a,M) but unlike the numpy einsum,…
patapouf_ai
  • 17,605
  • 13
  • 92
  • 132
1
vote
1 answer

Mean tensor product

I have another question which is related to my last problem( Python tensor product). There I found a mistake in my calculation. With np.tensordot I am calculating the following equation: <..> should display the average. In python code it does look…
HighwayJohn
  • 881
  • 1
  • 9
  • 22
1
vote
1 answer

numpy einsum: nested dot products

I have two n-by-k-by-3 arrays a and b, e.g., import numpy as np a = np.array([ [ [1, 2, 3], [3, 4, 5] ], [ [4, 2, 4], [1, 4, 5] ] ]) b = np.array([ [ [3, 1, 5], [0, 2, 3] …
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
1
vote
1 answer

Multiplying tensors containing images in numpy

I have the following 3rd order tensors. Both tensors matrices the first tensor containing 100 10x9 matrices and the second containing 100 3x10 matrices (which I have just filled with ones for this example). My aim is to multiply the matrices as the…
0
votes
1 answer

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), [0, 1, 2, 8, 9],[3, 4, 8, 9]) the result is [2, 2, 2, 2]…
Luis ALberto
  • 103
  • 3
0
votes
0 answers

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 array t_list=[0,....,T] with Nmax+1 elements. At each…
J.Agusti
  • 161
  • 11
0
votes
1 answer

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 reordering or vectorization that I am missing. The main…
Indiano
  • 684
  • 5
  • 19
0
votes
1 answer

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 when k != l My idea is to use numpy.einsum to…
Solarflare0
  • 269
  • 2
  • 5
0
votes
2 answers

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) I've tried all permutations (24x24) to reconstruct…
Luis ALberto
  • 103
  • 3
0
votes
0 answers

rewrite einsum("bhwHW,bHWc->bhwc") without einstein notation

The example code of DDPM works well in python. But after I convert it into tensorflowjs model, and run it in web browser, this line failed with error. tf.einsum("bhwHW,bHWc->bhwc", attn_score, v) Error: Found duplicate axes in input component…
Mr.Wang from Next Door
  • 13,670
  • 12
  • 64
  • 97
0
votes
1 answer

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.5781], # [0.8136, 0.5971], # …
Celso França
  • 653
  • 8
  • 31