philipperemy/keras-tcn

[tf2.4] Saving a model: WARNING:absl:Found untraced functions[...]

Luux opened this issue · 7 comments

Luux commented

Describe the bug
During saving a *.tf model with tensorflow 2.4, the following warnings occur:

WARNING:absl:Found untraced functions such as residual_block_0_layer_call_and_return_conditional_losses, residual_block_0_layer_call_fn, residual_block_1_layer_call_and_return_conditional_losses, residual_block_1_layer_call_fn, residual_block_2_layer_call_and_return_conditional_losses while saving (showing 5 of 400). These functions will not be directly callable after loading.
WARNING:absl:Found untraced functions such as residual_block_0_layer_call_and_return_conditional_losses, residual_block_0_layer_call_fn, residual_block_1_layer_call_and_return_conditional_losses, residual_block_1_layer_call_fn, residual_block_2_layer_call_and_return_conditional_losses while saving (showing 5 of 400). These functions will not be directly callable after loading.

These do not occur with tf2.3.

Paste a snippet

import tensorflow as tf
import numpy as np
from tcn import TCN

model_fn = "test.tf"

max_features = 64

max_len = 128

inputs = tf.keras.layers.Input(shape=(max_features, max_len))
inputs = tf.keras.layers.Input(shape=(max_features, max_len))
tcn = TCN(
    nb_filters=64,
    name="tcn1",
)(inputs)
dense = tf.keras.layers.Dense(units=1, activation="sigmoid")(tcn)
model = tf.keras.Model(inputs=inputs, outputs=dense)
model.compile(optimizer="adam")

print("Save model...")
model.save(model_fn)

print("Load model...")
model = tf.keras.models.load_model(
    # model_fn, custom_objects={"TCN": TCN, "CTCLayer": CTCLayer}, compile=False,
    model_fn
)

model.compile(optimizer="adam")

model.summary()

inputs = np.ones(shape=(1, 64, 128))
out2 = model.predict(inputs)
print(out2)

Dependencies
tensorflow==2.4.1
keras-tcn @ commit 455a035

Luux commented

Note that LSTMs currently face a very similar issue: tensorflow/tensorflow#47554

I have just started training a model that includes a TCN layer (see: here), and get these warnings:

WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x1520ad6f83c8>, because it is not built.
2021-12-02 12:08:44.140500: W tensorflow/python/util/util.cc:348] Sets are not currently considered sequences, but this may change in the future, so consider avoiding using them.
WARNING:absl:Found untraced functions such as residual_block_0_layer_call_and_return_conditional_losses, residual_block_0_layer_call_fn, residual_block_1_layer_call_and_return_conditional_losses, residual_block_1_layer_call_fn, residual_block_2_layer_call_and_return_conditional_losses while saving (showing 5 of 240). These functions will not be directly callable after loading.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x1520ad6f83c8>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x1520ad6f83c8>, because it is not built.

Is it safe to ignore these warnings? Or will it give me troubles when loading the model in a later stage?

Best,
Robbin

Luux commented

@rmndrs89 According to what others write in the linked issue, unexpected things such as broken models might happen. I invite you to join the 36 participants on their epic journey of waiting for some response of the tf devs...

@rmndrs89 All the Lambda layers used in the keras-tcn module do not contain trainable parameters. So I would say that you can safely ignore them. It would be more "dangerous" if some Lambda contained trainable parameters and could not be properly serialized.

My guess is: The WARNING results from this layer https://github.com/philipperemy/keras-tcn/blob/master/tcn/tcn.py#L294 not being built beforehand.

So in the version 3.4.1, I built the Lambda layer beforehand.

The first few warnings are harmless.

I will close this issue but feel free to re-open it should the warnings related to the Lambda subsist.

Thanks!