Abhishek Kishore
Abhishek Kishore

Reputation: 370

Why should the layers be initialized in the constructor of Model Subclass in tensorflow

I created a custom model by subclassing tf.keras.Model class. When I initialized(tf.keras.layers.Dense(20,activation='relu')) and called Dense layer in the call method of my custom Model, the model did not train at all. However, when I initialized the Dense layer in the constructor of my custom model class and later called the Dense layer in the call method of my custom model, the model trained well. Also, model summary in the first case showed zero trainable parameters while that was not the case in the second case.

Please help me understand why this happens.

I did not find any explanation for this on tensorflow.org.

Upvotes: 0

Views: 213

Answers (1)

Abhishek Kishore
Abhishek Kishore

Reputation: 370

I understand now. The call method is called after each batch and each call method would reinitialize the layer class(if initialized in the call method of model subclass) and all the previous weights would be lost.

Upvotes: 1

Related Questions