关于TF下的avg_pool对于padding=same时的注意事项

本文通过实验探讨TensorFlow中均值池化的实现细节,特别是关于'VALID'和'SAME'填充方式下如何计算平均值,澄清了不正确地排除零元素的误解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

看李金洪的那本tf的入门书籍,在均值池化部分实验中出现了疑惑

import tensorflow as tf  
  
img=tf.constant([  
        [[0.0,4.0],[0.0,4.0],[0.0,4.0],[0.0,4.0]],  
        [[1.0,5.0],[1.0,5.0],[1.0,5.0],[1.0,5.0]],  
        [[2.0,6.0],[2.0,6.0],[2.0,6.0],[2.0,6.0]],  
        [[3.0,7.0],[3.0,7.0], [3.0,7.0],[3.0,7.0]]
    ])  
  
img=tf.reshape(img,[1,4,4,2])  
  
pooling=tf.nn.max_pool(img,[1,2,2,1],[1,2,2,1],padding='VALID')  
pooling1=tf.nn.max_pool(img,[1,2,2,1],[1,1,1,1],padding='VALID')
pooling2=tf.nn.avg_pool(img,[1,4,4,1],[1,1,1,1],padding='SAME')  
pooling3=tf.nn.avg_pool(img,[1,4,4,1],[1,4,4,1],padding='SAME') 
nt_hpool2_flat = tf.reshape(tf.transpose(img), [-1, 16]) 
pooling4=tf.reduce_mean(nt_hpool2_flat,1) #1对行求均值(1表示轴是列)   0 对列求均值


with tf.Session() as sess:  
    print("image:")  
    image=sess.run(img)  
    print (image)  
    result=sess.run(pooling)  
    print ("reslut:\n",result)  

运行结果:

[[[[ 1.   5. ]
   [ 1.   5. ]
   [ 1.   5. ]
   [ 1.   5. ]]

  [[ 1.5  5.5]
   [ 1.5  5.5]
   [ 1.5  5.5]
   [ 1.5  5.5]]

  [[ 2.   6. ]
   [ 2.   6. ]
   [ 2.   6. ]
   [ 2.   6. ]]

  [[ 2.5  6.5]
   [ 2.5  6.5]
   [ 2.5  6.5]
   [ 2.5  6.5]]]]

书中对均值池化后解释是,为零元素不计入均值计算的分母中,实际上这里有误。

参见https://stackoverflow.com/questions/50543862/tensorflow-tf-nn-avg-pool-with-same-padding-does-not-average-over-padded-pi

 

class CBAMLayer(tf.keras.layers.Layer): def __init__(self, reduction_ratio=16, **kwargs): super(CBAMLayer, self).__init__(**kwargs) self.reduction_ratio = reduction_ratio def build(self, input_shape): channel = input_shape[-1] self.shared_layer_one = layers.Dense(channel // self.reduction_ratio, activation='relu') self.shared_layer_two = layers.Dense(channel) self.conv2d = layers.Conv2D(1, kernel_size=7, padding='same', activation='sigmoid') def call(self, input_feature): x = tf.cast(input_feature, tf.float32) # 统一数据类型 # Channel attention avg_pool = tf.reduce_mean(x, axis=[1, 2], keepdims=True) max_pool = tf.reduce_max(x, axis=[1, 2], keepdims=True) avg_out = self.shared_layer_two(self.shared_layer_one(avg_pool)) max_out = self.shared_layer_two(self.shared_layer_one(max_pool)) cbam_feature = tf.nn.sigmoid(avg_out + max_out) cbam_feature = tf.cast(cbam_feature, x.dtype) # 统一数据类型 channel_refined = x * cbam_feature avg_spatial = tf.reduce_mean(channel_refined, axis=-1, keepdims=True) max_spatial = tf.reduce_max(channel_refined, axis=-1, keepdims=True) concat = tf.concat([avg_spatial, max_spatial], axis=-1) spatial_attention = self.conv2d(concat) refined_feature = channel_refined * spatial_attention return channel_refined # Spatial attention avg_spatial = tf.reduce_mean(channel_refined, axis=-1, keepdims=True) max_spatial = tf.reduce_max(channel_refined, axis=-1, keepdims=True) concat = tf.concat([avg_spatial, max_spatial], axis=-1) spatial_attention = self.conv2d(concat) refined_feature = channel_refined * spatial_attention return tf.cast(refined_feature, input_dtype) def get_config(self): config = super(CBAMLayer, self).get_config() config.update({'reduction_ratio': self.reduction_ratio}) return
03-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值