我正在使用Python 3.7.7。和张量流 2.1.0。我是新手。我有 N 个具有形状的张量(1, 12, 12, 512),我想对每个数组求和以获得具有相同形状的张量(1, 12, 12, 512)。然后除以N。这些张量是编码器的输出,摘要如下:_________________________________________________________________Layer (type) Output Shape Param # =================================================================input_1 (InputLayer) [(None, 200, 200, 1)] 0 _________________________________________________________________conv1_1 (Conv2D) (None, 200, 200, 64) 1664 _________________________________________________________________conv1_2 (Conv2D) (None, 200, 200, 64) 102464 _________________________________________________________________pool1 (MaxPooling2D) (None, 100, 100, 64) 0 _________________________________________________________________conv2_1 (Conv2D) (None, 100, 100, 96) 55392 _________________________________________________________________conv2_2 (Conv2D) (None, 100, 100, 96) 83040 _________________________________________________________________pool2 (MaxPooling2D) (None, 50, 50, 96) 0 _________________________________________________________________conv3_1 (Conv2D) (None, 50, 50, 128) 110720 _________________________________________________________________conv3_2 (Conv2D) (None, 50, 50, 128) 147584 _________________________________________________________________pool3 (MaxPooling2D) (None, 25, 25, 128) 0 _________________________________________________________________conv4_1 (Conv2D) (None, 25, 25, 256) 295168 我一直在寻找,但我只找到了tf.math.reduce_mean并且我认为它没有做我想做的事。我该怎么做?更新:我想我可以使用tf.math.add_n对所有张量求和,然后将结果张量除以 N。但我不确定。
1 回答
阿晨1998
TA贡献2037条经验 获得超6个赞
看起来您只是在寻找 0 轴的平均值?
import tensorflow as tf
x = tf.random.uniform((100, 12, 12, 512), 0, 1, dtype=tf.int32)
tf.reduce_mean(x, axis=0, keepdims=True)
结果形状:
TensorShape([1, 12, 12, 512])
如果您正在处理 shape 的张量列表(1, 12, 12, 512),则效果是相同的:
x = [tf.random.uniform((1, 12, 12, 512), 0, 1, dtype=tf.int32) for i in range(10)]
tf.reduce_mean(x, axis=0)
TensorShape([1, 12, 12, 512])
添加回答
举报
0/150
提交
取消