0

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

nbro
  • 15,395
  • 32
  • 113
  • 196

1 Answers1

0

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.

y.selivonchyk
  • 8,987
  • 8
  • 54
  • 77