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
0
votes
0 answers

Is there any DGEMM-type einsum in python?

In DGEMM http://www.netlib.org/lapack/explore-html/d1/d54/group__double__blas__level3_gaeda3cbd99c8fb834a60a6412878226e1.html#gaeda3cbd99c8fb834a60a6412878226e1 one can do C = alpha * A.B + beta*C In eimsum, there is ij,jk->ik (or more general…
AlphaF20
  • 583
  • 6
  • 14
0
votes
1 answer

Python numpy computing out matrix with shape 3,3,3 from input matrecies with shape 3,3

I am currently building NeuralNetwork in python only using numpy. This is the layout of the problem area: I have one array holding the values for the input neurons in the columns and the rows represent the different training data points. It is of…
Phönix 64
  • 119
  • 1
  • 8
0
votes
2 answers

Calculate every 4-element-product in a vector with python

I have a (500000,30) numpy array and we can look it as a length-500000 list of size-30 vectors. I want to choose arbitrary 4 elements in the vector, calculate its product, and store all the 4-element-products. Finally I need to calculate the mean of…
Inm
  • 131
  • 3
0
votes
2 answers

How to calculate the outer sum (not product) between 2 (1D) vectors

It seems like np.einsum should do the trick, but I haven't been able to make it work. An example: a = np.arange(3) b = np.arange(2) #that computes the outer product res = np.einsum('i,j->ij',a,b) #The resulting array I am looking for is: out =…
Schach21
  • 412
  • 4
  • 21
0
votes
1 answer

np.einsum multiplication of matrices with several indices?

Given m x n matrix A and n x r matrix B how to write the following formula in np.einsum notation? f(i) = \sum_{j,k} a_ij * b_jk What will change in np.einsum if r x p matrix C will be added? f(i) = \sum_{j,k,l} a_ij * b_jk * c_kl
Sengiley
  • 221
  • 2
  • 7
0
votes
0 answers

How to perform einsum for decomposed tensor operations?

I am trying to measure the speed up performance between np.einsum('bcd,bce,bef->df' tensor1, tensor2, tensor1) and np.einsum('...->...', decomposed_tensor1, tensor2, decomposed_tensor1) operations. The decomposition method I am using is…
Yirmidokuz
  • 127
  • 1
  • 7
0
votes
0 answers

Can einsum be used to reshape operand?

I trying to modify the following code snippet to not use reshape a = np.random.randn(1, 2, 3, 5) b = np.random.randn(2, 5, 10) np.einsum("ijkl,mjl->kim", a, b.reshape(10,2,5)) At first I thought that the reshape is just transposing the operand,…
Kevin
  • 3,096
  • 2
  • 8
  • 37
0
votes
1 answer

Elegant Numpy Tensor product

I need to take the product over two tensors in numpy (or pytorch): I have A = np.arange(1024).reshape(8,1,128) B = np.arange(9216).reshape(8, 128, 9) And want to obtain C, with dot products summing over the last dim of A (axis=2) and the middle dim…
Carol Eisen
  • 519
  • 5
  • 18
0
votes
1 answer

Using numpy matmul in row-wise manner with broadcasting

I have an array of 3D points (n,3) that are to be rotated about the origin using a 3x3rotation matrix that is stored in the form of a nx3x3 array. At the moment I'm simply doing this in a for loop with matmul but I'm figuring this is pointless as…
0
votes
1 answer

Efficient way to compute an array matrix multiplication for a batch of arrays

I want to parallelize the following problem. Given an array w with shape (dim1,) and a matrix A with shape (dim1, dim2), I want each row of A to be multiplied for the corresponding element of w. That's quite trivial. However, I want to do that for a…
0
votes
0 answers

Python understanding array shape using einsum

I'm using einsum, and I'm having an issue creating an inner product of two 5x2 matrices such that the resulting array has a size of (5,). I was able to use the code: print(np.einsum('ij,kj->ik', A, B)) to create the cross product of the two matrices…
Pinka
  • 41
  • 3
0
votes
2 answers

extracting diagonals (sideway down) from 5d matrices using einsum

I only managed to extract one diagonal using Numpy einsum. How do I get the other diagonals like [6, 37, 68, 99] with help of einsum? x = np.arange(1, 26 ).reshape(5,5) y = np.arange(26, 51).reshape(5,5) z = np.arange(51, 76).reshape(5,5) t = …
0
votes
2 answers

numpy einsum collapse all but first dimension

I have an array of dimension (5,...) (... can be anything) and I would like to form the dot product of all dimension after the 5 such that the resulting array has shape (5,). I thought the einsum i...,i...->i looked promising, but alas import numpy…
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
0
votes
0 answers

Python: einsum inside for loop

Suppose A and B are two 4 dimensional numpy arrays with the same dimension. A = np.random.rand(5,5,2,10) B = np.random.rand(5,5,2,10) a, b, c, d = A.shape dat = [] for k in range(d): sum = 0 for l in range(c): sum = sum +…
Nicolas H
  • 535
  • 3
  • 13
0
votes
0 answers

Is there a way to do einsum in Python with boolean logic to optimize it?

Okay, so basically i'm working on some sort of image blending thing, and i have a function that will blend every image in the given array according to some specified weight, like in the code below: #Weighting Function def weight_sym(n): n =…
dwiandhika
  • 27
  • 2
  • 9