问题描述
Keras的文档在此处中介绍了如何编写自定义层,方法是从Layer
类继承.现在,我有另一个自定义图层CustomLayer
,我想从中继承该图层.我们将其称为新的自定义图层CustomLayer2
.我猜想,即使我不是从Layer
继承而是从CustomLayer
继承,继承过程也将与链接文档中描述的相同,但是我还将继承CustomLayer
的自定义功能.
The Keras' documentation describes here how to write a custom layer by inheriting from the Layer
class. Now, I have another custom layer CustomLayer
, from which I want to inherit. Let's call my new custom layer CustomLayer2
. I guess that the process of inheriting, even though I will not be inheriting from Layer
but from CustomLayer
, will be the same described in the linked documentation, but I will also inherit the custom functionality of CustomLayer
.
无论如何,在这个自定义层CustomLayer2
内,我想实现一些基于当前纪元或纪元步骤的逻辑.
Anyway, inside this custom layer CustomLayer2
I want to implement some logic that is based on the current epoch or step of the epoch.
我该怎么做?如何从图层的call
和/或build
方法动态获取当前纪元或纪元的步骤?
How can I do that? How can I dynamically get the current epoch or step of the epoch from the call
and/or build
methods of the layer?
也许可以通过回调来完成.例如,我可以有一个回调,该回调访问模型的实例,然后在模型内部进行更改.我不太喜欢这种解决方案,但是如果可行,那就很好.但是我们可以从模型实例更改模型各层的逻辑吗?
Maybe this can be done with callbacks. For example, I could have a callback that accesses an instance of the model and then changes something inside the model. I don't like much this solution, but if it works, that's fine. But can we change the logic of the layers of a model from an instance of a model?
过去,有人问过两个类似的问题
In the past, two similar questions have been asked
- Tensorflow Keras modify model variable from callback
- Can I access what was once `tf.get_global_step()` from within a custom Keras layer?
推荐答案
自定义回调将是前往此处的方法.通常,层逻辑被调用一次以构造计算功能逻辑.稍后将在训练期间使用此计算功能.同样,这通常是在优化程序开始运行之前发生的,因此与时代/步骤无关的事物都没有.
A custom callback would be the way to go here. Generally, layer logic is called once to construct computation function logic; this computational function will later be used during training. This, again, generally, happens before optimizer comes to life and therefore nothing related to epochs/steps is around.
因此,无法从自定义图层中获取训练步骤.
So, there is no way to get training steps from inside a custom layer.
这篇关于如何根据当前纪元或纪元步骤编写某些层的逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!