Search code examples
tensorflowsparse-matrixmatrix-multiplicationnumpy-einsum

What is the efficient way of multiplying chain of tensors in tensorflow


I have 3 sparse tensors of dimensions A = P*N, B = Q*N and C = R*N. What is the efficient way to compute the product matrix A*B*C such that dimension of the product matrix is P*Q*R in tensorflow.?

I have tried with tf.matmul and followed by tf.reshape but it won't give the product matrix with the dimension as specified above.

Thanks.


Solution

  • tf.einsum Should help you. Though I'm not really sure what you mean by A*B*C since dimensions are incompatible for matrix multiplication. Probably something like this:

    R = tf.einsum('il,jl,kl->ijk', A, B, C)