Implementing a minmax maxpooling layer

Viewed 149

I need to implement a minmax pooling layer. I have 4-dimensional tensor,(batch_size,height,width,activation_maps).

I want to implement a method where the number with the largest absolute value gets propagated to the next layer instead of max element being chosen.

I have attached an example to make thins clear.

enter image description here

I had an idea to execute maxpooling operation on input tensor x twice. One time on the original tensor and the second time after negating it. After that i can compare the values of both obtained results and choose largest absolute value. However, i am stucked. I have pasted the code below. Any further help will be great.

Thanks

class CustomMaxPooling(tf.keras.layers.Layer):
      def __init__(self, pool_size,strides,padding,shape):
        super(CustomMaxPooling, self).__init__()
    
        self.pool_size =pool_size
        self.strides=strides
        self.padding = padding
        self.shape = shape
    
        
      def call(self, x):
        positive = MaxPool2D(pool_size=self.pool_size,strides=self.strides, padding=self.padding,name =self.name)(x)
        negative = MaxPool2D(pool_size=self.pool_size,strides=self.strides, padding=self.padding,name =self.name)(-x)
       
        return

Just for the sake of testing:

X = tf.random.normal([4,4,4,3],  dtype=tf.dtypes.float32, seed=None, name=None)
result = CustomMaxPooling(pool_size=(2, 2),strides=(1, 1),padding='valid',shape=X.shape)(X)
1 Answers

You could just pool the absolute values of the input tensor directly using tf.abs:

X = tf.random.normal([4,4,4,3],  dtype=tf.dtypes.float32, seed=None, name=None)

# Pool the absolute of the input tensor
result = MaxPool2d()(tf.abs(X))
Related