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
1 answer

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) wherein the intended output is (1, H, F); H is an…
0
votes
0 answers

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=0; j < 4; j++){ jkpsi=nd*j; …
Luis ALberto
  • 103
  • 3
0
votes
0 answers

Multiplication of 3d array using np.einsum

Hello I have just recently started working with python 3d arrays and I am not too familiar with how to multiply each block in the 3d array with all other blocks in the 3d array. I am working with a np array with shape (31, 22, 16). Let say my numpy…
0
votes
2 answers

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 = np.random.randn(10, 10, 3, 3) b =…
Linus
  • 400
  • 2
  • 9
0
votes
1 answer

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 take into account the range on the inner sum O =…
Will
  • 198
  • 1
  • 6
0
votes
1 answer

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]], [[2, 0.2], [4, 0.3], [6, 0.4], [8, 0.5]], …
Sengiley
  • 221
  • 2
  • 7
0
votes
0 answers

Improving the time for summing a stack of inverse matrices

I'm hoping to speed up a Python script that inverts a large number of small matrices (a 2D array of matrices basically) and then performs a matrix sum over one of the axes of the outer array. So far, this is what I have, but I'm if I used np.einsum…
Andrew Hardy
  • 232
  • 1
  • 8
0
votes
0 answers

Proving Properties of Matrices (Associativity) using np.einsum

I'm experimenting with np.einsum and I was wondering if there's a way to prove Associativity just using np.einsum. Here's the data: A = np.array([[1, 1, 1], [2, 2, 2], [5, 5, 5]]) B = np.array([[0, 1, 0], …
Martin H
  • 155
  • 2
  • 3
  • 14
0
votes
2 answers

How to compute the outer sum (similar to outer product

Given tensors x and y, each with shape (num_batches, d), how can I use PyTorch to compute the sum of every combination of x and y within a batch? This is similar to outer product, except we don't want to multiply, but sum. (This implies that I…
SRobertJames
  • 8,210
  • 14
  • 60
  • 107
0
votes
0 answers

What does np.einsum act on?

I am facing with a problem! How does np.einsum act on these tensors? a = np.random.rand(2, 2, 5, 5) b = np.random.rand(4, 5, 5, 1) c = np.einsum('aijb,qwei->qweaj', b, a) the output shape is: (2, 2, 5, 4, 5) a = np.random.rand(2, 2, 5, 4, 5) b =…
Kak Milad
  • 1
  • 2
0
votes
1 answer

einsum not giving overflow error when applied to int arrays

I just had a bug which was based on np.sum and an equivalent (or at least I thought so...) np.einsum command not giving the same result. Here is an example: import numpy.random array = np.random.randint(-10000, 10000, size=(4, 100, 200, 600),…
Wolpertinger
  • 1,169
  • 2
  • 13
  • 30
0
votes
1 answer

einsum equivalent for ndarray multiplication

I have the following multiplication routine: import numpy as np a = np.random.rand(3,3) b = np.random.rand(3,50,50) res = np.zeros((3, 50, 50)) for i in range(50): for j in range(50): res[:,i,j] = a @ b[:,i,j] What is the einsum…
Trailer
  • 155
  • 9
0
votes
1 answer

Matrix Vector Product across Multiple Dimensions

I have two arrays: A = torch.rand((64, 128, 10, 10)) B = torch.rand((64, 128, 10)) I would like to compute the product, represented by C, where we do a matrix-vector multiplication across the first and second dimensions of A and B, so: # C should…
sgk525
  • 132
  • 2
  • 13
0
votes
1 answer

fastest way to stack ndarrays

Gist Basically I want to perform an increase in dimension of two axes on a n-dimensonal tensor. For some reason this operation seems very slow on bigger tensors. If someone can give me a reason or better method I'd be very happy. Goal Going from (4,…
gistBatch
  • 68
  • 1
  • 9
0
votes
0 answers

Vectorize this matmul like operation in Numpy, Numba, Cupy manner

Say I have an vector v(ij) and matrix M(jk). v = np.array([1.0, 1.0, 0.0, 0.0], dtype=np.float32) M = np.array([[0.0, 0.0, 0.2, 0.0], [0.0, 0.0, 0.0, -0.1], [0.0, 0.0, 0.0, 0.5], [0.0, 0.0, 0.0, 0.0]],…
Gestalt
  • 11
  • 1