Cannot load_model() for Functional model if compiled with tf.keras.optimizer.get(optimizer_config)
Hey! I might a little solution for you. Hope it helps There are two ways to solve this error choose which one You like

1. `
custom_optimizer = tf.keras.optimizers.RMSprop(learning_rate=0.001)
 model.compile(optimizer=custom_optimizer, loss='binary_crossentropy', metrics=['accuracy'])
`
just use this code and it will work great. 

2. Another way is to use this 

```
> optimizer_config = {'class_name': 'rmsprop', 'config': {'lr': 0.0001}}
> model.compile(optimizer=tf.keras.optimizers.get(optimizer_config), loss='binary_crossentropy', metrics=['accuracy'])
> 

> model.fit(X_train, y_train, epochs=10)
> 
>
> save_path = '/content/'
> model.save(save_path + 'my_model',save_format='tf')
> 
> 
> loaded_model = tf.keras.models.load_model(save_path + 'my_model')
```

instead of using save_format=keras , use tf it will work fine 


Thanks. Thanks a lot! This kind of works for me, but our ideal solution would keep saving the model in the `.keras` format but still being able to define the optimizer from a config file just from a string, without having to manually instantiate the RMSprop class. Hi @alvaro-stylesage ,

I have replicated the issue with TF2.14 version.

 With latest keras3 nightly version getting the value error as the API parameters got changed.

`ValueError: Unrecognized keyword arguments passed to Embedding: {'input_length': 5}`

However with tf-keras-nightly which is replacement of tf.keras getting another error like below.

`ValueError: Could not interpret optimizer identifier: <keras.src.optimizers.rmsprop.RMSprop object at 0x7879100aa440>`

Attaching [gist](https://colab.sandbox.google.com/gist/SuryanarayanaY/01ec12ec3ffbe02b9a61bbd62996ad52/62317.ipynb) for above testings.

This needs to be digged more and will get back you once we find out root cause. Thanks! Hi @alvaro-stylesage ,

This is not an issue with TF2.15v. Please refer attached [gist](https://colab.sandbox.google.com/gist/SuryanarayanaY/77e44152e3f0b1c43a3be7e46c8e8378/62317_tf-2-15.ipynb). Could you verify and confirm. Thanks! Even with tf-nightly+keras-nightly also its working fine.

Changes needed: `'lr' `should be renamed to `learning_rate` as it is deprecated in Keras3.The Embedding layer of Keras3 has no argument `input_length`. It will be automatically inferred from the input or you can also pass it to argument 
`input_shape = (batch_size,input_length)`.

Tested with the above changes and it works fine as per attached [gist](https://colab.sandbox.google.com/gist/SuryanarayanaY/c681b1fe35d95ea8b85ed469dfe144b8/62317_tf-nightly.ipynb). Thanks! This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/62317">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/62317">No</a>

### Issue type

Bug

### Have you reproduced the bug with TensorFlow Nightly?

Yes

### Source

source

### TensorFlow version

2.13

### Custom code

Yes

### OS platform and distribution

Linux Ubuntu 22.04

### Mobile device

_No response_

### Python version

3.9.5

### Bazel version

_No response_

### GCC/compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current behavior?

We are creating a Functional model object that contains a `TextVectorization()` layer and some `Dense` layers. The layer is adapted with the `.adapt()` method before training, the model is trained correctly and then saved correctly using the `.save(save_format='keras')` method.

For the training the optimizer is passed with `optimizer=tf.keras.optimizers.get(optimizer_config)` . Then when the model is saved and loaded again with the `tf.keras.models.load_model()` method this error raises:

`AttributeError: 'RMSprop' object has no attribute 'build'`

However, if the optimizer is defined as `optimizer='rmsprop' `, the error is not raised.


### Standalone code to reproduce the issue

```shell
import tensorflow as tf
from tensorflow import keras
import pandas as pd

from keras.layers import Input, TextVectorization, Embedding, GlobalAveragePooling1D, Dense
from keras.models import Model

train_df = pd.DataFrame({'review':['This is shit', 'I hate this', 'Great I love this!!!'],
                         'label': [0, 0, 1]})
X_train = train_df['review'].values
y_train = train_df['label'].values

# Define a Functional model:
vectorizer = TextVectorization(max_tokens=20, output_mode='int', output_sequence_length=5, pad_to_max_tokens=True)
vectorizer.adapt(X_train)

text_input = Input(shape=(1,), dtype=tf.string, name='text_input')
vectorizer_text = vectorizer(text_input)
embedding = Embedding(input_dim=20, input_length=5, output_dim=100)(vectorizer_text)
pooled = GlobalAveragePooling1D()(embedding)
output = Dense(1, activation='sigmoid', name='output')(pooled)

# Create the functional model
model = Model(inputs=text_input, outputs=output)

# Compile
optimizer_config = {'class_name': 'rmsprop', 'config': {'lr': 0.0001}}
model.compile(optimizer=tf.keras.optimizers.get(optimizer_config), loss='binary_crossentropy', metrics=['accuracy'])

# Train the model
model.fit(X_train, y_train, epochs=10)

# Save the model:
save_path = '/content/'
model.save(save_path + 'my_model.keras')

# Load the model:
loaded_model = tf.keras.models.load_model(save_path + 'my_model.keras')
```


### Relevant log output

```shell
AttributeError                            Traceback (most recent call last)

<ipython-input-61-3abc12e5f733> in <cell line: 2>()
      1 # Load the model:
----> 2 loaded_model = tf.keras.models.load_model(save_path + 'my_model.keras')

6 frames

/usr/local/lib/python3.10/dist-packages/keras/src/optimizers/legacy/optimizer_v2.py in __getattribute__(self, name)
    985         """Overridden to support hyperparameter access."""
    986         try:
--> 987             return super().__getattribute__(name)
    988         except AttributeError as e:
    989             # Needed to avoid infinite recursion with __setattr__.

AttributeError: 'RMSprop' object has no attribute 'build'
```
