简体   繁体   English

TF Keras v 1.14+:“模块”的子类模型或子类层

[英]TF Keras v 1.14+: subclass model or subclass layer for "module"

Tensorflow has some docs for subclassing (tf) Keras Model and Layer . Tensorflow 有一些用于子类化 (tf) Keras ModelLayer文档

However, it is unclear which to use for "modules" or "blocks" (eg several layers collectively).然而,尚不清楚哪个用于“模块”或“块”(例如,几个层的总称)。

Since it is technically several layers, I feel that subclassing Layer would be deceiving, and while subclassing Model works, I am unsure if there are any negative penalties for doing so.由于它在技术上是几个层,我觉得子类化Layer会欺骗,虽然子类化Model有效,我不确定这样做是否有任何负面惩罚。

eg例如

x = inputs
a = self.dense_1(x) # <--- self.dense_1 = tf.keras.Dense(...)
b = self.dense_2(a)
c = self.add([x, b])

which is appropriate to use?哪个适合使用?

Initially, there is no need to sublass anything with Keras.最初,不需要使用 Keras 将任何内容转为 sublass。 Unless you have a particular reason for that (which is not building, training, predicting), you don't subclass for Keras.除非您有特殊原因(不是构建、训练、预测),否则您不会为 Keras 进行子类化。

Buiding a Keras model:构建 Keras 模型:

Either using Sequential (the model is ready already, just add layers), or using Model (create a graph with layers and finally call Model(inputs, outputs) ), you don't need to subclass.无论是使用Sequential (模型已经准备好,只需添加层),或者使用Model (创建一个带有层的图形,最后调用Model(inputs, outputs) ),您都不需要子类化。

The moment you create an instance of Sequential or Model , you have a fully defined model, ready to use in all situations.当您创建SequentialModel的实例时,您就有了一个完全定义的模型,可以在所有情况下使用。

This model can even be used as parts of other models, its layers can be easily accessed to get intermetiate outputs and create new branches in your graph.该模型甚至可以用作其他模型的一部分,可以轻松访问其层以获取中间输出并在图形中创建新分支。

So, I don't see any reason at all to subclass Model , unless you are using some additional framework that would require this (but I don't think so).所以,我根本看不出有任何理由将Model子类化,除非您使用一些需要这样做的额外框架(但我不这么认为)。 This seems to be something from PyTorch users (because this kind of model building is typical for PyTorch, create a subclass for Module and add layers and a call method).这似乎是来自 PyTorch 用户的东西(因为这种模型构建是 PyTorch 的典型做法,为Module创建一个子类并添加层和调用方法)。 But Pytorch doesn't offer the same ease as Keras does for getting intermediate results.但是 Pytorch 不像 Keras 那样容易获得中间结果。

The main advantage of using Keras is exactly this: you can easily access layers from blocks and models and instantly start branching from that point without needing to rebuild any call methods or adding any extra code for that in the models.使用 Keras 的主要优势正是这样:您可以轻松地从块和模型访问层,并立即从该点开始分支,而无需重建任何调用方法或在模型中为此添加任何额外代码。 So, when you subclass Model, you just defeat the purpose of Keras making it all more difficult.因此,当您对 Model 进行子类化时,您只会破坏 Keras 使其变得更加困难的目的。

The docs you mentioned say:您提到的文档说:

Model subclassing is particularly useful when eager execution is enabled since the forward pass can be written imperatively.当启用急切执行时,模型子类化特别有用,因为可以命令性地编写前向传递。

I don't really understand what "imperatively" means, and I don't see how it would be easier than just building a model with regular layers.我真的不明白“命令式”是什么意思,而且我看不出这比仅构建具有常规层的模型更容易。

Another quote from the docs:文档中的另一个引用:

Key Point: Use the right API for the job.关键点:为工作使用正确的 API。 While model subclassing offers flexibility, it comes at a cost of greater complexity and more opportunities for user errors.虽然模型子类化提供了灵活性,但代价是更高的复杂性和更多的用户错误机会。 If possible, prefer the functional API.如果可能,更喜欢函数式 API。

Well... it's always possible.嗯……总是有可能的。

Subclassing layers子类化层

Here, there may be good reasons to do so.在这里,可能有充分的理由这样做。 And these reasons are:这些原因是:

  • You want a layer that performs custom calculations that are not available with regular layers您需要一个执行常规图层不可用的自定义计算的图层
  • This layer must have persistent weights.该层必须具有持久权重。

If you don't need "both" things above, you don't need to subclass a layer.如果您不需要上面的“两者”,则不需要对图层进行子类化。 If you just want "custom calculations" without weights, a Lambda layer is enough.如果您只想要没有权重的“自定义计算”, Lambda层就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM