why implementing the “call” method when subclassing a tf.keras layer(or model) class makes the layer(model) object callable?

前端 未结 1 1073
栀梦
栀梦 2021-01-28 17:36

When writing customized tf.keras layers, we have to implement the "call" method, since a object of a class can be called like a function with "()&quo

相关标签:
1条回答
  • 2021-01-28 18:21
    from keras.models import Model
    import inspect
    
    inspect.getmro(Model)
    # (keras.engine.training.Model, keras.engine.network.Network, keras.engine.layer._Layer)
    

    inspect.getmro(CLS) returns a tuple of class CLS's base classes, including CLS, in method resolution order.

    The __call__ method inside Model infact comes from keras.engine.layer._Layer class. You can refer the code here

    On line 996, inside __call__ method call_fn is assigned as call & is indeed called on line 979.

    So, essentially, in a way I guess, the following holds true -

    def __call__(self, input):
        return self.call(input)
    

    Let's discuss further!

    0 讨论(0)
提交回复
热议问题