How can I write some layer's logic based on the current epoch or step of epoch?

谁说胖子不能爱 提交于 2020-04-30 06:31:52

问题


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.

Anyway, inside this custom layer CustomLayer2 I want to implement some logic that is based on the current epoch or step of the epoch.

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?

回答1:


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.



来源:https://stackoverflow.com/questions/61329343/how-can-i-write-some-layers-logic-based-on-the-current-epoch-or-step-of-epoch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!