在一些使用tf2的Tensorflow教程中(例如,具有Attention和Eager要点的神经机器翻译),它们定义了custom tf.keras.Model
而不是tf.keras.layers.Layer
s(例如BahdanauAttention(tf.keras.Model):
)
另外,模型:组成层文档tf.keras.Model
明确使用。本节说:
创建包含其他图层的类似图层的东西时使用的主要类是tf.keras.Model。通过从tf.keras.Model继承来实现一个。
听起来我们需要继承tf.keras.Model
以定义组成子图层的图层。
但是,据我检查,即使我将定义ResnetIdentityBlock
为的子类,此代码也可以使用tf.keras.layers.Layer
。其他两个教程也可以使用Layer
。除此之外,另一个教程说
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.