吴裕雄--天生自然TensorFlow2教程:Broadcasting

Broadcasting可以理解成把维度分成大维度和小维度,小维度较为具体,大维度更加抽象。也就是小维度针对某个示例,然后让这个示例通用语大维度。

import tensorflow as tf

x = tf.random.normal([4,32,32,3])
x.shape
(x+tf.random.normal([3])).shape
(x+tf.random.normal([32,32,1])).shape
(x+tf.random.normal([4,1,1,1])).shape
try:
    (x+tf.random.normal([1,4,1,1])).shape
except Exception as e:
    print(e)
(x+tf.random.normal([4,1,1,1])).shape
b = tf.broadcast_to(tf.random.normal([4,1,1,1]),[4,32,32,3])
b.shape
a = tf.ones([3,4])
a.shape
a1 = tf.broadcast_to(a,[2,3,4])
a1.shape
a2 = tf.expand_dims(a,axis=0)  # 0前插入一维
a2.shape
a2 = tf.tile(a2,[2,1,1])  # 复制一维2次,复制二、三维1次
a2.shape
原文地址:https://www.cnblogs.com/tszr/p/12123928.html