4. Broadcasting 机制
张量维度扩张的一个手段,对一个数据重复n多次,但是没有在数据层面上的复制
Key idea
- insert 1 dim ahead if needed
需要位置插入 1 dim。 - espand dims with size 1 to same size
给 dim 为 1 的扩展成所需的数量
例如:Feature maps: [4, 32, 32, 3]
变化过程:Bias: [3] → [1, 1, 1, 3] → [4, 32, 32, 3]
In [31]: x=tf.random.normal([4,32,32,3])
In [32]: (x+tf.random.normal([3])).shape
Out[32]: TensorShape([4, 32, 32, 3])
In [33]: (x+tf.random.normal([32,32,1])).shape
Out[33]: TensorShape([4, 32, 32, 3])
In [34]: (x+tf.random.normal([4,1,1,1])).shape
Out[34]: TensorShape([4, 32, 32, 3])
In [35]: (x+tf.random.normal([1,4,1,1])).shape #报错
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)