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:
np.tensordot(np.conjugate(B),B,axes=([0,1],[0,1]))
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.
tensordot
usesreshape
andtranspose
to reduce the problem to anp.dot
call (the usual last axis, 2nd to the last axis sum of products). It may then reshape/transpose back. With newereinsum
andmatmul
available, it is no longer that useful. $\endgroup$