有人能告诉我这里发生了什么吗?我很困惑。
In [125]: a
Out[125]: <tf.Tensor 'MatMul_86739:0' shape=(100, 1) dtype=float32>
In [126]: embed
Out[126]: <tf.Tensor 'embedding_lookup_41205:0' shape=(100,) dtype=float32>
In [128]: a+embed
Out[128]: <tf.Tensor 'add_43373:0' shape=(100, 100) dtype=float32>
(100,1)+(100,)怎么能(100,100)?如果是的话,为什么?
答案 0 :(得分:1)
TensorFlow广播运营商的规则基于NumPy's broadcasting rules。
广播的基本算法从右到左工作。假设我们正在添加(或应用另一个二进制广播运算符)两个张量x
和y
,以下代码计算结果的形状:
result_shape = []
# Loop over the matching dimensions of x and y in reverse.
for x_dim, y_dim in zip(x.shape[::-1], y.shape[::-1]):
if x.shape == y.shape:
result_shape.insert(0, x.shape)
elif x.shape == 1:
result_shape.insert(0, y.shape) # x will be broadcast along this dimension.
elif y.shape == 1:
result_shape.insert(0, x.shape) # y will be broadcast along this dimension.
else:
raise ValueError("Shapes of x and y are incompatible.")
# If x and y have a different rank, the leading dimensions are inherited
# from the tensor with higher rank.
if len(x.shape) > len(y.shape):
num_leading_dims = len(x.shape) - len(y.shape)
result_shape = x.shape[0:num_leading_dims] + result_shape
elif len(y.shape) > len(x.shape):
num_leading_dims = len(y.shape) - len(x.shape)
result_shape = y.shape[0:num_leading_dims] + result_shape
现在,在您的示例中,您有x.shape = (100,)
和y.shape = (100, 1)
:
100
和1
之间,因此result_shape = [100]
。y.shape
超过x.shape
,因此我们将结果的前导维度添加到result_shape = [100, 100]
。