UrmLmn
UrmLmn

Reputation: 1257

ValueError: Shape must be rank 2 but is rank 3 for 'MatMul'

I have the following TensorFlow code:

layer_1 = tf.add(tf.matmul(tf.cast(x, tf.float32), weights['h1']), biases['b1'])

But is throwing the following error:

ValueError: Shape must be rank 2 but is rank 3 for 'MatMul' (op: 'MatMul') with input shapes: [?,5741,20000], [20000,128].

It says that x has the shape of (?,5741,20000). How could I transform the shape of x to (5741, 20000)?

Thank you in advance!

Upvotes: 4

Views: 8685

Answers (3)

nairouz mrabah
nairouz mrabah

Reputation: 1217

I would suggest to work with tensors dot product instead of simple matrix multiplication in order to keep the batch size. This is answer is more general than @mrry

layer_1 = tf.add(tf.tensordot(tf.cast(x, tf.float32), weights['h1'], [[2], [0]]), biases['b1'])

Upvotes: 3

mrry
mrry

Reputation: 126194

Assuming x the dynamic shape of x is (1, 5741, 20000), you can transform its shape to (5741, 20000) using tf.squeeze() as follows:

x = tf.squeeze(x, axis=[0])

Upvotes: -1

Heapify
Heapify

Reputation: 2901

It looks like you are trying to matrix multiply 'x' with 'weights'. x has a shape of [5741, 20000] for one example, but when you feed examples in batches x will have a shape of [?, 5741, 20000]. Similarly, weights should also have a shape of [?, 20000, 128]. But, from the error, it looks like your weights are still [20000, 128] which tells me that you have some problem in your code which is not transforming the weights variable to shape [?, 20000, 128]. When you can figure this out, the error should go away. The result of matrix multiplication should have a shape of [?, 5741, 128]

Upvotes: 0

Related Questions