Search code examples
pythontensorflowsubclassing

The all method not see the instance field values for subclassing keras.Model


For subclassing to create a Tensorflow model with the following code:

class MyClass(keras.Model):
    def __int__(
        self,
        input_shape: tuple,
        classes_count: int = 10,
        model_name: str = 'model_name',
        **kwargs,
    ):
        super(MyClass, self).__init__(name=self.__class__.__name__, **kwargs)
        self.my_info = "foo"

    def call(self, inputs):
        x = self.my_info

        return x


var = MyClass((2, 2))
print(var.call("asd"))

The call method can't see the self field value:

    x = self.my_info
AttributeError: 'MyClass' object has no attribute 'my_info'

What am I doing wrong and how to get access to self elements?


Solution

  • You've called your constructor __int__ rather than __init__. It didn't cause an error directly because __int__ is a valid magic method and keras.Model has a default constructor.