4

I have created keras functional API model and now I'm trying to look into it's layers outputs creating submodels for every layer which starts with original model's input and ends with the layer of my choosing. I don't understand what is the proper way to do this without getting

WARNING:tensorflow:11 out of the last 11 calls to 
<function Model.make_predict_function.<locals>.predict_function at 0x7fb8ebc92700> 
triggered tf.function retracing.
Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly 
in a loop, (2) passing tensors with different shapes, 
(3) passing Python objects instead of tensors. 
For (1), please define your @tf.function outside of the loop. 
For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes 
that can avoid unnecessary retracing. 
For (3), please refer to https://www.tensorflow.org/tutorials/customization/performance#python_or_tensor_args 
and https://www.tensorflow.org/api_docs/python/tf/function for  more details.

I don't know where to add @tf.function(experimental_relax_shapes=True) because I'm not defining my own functions which I pass to Keras
I have tried to disable warning with tf.get_logger().setLevel(3) with no success

This is where I get this message:

def extract(layer):
    submodel = keras.Model(inputs = model.input, outputs = layer.output)
    submodel_output = submodel.predict(np.array(X[random_index]).reshape(1, IMG_SIZE, IMG_SIZE, 3))
    
for model_layer in model.layers:
    extract(model_layer)

If there is a way to solve this problem / disable warning (optimization is not the issue) please let me know and thank you in advance.

Mateusz Bugaj
  • 363
  • 1
  • 4
  • 12

1 Answers1

6

It's possible to disable warnings using tf.get_logger().setLevel('ERROR'), as explained in this answer.

Peiffap
  • 163
  • 1
  • 14
Mateusz Bugaj
  • 363
  • 1
  • 4
  • 12