[Python] AttributeError When Updating Learning Rate in Keras Using K.set_value

Discussão em 'Python' iniciado por Stack, Abril 1, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to update the learning rate of my Keras model dynamically during training. I'm using the following code:

    import tensorflow as tf
    from tensorflow.keras import backend as K

    ...
    ...

    # Change learning rate to 0.001
    K.set_value(autoencoder_model.optimizer.learning_rate, 0.001)


    However, I get this error:

    ---------------------------------------------------------------------------
    AttributeError Traceback (most recent call last)
    <ipython-input-33-76f5edca812e> in <cell line: 0>()
    ----> 1 K.set_value(autoencoder_model.optimizer.learning_rate, 0.001)

    /usr/local/lib/python3.11/dist-packages/keras/src/legacy/backend.py in set_value(x, value)
    1883 def set_value(x, value):
    1884 """DEPRECATED."""
    -> 1885 value = np.asarray(value, dtype=x.dtype.name)
    1886 x.assign(value)
    1887

    AttributeError: 'str' object has no attribute 'name'


    I'm using TensorFlow 2.18.0. I suspect that autoencoder_model.optimizer.learning_rate is not a Keras variable, but I'm not sure how to update it correctly.

    What is the correct way to update the learning rate dynamically in Keras for this version of TensorFlow?

    Thank you for helping in advance!

    Continue reading...

Compartilhe esta Página