M_Fatih89
M_Fatih89

Reputation: 49

Multiplying two 3d tensors with different shapes (Tensorflow)

In TensorFlow, If I have two 3 dimensional tensors, one of dimension (100, 9, 135) and the other has dimension (100, 29, 135):

x1: Tensor(shape=(100, 9, 135), dtype=float64)

x2: Tensor(shape=(100, 29, 135), dtype=float64)

I need to multiply these two tensors, so when I was using "tf.multiply" I got an error as follows:

z = tf.multiply(x1,x2)

print("z:", z)

ValueError: Dimensions must be equal, but are 9 and 29, with input shapes: [100,9,135], [100,29,135].

How can this be done in TensorFlow? Thanks in advance.

Upvotes: 1

Views: 193

Answers (1)

AloneTogether
AloneTogether

Reputation: 26698

I am not sure what output shape you expect, but you can try experimenting with tf.einsum to do matrix multiplication:

import tensorflow as tf

t1 = tf.random.normal((2, 9, 35))
t2 = tf.random.normal((2, 29, 35))

e = tf.einsum('bij,bkj->bik', t1, t2)
# or e =  tf.einsum('...ij,...kj->...ik', t1, t2)

print(e)

Check the docs for more options.

Upvotes: 1

Related Questions