【问题标题】:How to calculate the mean value of a 4-D tensor in TensorFlow如何在 TensorFlow 中计算 4-D 张量的平均值
【发布时间】:2019-04-28 00:37:08
【问题描述】:

我刚刚用TensorFlow实现了一个CNN模型。在这里,我有一个 4 维的张量:A=[16,96,96,64](16 是批量大小,96 是图像块大小,64 是特征图的数量)。我只需要计算张量 A 中每个特征图的平均值(如平均池)并返回一个大小为 [16,1,1,64] 的新张量,它包含 A 中每个特征图的平均值.

您能告诉我我是如何意识到这一点的吗?

提前致谢!

一个大小为[16,96,96,64]的张量,我需要得到这个张量中每个特征图的平均值,并返回一个大小为[16,1,1,64]的张量。我不确定如何实现这一点。

结果应该是张量,包括张量A中特征图的平均值,它的大小为[16,1,1,64]。

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    使用tf.reduce_mean() 和指定参数axis 计算均值,然后使用tf.expand_dims() 恢复缩减轴:

    import tensorflow as tf
    import numpy as np
    
    x = tf.placeholder(tf.float32, (None, 96, 96, 64))
    
    reduced = tf.expand_dims(tf.reduce_mean(x, axis=1), 1)
    reduced = tf.expand_dims(tf.reduce_mean(reduced, axis=2), 2)
    
    data = np.random.normal(size=(16, 96, 96, 64))
    
    with tf.Session() as sess:
        evaled = reduced.eval({x:data})
        print(evaled.shape) # (16, 1, 1, 64)
    

    【讨论】:

      猜你喜欢
      • 2019-01-05
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 2018-06-16
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 1970-01-01
      相关资源
      最近更新 更多