-
Notifications
You must be signed in to change notification settings - Fork 19.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
apply_gradients AttributeError: 'ResourceVariable' object has no attribute 'overwrite_with_gradient' #20517
Comments
Could you please provide some sample reproducible script to replicate the reported behavior. Thanks! |
@sachinprasadhs Sure, please try this cut down simple example showing the problem:
|
@andrewl36 import numpy as np
import tensorflow as tf
import keras
class MyModel(keras.Model):
def __init__(self):
super().__init__()
# Keras model Layers
self.hidden_layers = [
keras.layers.Dense(32, activation="tanh") for _ in range(2)
]
self.output_layer = keras.layers.Dense(1)
# Custom variable
self.my_var = self.add_weight(shape=(), dtype="float32", name="my_var")
self.my_var.assign(0.1)
def call(self, inputs):
x = inputs
for layer in self.hidden_layers:
x = layer(x)
return self.output_layer(x)
data = np.array(
[
[0.0, 10.4],
[900.0, 21.1],
[3900.0, 64.2],
]
)
model = MyModel()
inputs = data[:, 0:1]
outputs = data[:, 1:]
epochs = 1000
learning_rate = 0.005
optimizer = keras.optimizers.Adam(learning_rate=learning_rate)
pbar = keras.utils.Progbar(epochs)
for epoch in range(epochs):
with tf.GradientTape() as tp:
y_pred = model(inputs)
loss = tf.reduce_mean(tf.square((outputs - y_pred)))
gradients = tp.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
values = {"loss": loss.numpy()}
pbar.add(1, values.items()) The key is to use EDITED: |
@james77777778 thank you, yes that does work now, cheers |
When I have a mix of tf.Variable and KerasVariables I get the following error:
I suspect this is because my list of variables is [KerasVariables] + [tf.Variables]
and the following line only checks the first in the list as to whether overwrite_with_gradient can be used?
keras/keras/src/optimizers/base_optimizer.py
Line 675 in 660da94
The text was updated successfully, but these errors were encountered: