TensorFlow-Broadcast details

TensorFlow官方网站对Broadcast的解释不够详细,具体要参考Scipy网站的解析。

https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html

Broadcasting用于处理不同形状的numpy arrays的算术操作,形状较小的array通过复制自身,”broadcast”到与较大的array匹配的形状。例如:

a = np.array([1.0, 2.0, 3.0])
b = 2.0
a * b
array([ 2., 4., 6.])

处理两个array时,NumPy element-wise地比较它们的形状,两个array的dimensions必须满足以下两个条件之一:

  1. they are equal
  2. one of them is 1

例如:

A (4d array): 8 x 1 x 6 x 1
B (3d array): 7 x 1 x 5
Result (4d array): 8 x 7 x 6 x 5