使用 Tensorflow 执行 matmul 时出现 ValueError [英] ValueError when performing matmul with Tensorflow
问题描述
我完全是 TensorFlow 的初学者,我正在尝试将两个矩阵相乘,但我不断收到一个异常消息:
ValueError: Shapes TensorShape([Dimension(2)]) 和 TensorShape([Dimension(None), Dimension(None)]) 必须具有相同的等级
这是最小的示例代码:
data = np.array([0.1, 0.2])x = tf.placeholder("float", shape=[2])T1 = tf.Variable(tf.ones([2,2]))l1 = tf.matmul(T1, x)init = tf.initialize_all_variables()使用 tf.Session() 作为 sess:sess.run(初始化)sess.run(feed_dict={x: 数据}
令人困惑的是,以下非常相似的代码运行良好:
data = np.array([0.1, 0.2])x = tf.placeholder("float", shape=[2])T1 = tf.Variable(tf.ones([2,2]))init = tf.initialize_all_variables()使用 tf.Session() 作为 sess:sess.run(初始化)sess.run(T1*x, feed_dict={x: 数据}
谁能指出问题所在?我一定在这里遗漏了一些明显的东西..
tf.matmul()
op 要求它的两个输入都是矩阵(即二维张量)*,并且不执行任何自动转换.您的 T1
变量是一个矩阵,但您的 x
占位符是一个长度为 2 的向量(即一维张量),这是错误的来源.>
相比之下,*
运算符(tf.multiply()
) 是一个广播元素乘法.它将按照 NumPy 广播将向量参数转换为矩阵规则.
为了让你的矩阵乘法工作,你可以要求 x
是一个矩阵:
data = np.array([[0.1], [0.2]])x = tf.placeholder(tf.float32, shape=[2, 1])T1 = tf.Variable(tf.ones([2, 2]))l1 = tf.matmul(T1, x)init = tf.initialize_all_variables()使用 tf.Session() 作为 sess:sess.run(初始化)sess.run(l1, feed_dict={x: 数据})
...或者你可以使用 tf.expand_dims()代码>
将向量转换为矩阵的操作:
data = np.array([0.1, 0.2])x = tf.placeholder(tf.float32, shape=[2])T1 = tf.Variable(tf.ones([2, 2]))l1 = tf.matmul(T1, tf.expand_dims(x, 1))init = tf.initialize_all_variables()使用 tf.Session() 作为 sess:# ...
* 刚开始贴答案时确实如此,但现在 tf.matmul()
也支持批量矩阵乘法.这要求两个参数都具有至少 2 个维度.有关详细信息,请参阅文档.
I'm a total beginner to TensorFlow, and I'm trying to multiply two matrices together, but I keep getting an exception that says:
ValueError: Shapes TensorShape([Dimension(2)]) and TensorShape([Dimension(None), Dimension(None)]) must have the same rank
Here's minimal example code:
data = np.array([0.1, 0.2])
x = tf.placeholder("float", shape=[2])
T1 = tf.Variable(tf.ones([2,2]))
l1 = tf.matmul(T1, x)
init = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
sess.run(feed_dict={x: data}
Confusingly, the following very similar code works fine:
data = np.array([0.1, 0.2])
x = tf.placeholder("float", shape=[2])
T1 = tf.Variable(tf.ones([2,2]))
init = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
sess.run(T1*x, feed_dict={x: data}
Can anyone point to what the issue is? I must be missing something obvious here..
The tf.matmul()
op requires that both of its inputs are matrices (i.e. 2-D tensors)*, and doesn't perform any automatic conversion. Your T1
variable is a matrix, but your x
placeholder is a length-2 vector (i.e. a 1-D tensor), which is the source of the error.
By contrast, the *
operator (an alias for tf.multiply()
) is a broadcasting element-wise multiplication. It will convert the vector argument to a matrix by following NumPy broadcasting rules.
To make your matrix multiplication work, you can either require that x
is a matrix:
data = np.array([[0.1], [0.2]])
x = tf.placeholder(tf.float32, shape=[2, 1])
T1 = tf.Variable(tf.ones([2, 2]))
l1 = tf.matmul(T1, x)
init = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
sess.run(l1, feed_dict={x: data})
...or you could use the tf.expand_dims()
op to convert the vector to a matrix:
data = np.array([0.1, 0.2])
x = tf.placeholder(tf.float32, shape=[2])
T1 = tf.Variable(tf.ones([2, 2]))
l1 = tf.matmul(T1, tf.expand_dims(x, 1))
init = tf.initialize_all_variables()
with tf.Session() as sess:
# ...
* This was true when I posted the answer at first, but now tf.matmul()
also supports batched matrix multiplications. This requires both arguments to have at least 2 dimensions. See the documentation for more details.
这篇关于使用 Tensorflow 执行 matmul 时出现 ValueError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!