TensorFlow:设计自定义层时__call __()和call()的功能之间的区别? [英] TensorFlow: Difference between functioning of __call__() and call() when designing custom layers?
问题描述
我在TensorFlow中设计了带有自定义层的神经网络.首先,我使用 __ call __()
定义前向传递,然后调用 model.summary()
.这给了我以下错误
I've designed a neural network with a custom layer in TensorFlow. Firstly, I used __call__()
to define the forward pass and then I called the model.summary()
. This gave me following error
ValueError: This model has not yet been built. Build the model first by calling `build()` or calling `fit()` with some data, or specify an `input_shape` argument in the first layer(s) for automatic build.
但是,当我用 call()
替换它时,它工作得很好.
However, when I replaced it with call()
, It worked just fine.
问:我想了解幕后情况.
Q: I want to understand what is going on under the hood.
我尚未创建 build()
并已在 __ init __()
推荐答案
您遇到的 ValueError
是合理的.在此处并且 call
和 __ call __
之间的区别也已经通过链接).
The ValueError
you've faced is reasonable. This is asked and get answered already over here And also the difference between call
and __call__
is also answered already over here;(as you didn't give any code to reproduce so I'm referring to this link). However, FYI, I've written an article about model subclassing and custom training from scratch, I hope the modeling approach part may give you some insight (the article link).
评论部分中的问题太多,所以我想在这里添加我的回答.基本上, __ call __
方法在 Layer
类.这个图层类是由 Model
类继承的.比方说
Too many questions in the comment section, so I like to add my response here. Basically, the __call__
method is implemented in the Layer
class. And this layer class is inherited by the Model
class. Let's say
import inspect
from tensorflow.keras.models import Model
# inspect.getmro(CLASS) returns a tuple of class CLASS's base classes,
# including CLASS, in Method Resolution Order (MRO).
inspect.getmro(Model)
# out
(tensorflow.python.keras.engine.training.Model,
tensorflow.python.keras.engine.base_layer.Layer,...)
现在,在 Layer
类的 __ call __
方法中,已分配此处>.如:
Now, in __call__
method in the Layer
class has assigned self.call
method to call_fn
. Which is further used here. Such as:
def __call__(self, input):
call_fn = self.call
outputs = call_fn(input, ...)
return outputs
因此,当我们调用 Model
类实例时,它的继承的 __ call __
方法(来自 Layer
类)将自动执行,这将调用我们的自己的 call
方法.
So when we call our Model
class instance, it's inherited __call__
method (from Layer
class) will be executed automatically, which calls our own call
method.
模型摘要
使用子类化API进行建模与顺序或功能性API不同.它不是图网络,因此只有包含模型名称的节点才会用这些模型绘制.我们真的不能对子类模型的结构做任何假设.这就是为什么我们无法获得输出形状.但是,有一种解决方法可以方便地获得模型形状汇总的工具并对其进行绘制.在此处中进行检查.
The modeling with Subclassing API is not as same as Sequential or Functional API. It is not graph networks and therefore only a node containing the model name would be plotted with these models. We can't really assume anything about the structure of a subclassed Model. That's why we can't get the output shape. However, there's a workaround to get the facilities of model shape summary and plot it either conveniently. Check here.
这篇关于TensorFlow:设计自定义层时__call __()和call()的功能之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!