简体   繁体   中英

How does call() work for a custom Keras layer?

I am trying to build my own Keras layer by inheriting tf.keras.layers.Layer and I don't really understand what the call() method is doing. I have set my call() method to:

def call(self,inputs): 
    print('call')
    return inputs

When I run the.network, I would expect 'call' to be printed many times (with a training set of 100 examples and 10 epochs I would expect this to be printed 1000 times). However, 'call' is printed once when the model is built, then 3 times during the first epoch and then never again. Is my.network not using this layer in the subsequent epochs? Why is it only being called 3 times in the first epoch despite there being 100 training examples?

Call method automatically decorated by @tf.function. It means that keras builds dataflow graph on the first call and runs this graph on the next calls.

Calling python functions happening only on the first call. See details here - https://www.tensorflow.org/guide/function#debugging .

I am trying to build my own Keras layer by inheriting tf.keras.layers.Layer and I don't really understand what the call() method is doing. I have set my call() method to:

def call(self,inputs): 
    print('call')
    return inputs

When I run the network, I would expect 'call' to be printed many times (with a training set of 100 examples and 10 epochs I would expect this to be printed 1000 times). However, 'call' is printed once when the model is built, then 3 times during the first epoch and then never again. Is my network not using this layer in the subsequent epochs? Why is it only being called 3 times in the first epoch despite there being 100 training examples?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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