_MEAN_RGB = [123.15, 115.90, 103.06]
def _preprocess_subtract_imagenet_mean(inputs):
"""Subtract Imagenet mean RGB value."""
mean_rgb = tf.reshape(_MEAN_RGB, [1, 1, 1, 3])
print("mean_rgb:\n", mean_rgb)
return inputs - mean_rgb
inputs = tf.random.uniform(shape=[2, 448, 448, 3], maxval=255)
print("inputs:\n", inputs)
imgs_new = _preprocess_subtract_imagenet_mean(inputs)
print("imgs_sub:\n", imgs_new)

會Boardcast!
– 從最后面的維度開始匹配。
– 在前面插入若干維度。
– 將維度的size從1通過expand變到和某個Tensor相同的維度。
總之,Broadcasting操作就是自動實現了若干unsqueeze和expand操作,以使兩個tensor的shape一致,從而完成某些操作(往往是加法)。
