ValueError:形状必须是第 2 级,但对于“MatMul"是第 3 级 [英] ValueError: Shape must be rank 2 but is rank 3 for 'MatMul'
本文介绍了ValueError:形状必须是第 2 级,但对于“MatMul"是第 3 级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下 TensorFlow 代码:
I have the following TensorFlow code:
layer_1 = tf.add(tf.matmul(tf.cast(x, tf.float32), weights['h1']), biases['b1'])
但是抛出以下错误:
ValueError: Shape must be rank 2 but is rank 3 for 'MatMul' (op: 'MatMul') with input shapes: [?,5741,20000], [20000,128].
它说 x
的形状是 (?,5741,20000).如何将 x
的形状转换为 (5741, 20000)?
It says that x
has the shape of (?,5741,20000). How could I transform the shape of x
to (5741, 20000)?
先谢谢你!
推荐答案
我建议使用张量点积而不是简单的矩阵乘法以保持批量大小.这个答案比 @mrry
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'])
这篇关于ValueError:形状必须是第 2 级,但对于“MatMul"是第 3 级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文