In some Tensorflow tutorials with tf2 (e.g. Neural Machine Translation with Attention and Eager essentials), they define custom tf.keras.Model
s instead of tf.keras.layers.Layer
s (e.g. BahdanauAttention(tf.keras.Model):
)
Also, Models: composing layers doc uses tf.keras.Model
explicitly. The section says:
The main class used when creating a layer-like thing which contains other layers is tf.keras.Model. Implementing one is done by inheriting from tf.keras.Model.
It sounds we need to inherit tf.keras.Model
to define a layer which compose child layers.
However, as far as I checked, this code works even if I define ResnetIdentityBlock
as a child class of tf.keras.layers.Layer
. Other two tutorials work with Layer
too.
In addition to that, another tutorial says
Model is just like a Layer, but with added training and serialization utilities.
Thus, I have no idea what is the real difference between tf.keras.Model
and tf.keras.layers.Layer
and why those three tutorial with Eager execution uses tf.keras.Model
though they don't use training and serialization utilities of tf.keras.Model
.
Why do we need to inherit tf.keras.Model
in those tutorials?
Additional comment
utilities of Model
work only with special subsets of Layer
(Layers whose call
receive only one input). Thus, I think the idea like "Always extend Model because Model has more features" is not correct. Also, it violates a basic programming program like SRP.