I've encountered a program where np.tensordot was used, so I tried looking it up but I can't really understand what this function is doing...

I feel rather confident in my understanding of tensors in a abstract context (i.e. in pure maths/physics), but I don't really understand what this tensordot is supposed to do. I'm also familiar with np.einsum (which I find much simpler to use/understand) and I think I read somewhere that one can perform the same operations with tensordot and einsum, is this true?

To maybe give a concrete example. Take a set of $B:=\{A^{[n]}\}$ of 3-tensors, i.e. $(A^{[n]})_{ijk}$ are the components (if you are familiat with physics, one might think of MPS). A left normalized-MPS would then satisfy $$\sum_{i_k} \left(A^{[k]i_k}\right)^\dagger A^{[k]i_k}= \operatorname{id}.$$

Apparantly one way to calculate this expression is given by the following code:


Unfortunately I have no idea what this axes-specification here does. I tired looking it up for simpler examples (see here) but I don't really get it.

I think it would help me immensly to see some more complex examples wirtten out in code form, i.e. with tensordot, but also the mathematical version in form of products of coefficients of abstract tensors.


If you are familiar with einsum, maybe this explanation does it: axes[0] and axes[1] specify the locations of the repeated letters in the parameters of einsum. For instance,

np.tensordot(a, b, axes=[(0,2),(3,1)])

corresponds to

np.einsum('ijkl,mkni', a, b)

Indeed, 'ijkl'[(0,2)] == 'ik' == 'mkni'[(3,1)], and all the other letters are distinct.

| cite | improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.