-1
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ tensordot uses reshape and transpose to reduce the problem to a np.dot call (the usual last axis, 2nd to the last axis sum of products). It may then reshape/transpose back. With newer einsum and matmul available, it is no longer that useful. $\endgroup$
    – hpaulj
    Commented Jun 2, 2020 at 20:09

1 Answer 1

2
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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