RobinHood
RobinHood

Reputation: 407

Multiply Tensors with different ranks

I would like to multiply two tensors with different ranks, e.g.

tensor A -> (i x j x k x l)
tensor B -> (l x m)

to get another tensor with this dimensions.

tensor C -> (i x j x k x m) = (i x j x k x l)*(l x m)

In tensorflow I have to reshape all the time to get the output as

(A.reshape(-1,l)*B).reshape(i,j,k,m)

Any other operation that does it directly?

Upvotes: 5

Views: 2638

Answers (1)

Yan
Yan

Reputation: 386

You need tf.tensordot. Reshaping before real multiplication is not necessary. If you want to contract tensor A and B along the A's last dimension and the B's first dimension, you can multiply tensor A and tensor B in this way:

tf.tensordot(A, B, axes=[[3], [0]])

Upvotes: 3

Related Questions