ValueError: Dimensions must be equal, but are 4096 and 9 for 'mul'. Why no broadcasting here?

ValueError: Dimensions must be equal, but are 4096 and 9 for 'mul'. Why no broadcasting here?

我有一个很简单的例子:

import tensorflow as tf
import pdb

number_features = tf.random_uniform((4096,22))

probs = number_features
probs_L = probs[:,:3]
probs_S1 = probs[:,3:12]
probs_S2 = probs[:,12:22]

confidence_no_digits = probs_L[:,0]
confidence_single_digit = probs_L[:,1] * probs_S1

with tf.Session() as sess:
    result = sess.run([confidence_single_digit])

但是这给出了:

ValueError: Dimensions must be equal, but are 4096 and 9 for 'mul' (op: 'Mul') with input shapes: [4096], [4096,9].

为什么我不能将大小为 [4096] 的向量与大小为 [4096,9] 的矩阵逐元素相乘。为什么广播在这里不起作用?

如果我没记错的话,* 符号表示逐元素乘法,而您需要矩阵乘法。您应该使用 TF 的矩阵乘法函数 matmul.

尝试:

confidence_single_digit = tf.matmul(probs_L[:,1], probs_S1)

更新:如果你想要按元素乘法,那么在这个问题中使用正常的multiplication function. This can be

尝试:

confidence_single_digit = tf.multiply(probs_L[:,1], probs_S1)

注意:我以前从未使用过TensorFlow。这可能是寻找错误的起点。

你用这个得到了想要的结果吗?

confidence_single_digit = tf.expand_dims(probs_L[:,1],1) * probs_S1

现在的形状是这些。

<bound method Tensor.get_shape of <tf.Tensor 'ExpandDims_1:0' 
shape=(4096, 1) dtype=float32>>

<bound method Tensor.get_shape of <tf.Tensor 'strided_slice_2:0' 
shape=(4096, 9) dtype=float32>>

tensorflow 中的广播遵循与 NumPy broadcasting 相同的模式。当对两个数组进行操作时,它会按元素比较它们的形状,从最后一个维度开始,然后一直到第一个维度。二维兼容时:

  • 它们相等,或者
  • 其中一个是1,或者
  • 缺少一个维度

在这种情况下,从最后一个维度开始,维度4096(第一个数组的最后一个维度)和9(第二个数组的最后一个维度)根据上述规则不兼容,因此给你一个错误。

为了修复它以获得您想要的广播效果,您可以将第一个数组转换为具有兼容的形状:

confidence_single_digit = tf.expand_dims(probs_L[:,1],1) * probs_S1

所以形状分别是(4096, 1)和(4096, 9)