简体   繁体   中英

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!

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'])

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]

假设x的动态形状x(1, 5741, 20000)可以将其形状变换到(5741, 20000)使用tf.squeeze()如下:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM