Maxout description and implementation (Python)

at first

This article describes the following and implements the Maxout function in Python code.

  1. What is the Maxout function?
  2. Maxout function code and usage

In this article, Maxout function is implemented so that it can be used as a layer of Keras. The code and description are at the end.

What is the Maxout function?

The Maxout function is used as a layer activation function in deep learning models such as CNN and DNN. The main advantage of using the Maxout function as an activation function is that you can pass data to the next layer without changing the size of the data given by the previous layer.

To explain this, in general, CNN and DNN often use a Pooling layer to reduce the size of data, whereas the Maxout function reduces the number of dimensions corresponding to the number of channels instead of reducing the size. I am doing. As a result, it is not necessary to use the Pooling layer, and it is used when you want to keep the size of the data in the layer as much as possible. (Actually, it is used in the CNN layer and is often used in combination with the Pooling layer)

When written as a mathematical formula, the Maxout function can be expressed as follows. スクリーンショット 2020-06-13 21.28.40.png

What the Maxout function actually does is take the Max of the pixels located at the same location in each dimension (channel, feature map) and use that as the pixel of the output data. The picture will look like the one below.

40537_2019_233_Fig2_HTML.png

Reference https://www.google.com/url?sa=i&url=https%3A%2F%2Flink.springer.com%2Farticle%2F10.1186%2Fs40537-019-0233-0&psig=AOvVaw2-jjWv_TTq3t2bz_Py6_S0&ust=1592137921627000&source=images&cd=vfe&ved=0CA0QjhxqFwoTCOiAvJDm_ukCFQAAAAAdAAAAABAD

When actually implementing it as code, make it possible to specify the number of dimensions after output. For example, let the number of dimensions of the output be 2 and the number of dimensions of the input be N. In this case, the input data is divided into two n / 2D chunks, and Maxout is performed for each.

image.png

Implementation as Keras Layer

Implement as follows. It has been confirmed to work with both Tensorflow 2 and 1.

Maxout.py



import tensorflow as tf
from typeguard import typechecked
import keras

class Maxout(keras.layers.Layer):
    #num_Specify the number of dimensions after output with unit
    #Specify the axis for which you want Max in axis (usually the default value. For Channel first, specify 1)
    @typechecked
    def __init__(self, num_units: int, axis: int = -1, **kwargs):
        super().__init__(**kwargs)
        self.num_units = num_units
        self.axis = axis

    def call(self, inputs):
        inputs = tf.convert_to_tensor(inputs)
        shape = inputs.get_shape().as_list()
        # Dealing with batches with arbitrary sizes
        for i in range(len(shape)):
            if shape[i] is None:
                shape[i] = tf.shape(inputs)[i]

        num_channels = shape[self.axis]
        if not isinstance(num_channels, tf.Tensor) and num_channels % self.num_units:
            raise ValueError(
                "number of features({}) is not "
                "a multiple of num_units({})".format(num_channels, self.num_units)
            )

        if self.axis < 0:
            axis = self.axis + len(shape)
        else:
            axis = self.axis
        assert axis >= 0, "Find invalid axis: {}".format(self.axis)

        expand_shape = shape[:]
        expand_shape[axis] = self.num_units
        k = num_channels // self.num_units
        expand_shape.insert(axis, k)

        outputs = tf.math.reduce_max(
            tf.reshape(inputs, expand_shape), axis, keepdims=False
        )
        return outputs

    def compute_output_shape(self, input_shape):
        input_shape = tf.TensorShape(input_shape).as_list()
        input_shape[self.axis] = self.num_units
        return tf.TensorShape(input_shape)

    def get_config(self):
        config = {"num_units": self.num_units, "axis": self.axis}
        base_config = super().get_config()
        return {**base_config, **config}

A usage example is shown below. If you call it like this, it will work.

example.py



from Maxout import Maxout

conv2d = Conv2D(64, kernel_size, strides, padding)(input)
maxout = Maxout(n_units)(conv2d)

in conclusion

This time I explained the Maxout function. Maxout is often used as an activation function for LCNN etc. in recent studies. I hope you find this article useful.

Reference Maxout Networks (https://arxiv.org/pdf/1302.4389.pdf) A Light CNN for Deep Face Representation with Noisy Labels (https://arxiv.org/pdf/1511.02683.pdf)

Recommended Posts

Maxout description and implementation (Python)
Sorting algorithm and implementation in Python
Implementation module "deque" in queue and Python
Python data structure and internal implementation ~ List ~
Implementation of TRIE tree with Python and LOUDS
Explanation of edit distance and implementation in Python
[python] Compress and decompress
Python and numpy tips
[Python] pip and wheel
RNN implementation in python
ValueObject implementation in Python
Batch design and python
Python iterators and generators
Python packages and modules
Vue-Cli and Python integration
Ruby, Python and map
python input and output
Python and Ruby split
Perceptron basics and implementation
Python3, venv and Ansible
Python asyncio and ContextVar
SVM implementation in python
Merge sort implementation / complexity analysis and experiment in Python
[# 2] Make Minecraft with Python. ~ Model drawing and player implementation ~
Logical symbols learned in marriage (and implementation examples in Python)
Difference between return, return None, and no return description in Python
Overview of generalized linear models and implementation in Python
Programming with Python and Tkinter
Python implementation of CSS3 blend mode and talk of color space
Encryption and decryption with Python
Python: Class and instance variables
3-3, Python strings and character codes
Python and hardware-Using RS232C with Python-
Python on Ruby and angry Ruby on Python
Python indentation and string format
Python real division (/) and integer division (//)
Install Python and Flask (Windows 10)
About python objects and classes
About Python variables and objects
Apache mod_auth_tkt and Python AuthTkt
Å (Ongustromu) and NFC @ Python
[Line / Python] Beacon implementation memo
Understand Python packages and modules
# 2 [python3] Separation and comment out
Python shallow copy and deep copy
Python and ruby slice memo
Explanation and implementation of SocialFoceModel
Python installation and basic grammar
Normalizing Flow theory and implementation
I compared Java and Python!
Python shallow and deep copy
About Python, len () and randint ()
Install Python 3.7 and Django 3.0 (CentOS)
Python class variables and instance variables
Ruby and Python syntax ~ branch ~
[Python] Python and security-① What is Python?
Stack and Queue in Python
python metaclass and sqlalchemy declareative
Fibonacci and prime implementations (python)
Python implementation of particle filters
Python basics: conditions and iterations