[Python] When saving and loading Keras or TensorFlow models: How do I disable/hide the...

Discussão em 'Python' iniciado por Stack, Novembro 28, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to save and load Keras and/or TensorFlow models, and approaching the problem by following the Keras documentation and a TensorFlow tutorial. In VS Code on my PC, both the native .keras format and the SavedModel format described in the TensorFlow tutorial return errors. However, the following near-minimal example using the legacy .h5 format does complete without errors (this example is adapted from the Keras documentation).

    import keras

    model = keras.Sequential(
    [
    keras.layers.Dense(5, input_shape=(3,)),
    keras.layers.Softmax(),
    ],
    )
    model.save('model.h5')
    loaded_model = keras.saving.load_model('model.h5')


    And yet it also displays two warnings, which become annoying when running the model many times:

    WARNING:absl:You are saving your model as an HDF5 file via model.save() or keras.saving.save_model(model). This file format is considered legacy. We recommend using instead the native Keras format, e.g. model.save('my_model.keras') or keras.saving.save_model(model, 'my_model.keras').

    WARNING:absl:No training configuration found in the save file, so the model was not compiled. Compile it manually.

    The first corresponds to the .save method and the second to the .load_model method.

    How can these warnings be suppressed so that they do not appear on every run?


    For reference, I have tried adding the following without success.

    import os
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

    import tensorflow as tf
    tf.get_logger().setLevel('ERROR')

    import warnings
    warnings.filterwarnings("ignore")

    import tensorflow as tf
    tf.keras.config.disable_interactive_logging()

    Continue reading...

Compartilhe esta Página