tf_keras==2.20.1

self._model = TFDistilBertForTokenClassification.from_pretrained('models/NameEntityRecognition', num_labels=len(self._unique_tags))
self._model.compile(optimizer=Adam(learning_rate=self._learning_rate), loss=self._model.hf_compute_loss, metrics=['accuracy'])

I have tried os.environ['TF_USE_LEGACY_KERAS'] = '1' but to no avail.

  File "/usr/src/Python/pAIthon/NameEntityRecognition.py", line 52, in BuildTrainModel
    self._model.compile(optimizer=Adam(learning_rate=self._learning_rate), loss=self._model.hf_compute_loss, metrics=['accuracy']) # can also use any keras loss fn
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/khteh/.local/share/virtualenvs/pAIthon-GaqEDHQT/lib/python3.13/site-packages/transformers/modeling_tf_utils.py", line 1547, in compile
    super().compile(
    ~~~~~~~~~~~~~~~^
        optimizer=optimizer,
        ^^^^^^^^^^^^^^^^^^^^
    ...<6 lines>...
        **kwargs,
        ^^^^^^^^^
    )
    ^
  File "/home/khteh/.local/share/virtualenvs/pAIthon-GaqEDHQT/lib/python3.13/site-packages/tf_keras/src/utils/traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/home/khteh/.local/share/virtualenvs/pAIthon-GaqEDHQT/lib/python3.13/site-packages/tf_keras/src/optimizers/__init__.py", line 335, in get
    raise ValueError(
        f"Could not interpret optimizer identifier: {identifier}"
    )
ValueError: Could not interpret optimizer identifier: <keras.src.optimizers.adam.Adam object at 0x7e2fc0491010>

Comment From: dhantule

Hi @khteh, please provide some reproducible code to reproduce this issue

Comment From: khteh

https://github.com/khteh/pAIthon/blob/master/NameEntityRecognition.py

Comment From: monicadsong

Hi @khteh --

The Keras team would like to learn more about your Keras usage. Please thumbs up this comment if you are open to speaking with us and we'll reach out to you over email.

Thank you so much! Monica, Keras Product Manager

Comment From: dhantule

Hi @khteh,

Please try using optimizer = tf_keras.optimizers.Adam(learning_rate=self._learning_rate) and update the model compile line with this self._model.compile(optimizer=optimizer, loss=self._model.hf_compute_loss, metrics=['accuracy']), please check if you have Keras 3 installed in your environment and uninstall it which may be causing this issue.

If this doesn't work please provide only the relevant parts of code in a colab gist that reproduces the error. Thanks!

Comment From: khteh

How different is your suggestion from my post? No, only tf_keras==2.20.1.

Comment From: dhantule

Hi @khteh,

I've created a small example that reproduces this error, I was able to fix this by ensuring the Adam is explicitly imported from tf_keras.optimizers, to resolve your error correct the import and use from tf_keras.optimizers import Adam . Please refer this attached gist.

Comment From: khteh

I have from tensorflow.keras.optimizers import Adam in around 20 .py module files and they never cause me any problem / error. Why is this different?

Comment From: dhantule

Did you face this issue after the upgrading to latest tf_keras?

Comment From: khteh

No but importing from this different module is inconsistent with my other python module code and it concerns me.

Comment From: dhantule

@khteh, can you provide a small reproducible script possibly in a colab? I can't reproduce the issue with the link you sent me.

Comment From: khteh

Not sure what you mean and why do you need that?