Zhao
Zhao

Reputation: 2193

What is difference between 'call' and '__call__' in TensorFlow BasicLSTMCell implementation?

I am studying Tensorflow BasicLSTMCell while I found that there are two similar methods within the class: __call__ and call. The two methods has the same parameters and the documentation does not say the difference. Refering the source code does not give me any clue of this. But I am guessing that the the __call__ method is inherited from somewhere, and call overrides __call__. If this is the case, why not just use __call__ instead of call in the source code?

Upvotes: 4

Views: 551

Answers (2)

Lawhatre
Lawhatre

Reputation: 1450

The method __call__() does not builds the model. It just carries out the forward pass in the model. When you use call(), it builds the model. You can see the difference when you call model.summary(). The first method will give ValueError while the later will work fine.

There is another method called build(). The best practice is to declare the tf.Variables() inside this method (when you get to know the input shape) instead of __init__() method. This method later builds the model.

Conclusion: If you use call() it will work same as __call__ and additionally, it will also build the model. But if you really want to use the later then, also declare build() along with it.

Upvotes: 1

Zhengkai Tu
Zhengkai Tu

Reputation: 21

I ran into similar problem when studying RNNCell.

It is in Class Layer in base_layer.py that __call__ wraps in call, "applying pre- and post-processing steps".

Upvotes: 2

Related Questions