Questions tagged [tensordot]

Tag for questions about the tensordot operation in n-dimensional arrays / tensors. Tensordot computes the dot product along specified axes for arrays.

Tensordot computes the dot product along specified axes for arrays. This operation is implemented in most matrix computation toolkits such as numpy and tensorflow.

38 questions
5
votes
1 answer

Most Pythonic way to multiply these two vectors?

I have two ndarrays with shapes: A = (32,512,640) B = (4,512) I need to multiply A and B such that I get a new ndarray: C = (4,32,512,640) Another way to think of it is that each row of vector B is multiplied along axis=-2 of A, which results in…
wolfblade87
  • 173
  • 10
4
votes
3 answers

Dot product between two 3D tensors

I have two 3D tensors, tensor A which has shape [B,N,S] and tensor B which also has shape [B,N,S]. What I want to get is a third tensor C, which I expect to have [B,B,N] shape, where the element C[i,j,k] = np.dot(A[i,k,:], B[j,k,:]. I also want to…
gorjan
  • 5,405
  • 2
  • 20
  • 40
3
votes
1 answer

Memory and time in tensor operations python

Goal My goal is to calculate the tensor given by the formula which you can see below. The indices i, j, k, l run from 0 to 40 and p, m, x from 0 to 80. Tensordot approach This summation is just contracting 6 indices of enormous tensor. I tried to…
Michal
  • 33
  • 4
3
votes
1 answer

Python vectorization of matrix-vector operation

I have a Matrix A with shape (2,2,N) and a Matrix V with shape (2,N) I want to vectorize the following: F = np.zeros(N) for k in xrange(N): F[k] = np.dot( A[:,:,k], V[:,k] ).sum() Any way this can be done with either tensordot or any other…
fmonegaglia
  • 2,749
  • 2
  • 24
  • 34
2
votes
2 answers

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.shape is on the order of (1000, 5, 30, 30), and…
Faydey
  • 725
  • 1
  • 5
  • 12
2
votes
1 answer

NumPy tensordot grouped calculation

suppose I have two arrays: import numpy as np a=np.array([[1,2], [3,4]]) b=np.array([[1,2], [3,4]]) and I want to element-wise multiply the arrays then sum the elements, i.e. 1*1 + 2*2 + 3*3 + 4*4 = 30, I can…
Sam-gege
  • 723
  • 3
  • 12
2
votes
1 answer

One line einsum functions with "interleaved" output indexing impossible to recreate using tensordot?

The similarities and differences between NumPy's tensordot and einsum functions are well documented and have been extensively discussed in this forum (e.g. [1], [2], [3], [4], [5]). However, I've run into an instance of matrix multiplication using…
ryanhill1
  • 165
  • 1
  • 9
2
votes
1 answer

How to make tensordot operations after permutation

I have 2 tensors, A and B: A = torch.randn([32,128,64,12],dtype=torch.float64) B = torch.randn([64,12,64,12],dtype=torch.float64) C = torch.tensordot(A,B,([2,3],[0,1])) D = C.permute(0,2,1,3) # shape:[32,64,128,12] tensor D comes from the…
2
votes
2 answers

Using tensordot with torch.sparse tensors

Is it possible to use a similar method as "tensordot" with torch.sparse tensors? I am trying to apply a 4 dimensional tensor onto a 2 dimensional tensor. This is possible using torch or numpy. However, I did not find the way to do it using…
ZZZ
  • 21
  • 4
2
votes
0 answers

faster alternative to numpy einsum?

I have two matrices - U and V with n vectors in each one as follows: U = [u1, u2, u3,... un] V = [v1, v2, v3,... vn] (in fact U and V are 2d numpy arrays, the vectors u1..un, and v1...vn are 1d) I want to create a 1d numpy array of n values: W =…
Assaf
  • 184
  • 1
  • 11
2
votes
2 answers

Vectorized computation of numpys tensor dot

I have two vectors containing tensors of shape (3,3) and shape (3,3,3,3) respectively. The vectors have the same length, I am computing the element-wise tensor dot of these two vectors . For example, want to vectorise the following computation to…
T A
  • 1,677
  • 4
  • 21
  • 29
1
vote
2 answers

np.tensordot function, how to multiply a tensor by successive slices of another?

I want to multiply two 3D tensors in a specific way. The two tensors have shapes T1 = (a,b,c) and T2 = (d,b,c). What I want is to multiply a times T2 by the successive 'slices' (b,c) of a. In other words, I want to have the same as this code…
Gaetano
  • 111
  • 2
1
vote
1 answer

Replicate operation tensor operation using `torch.tensordot()` and `torch.stack()`

I have a tensor operation that I would like to replicate using a combination of torch.stack() and torch.tensordot() to generalize it further on a larger program. In summary, I want to replicate the tensor V_1 using said operations into another…
1
vote
2 answers

Built-in index dependent weight for tensordot in numpy?

I would like to obtain a tensordot of two arrays with the same shape with index-dependent weight applied, without use of explicit loop. For example, import numpy as np A=np.array([1,2,3]) B=np.array([-2,6,9]) C=np.zeros((3,3)) for i in…
user16308
  • 43
  • 5
1
vote
1 answer

numpy einsum/tensordot with shared non-contracted axis

Suppose I have two arrays: import numpy as np a = np.random.randn(32, 6, 6, 20, 64, 3, 3) b = np.random.randn(20, 128, 64, 3, 3) and want to sum over the last 3 axes, and keep the shared axis. The output dimension should be (32,6,6,20,128). Notice…
Sam-gege
  • 723
  • 3
  • 12
1
2 3