2

I have a tensor of rank N and bond dimension d with shape T=(d,...,d). I would like to multiply with a matrix M=(D,d) where D is not the same as d. The resulting tensor should have shape (d,...,d,D,d,...,d).

While I can do this to get eg (d,...,d,D,d,d) tensor:

np.einsum('ij, ...jkl->...ikl', M,T)

The tensor can be of quite a large rank and I need to do this several times. Therefore I want to avoid writing out each particular case as I did above as it would be impractical.

Can anyone suggest a better/more general/alternative way to do this? I would really appreciate any help. Thanks in advance.

lightfield
  • 23
  • 3

2 Answers2

1

Interesting problem. What you want is mutiply two tensors on specific dimensions and obtain a new tensor with a specific shape.

I tried different approaches and eventually came up with a composition of numpy.tensordot and numpy.rollaxis.

The former allows the tensor product between specified axes. The latter rotates the tensor to get the expected shape.

It was an interesting question, thanks. I hope I got this right, let me know.

import numpy as np
d=4
N=5
D=7
T = np.random.randint(0,9, (d,)*N)
M = np.random.randint(0,9, (D,d))

r = np.einsum('ij, ...jkl->...ikl', M,T)

i = 1
j = -3 

v = np.tensordot(M,T,axes=[[i],[j]])
v = np.rollaxis(v,0,j)

assert((v==r).all())
MarcoP
  • 1,438
  • 10
  • 17
  • Thank you very much for your answer! With little change it becomes exactly what I was looking for. `i = 1` `j = -2` `v = np.tensordot(M,T,axes=[[i],[j-1]])` `v = np.rollaxis(v,0,j)` – lightfield Mar 16 '21 at 10:17
0

A matrix is similar (not equal) to a tensor of rank 2 https://physics.stackexchange.com/questions/20437/are-matrices-and-second-rank-tensors-the-same-thing , Multiply Tensors with different ranks )

In scikit-tensor are tensor-matrix operations ( https://pypi.org/project/scikit-tensor/ )

scikit-tensor is a Python module for multilinear algebra and tensor factorizations. Currently, scikit-tensor supports basic tensor operations such as folding/unfolding, tensor-matrix and tensor-vector products as well as the following tensor factorizations

Canonical / Parafac Decomposition
Tucker Decomposition
RESCAL
DEDICOM
INDSCAL

Moreover, all operations support dense and tensors.

source : https://github.com/mnick/scikit-tensor

Tensor contraction :

https://physics.stackexchange.com/questions/252473/converting-between-matrix-multiplication-and-tensor-contraction

ralf htp
  • 9,149
  • 4
  • 22
  • 34
  • Please forgive my imprecise language. This was just a computational problem; for my purposes it relates to the shape of the portion of the data I want to store and manipulate. – lightfield Mar 16 '21 at 10:49