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
model = keras.models.Sequential([keras.layers.Dense(10)])
model.compile(keras.optimizers.SGD(), loss='mse')
# Change learning rate to 0.001
K.set_value(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(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.
What is the correct way to update the learning rate dynamically in Keras for this version of TensorFlow?
Thank you for helping in advance!