When creating a model using Keras subclassing API, we write a custom model class and define a function named call(self, x)
(mostly to write the forward pass) which expects an input. However, this method is never called and instead of passing the input to call
, it is passed to the object of this class as model(images)
.
How are we able to call this model
object and pass values when we haven't implemented Python special method, __call__
in the class
class MyModel(Model):
def __init__(self):
super(MyModel, self).__init__()
self.conv1 = Conv2D(32, 3, activation='relu')
self.flatten = Flatten()
self.d1 = Dense(128, activation='relu')
self.d2 = Dense(10, activation='softmax')
def call(self, x):
x = self.conv1(x)
x = self.flatten(x)
x = self.d1(x)
return self.d2(x)
# Create an instance of the model
model = MyModel()
Use tf.GradientTape to train the model:
@tf.function
def train_step(images, labels):
with tf.GradientTape() as tape:
predictions = model(images)
loss = loss_object(labels, predictions)
gradients = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
train_loss(loss)
train_accuracy(labels, predictions)
Shouldn't the input be passed like below:
model = MyModel()
model.call(images)