Environment:

Python 3.12.7 Tensorflow 2.16.1 Keras 3.8.0

Problem:

All optimizers from tf.keras.optimizers do not have a method apply() to write the train routine from scratch. But the docs states that it exists.

AttributeError                            Traceback (most recent call last)
Cell In[11], line 1
----> 1 getattr(tf.keras.optimizers.Optimizer(name="test"), "apply")

AttributeError: 'Optimizer' object has no attribute 'apply'

Possible solution

Need to use method apply_gradients(zip(gradients, model_parameters))

Comment From: dhantule

@konstantin-frolov, thanks for reporting this.

If you're trying to apply gradients to variable you can use apply_gradients(zip(gradients, model_parameters)), you can read more about theapply_gradients method here. If that doesn't work please provide some minimal reproducible code. Attaching gist for reference.

Comment From: rameshdange5191

@konstantin-frolov and @dhantule

  1. Tensorflow v2.16.1's adam docs' View Source on Github (full XPath: /html/body/section/section/main/devsite-content/article/div[3]/div[2]/table/tbody/tr/td/a) links to Keras v3.3.3's adam optimizer. Shouldn't it link to Tensorflow v2.16.1's Keras' adam optimizer?
  2. Keras v3.3.3's base optimizer has the apply method and I didn't find the apply method in Tensorflow v2.16.1's Keras' optimizer_v2.
  3. Also Tensorflow v2.16.1's Keras' docs says the file was autogenerated. Could there be an error in the code that autogenerated the documentation?

Comment From: hertschuh

@konstantin-frolov

The base Optimizer class has an apply method defined here.

I tried to run the guide you linked and it worked for me.

AttributeError                            Traceback (most recent call last)
Cell In[11], line 1
----> 1 getattr(tf.keras.optimizers.Optimizer(name="test"), "apply")

AttributeError: 'Optimizer' object has no attribute 'apply'

How did you produce this error? What were the imports and code beforehand?


@rameshdange5191

  1. The link is correct. Starting from Tensorflow 2.16, tf.keras refers to Keras 3 in the keras repo repo. With Tensorflow 2.15 and earlier, tf.keras was Keras 2, which is now in the tf_keras repo. More info in this page.

  2. The code you linked (under tensorflow/python/keras) is actually never used and should have been removed.

  3. Yes, it is autogenerated from the keras repo, not tensorflow/python/keras.

Comment From: github-actions[bot]

This issue is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.