Python 如何在tensorflow中实现最小池?

Python 如何在tensorflow中实现最小池?,python,tensorflow,machine-learning,keras,deep-learning,Python,Tensorflow,Machine Learning,Keras,Deep Learning,Tensorflow具有用于平均池和最大池的操作,但不用于最小池 是否有一些工作可以让最小池 我们可以通过这种方式手动重新创建MaxPooling x = np.random.uniform(0,1, (5,30,30,3)).astype('float32') n_channel = x.shape[-1] patches = tf.image.extract_patches(x, sizes = [1, 3, 3, 1

Tensorflow具有用于平均池和最大池的操作,但不用于最小池


是否有一些工作可以让最小池

我们可以通过这种方式手动重新创建MaxPooling

x = np.random.uniform(0,1, (5,30,30,3)).astype('float32')

n_channel = x.shape[-1]
patches = tf.image.extract_patches(x, 
                                   sizes = [1, 3, 3, 1], 
                                   strides = 4*[1], 
                                   rates = 4*[1], 
                                   padding = 'VALID')

channel_pool = [tf.reduce_max(patches[:,:,:,c::n_channel], keepdims=True, axis=-1) for c in range(n_channel)]
res = tf.concat(channel_pool, axis=-1)

tf.reduce_all(res == MaxPool2D(pool_size=(3, 3), strides=(1,1), padding="valid")(x)) ## TRUE !!! 
在上面的例子中,我们可以简单地使用tf.reduce\u min切换到MinPooling

我们可以将整个过程封装在Lambda层中,以便在keras模型中使用它:Lambdamin_poolx

def min_pool(x):

    n_channel = x.shape[-1]
    patches = tf.image.extract_patches(x, 
                                       sizes = [1, 3, 3, 1], 
                                       strides = 4*[1], 
                                       rates = 4*[1], 
                                       padding = 'VALID')

    channel_pool = [tf.reduce_min(patches[:,:,:,c::n_channel], keepdims=True, axis=-1) for c in range(n_channel)]

    return tf.concat(channel_pool, axis=-1)