Search code examples
pythonkeras-layer

What does super achieve


This is the code for building custom keras layer

class MyLayer(Layer):

    def __init__(self, output_dim, **kwargs):
        self.output_dim = output_dim
        super(MyLayer, self).__init__(**kwargs)

what is super doing here and is it absolutely necessary ?


Solution

  • Welcome to stackoverflow. Super -

    “.. returns a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class. The search order is same as that used by getattr() except that the type itself is skipped.”
    

    Specifically in the case of Keras, the base Layer performs all the base actions for the class, as in the source code,

    https://github.com/keras-team/keras/blob/master/keras/engine/base_layer.py#L21

    You could read more about Super and Inheritance at

    https://realpython.com/python-super/