'Adam' object has no attribute 'build' (saving and loading keras.optimizers.Adam)
same problem reported with Keras here: https://github.com/keras-team/tf-keras/issues/46 @palminha Thank you for raising this issue!
@SuryanarayanaY I was able to replicate the issue on Macbook, please find the attached screenshot below;
<img width="564" alt="image (3)" src="https://github.com/tensorflow/tensorflow/assets/84765720/a478b0ef-5b2c-44a7-a790-578da03286d7">

I was able to run the coed successfully on colab, please have a look at this [gist](https://colab.research.google.com/gist/sushreebarsa/695b5adea44465c59315c78154a787c1/61915.ipynb). Thank you! Hi, I am Debrup. I want to work on this problem can I be pleased assigned to the problem? @ palminha,

For MacOS we need to import optimizers from legacy space like keras.optimizers.legacy.Adam. I tried explicitly replacing the Adam with legacy .Adam and still its not working. Since there is no problem with Linux and Macos package was built and maintained by Apple itself, you need to report the issue at  Apple developer forum [here](https://developer.apple.com/forums/tags/tensorflow-metal/).

Meanwhile I will also take it to the attention of concerned engineer.

Thank you! CC - @nitins17 ,Do you have any pointers here?

Thanks! @SuryanarayanaY Here is my machine the dependency graph: in my case i'm not using `tensorflow-metal`... i'm using the package `tensorflow` (that installs as dependency the `tensorflow-macos`)

```
tensorflow==2.13.0
└── tensorflow-macos [required: ==2.13.0, installed: 2.13.0]
    ├── absl-py [required: >=1.0.0, installed: 2.0.0]
    ├── astunparse [required: >=1.6.0, installed: 1.6.3]
    │   ├── six [required: >=1.6.1,<2.0, installed: 1.16.0]
    │   └── wheel [required: >=0.23.0,<1.0, installed: 0.41.2]
    ├── flatbuffers [required: >=23.1.21, installed: 23.5.26]
    ├── gast [required: >=0.2.1,<=0.4.0, installed: 0.4.0]
    ├── google-pasta [required: >=0.1.1, installed: 0.2.0]
    │   └── six [required: Any, installed: 1.16.0]
    ├── grpcio [required: >=1.24.3,<2.0, installed: 1.58.0]
    ├── h5py [required: >=2.9.0, installed: 3.9.0]
    │   └── numpy [required: >=1.17.3, installed: 1.23.5]
    ├── keras [required: >=2.13.1,<2.14, installed: 2.13.1]
    ├── libclang [required: >=13.0.0, installed: 16.0.6]
    ├── numpy [required: >=1.22,<=1.24.3, installed: 1.23.5]
    ├── opt-einsum [required: >=2.3.2, installed: 3.3.0]
    │   └── numpy [required: >=1.7, installed: 1.23.5]
    ├── packaging [required: Any, installed: 23.1]
    ├── protobuf [required: >=3.20.3,<5.0.0dev,!=4.21.5,!=4.21.4,!=4.21.3,!=4.21.2,!=4.21.1,!=4.21.0, installed: 3.20.3]
    ├── setuptools [required: Any, installed: 68.2.2]
    ├── six [required: >=1.12.0, installed: 1.16.0]
    ├── tensorboard [required: >=2.13,<2.14, installed: 2.13.0]
    │   ├── absl-py [required: >=0.4, installed: 2.0.0]
    │   ├── google-auth [required: >=1.6.3,<3, installed: 2.23.0]
    │   │   ├── cachetools [required: >=2.0.0,<6.0, installed: 5.3.1]
    │   │   ├── pyasn1-modules [required: >=0.2.1, installed: 0.3.0]
    │   │   │   └── pyasn1 [required: >=0.4.6,<0.6.0, installed: 0.5.0]
    │   │   ├── rsa [required: >=3.1.4,<5, installed: 4.9]
    │   │   │   └── pyasn1 [required: >=0.1.3, installed: 0.5.0]
    │   │   └── urllib3 [required: <2.0, installed: 1.26.16]
    │   ├── google-auth-oauthlib [required: >=0.5,<1.1, installed: 1.0.0]
    │   │   ├── google-auth [required: >=2.15.0, installed: 2.23.0]
    │   │   │   ├── cachetools [required: >=2.0.0,<6.0, installed: 5.3.1]
    │   │   │   ├── pyasn1-modules [required: >=0.2.1, installed: 0.3.0]
    │   │   │   │   └── pyasn1 [required: >=0.4.6,<0.6.0, installed: 0.5.0]
    │   │   │   ├── rsa [required: >=3.1.4,<5, installed: 4.9]
    │   │   │   │   └── pyasn1 [required: >=0.1.3, installed: 0.5.0]
    │   │   │   └── urllib3 [required: <2.0, installed: 1.26.16]
    │   │   └── requests-oauthlib [required: >=0.7.0, installed: 1.3.1]
    │   │       ├── oauthlib [required: >=3.0.0, installed: 3.2.2]
    │   │       └── requests [required: >=2.0.0, installed: 2.31.0]
    │   │           ├── certifi [required: >=2017.4.17, installed: 2023.7.22]
    │   │           ├── charset-normalizer [required: >=2,<4, installed: 3.2.0]
    │   │           ├── idna [required: >=2.5,<4, installed: 3.4]
    │   │           └── urllib3 [required: >=1.21.1,<3, installed: 1.26.16]
    │   ├── grpcio [required: >=1.48.2, installed: 1.58.0]
    │   ├── markdown [required: >=2.6.8, installed: 3.4.4]
    │   ├── numpy [required: >=1.12.0, installed: 1.23.5]
    │   ├── protobuf [required: >=3.19.6, installed: 3.20.3]
    │   ├── requests [required: >=2.21.0,<3, installed: 2.31.0]
    │   │   ├── certifi [required: >=2017.4.17, installed: 2023.7.22]
    │   │   ├── charset-normalizer [required: >=2,<4, installed: 3.2.0]
    │   │   ├── idna [required: >=2.5,<4, installed: 3.4]
    │   │   └── urllib3 [required: >=1.21.1,<3, installed: 1.26.16]
    │   ├── setuptools [required: >=41.0.0, installed: 68.2.2]
    │   ├── tensorboard-data-server [required: >=0.7.0,<0.8.0, installed: 0.7.1]
    │   ├── werkzeug [required: >=1.0.1, installed: 2.3.7]
    │   │   └── MarkupSafe [required: >=2.1.1, installed: 2.1.3]
    │   └── wheel [required: >=0.26, installed: 0.41.2]
    ├── tensorflow-estimator [required: >=2.13.0,<2.14, installed: 2.13.0]
    ├── termcolor [required: >=1.1.0, installed: 2.3.0]
    ├── typing-extensions [required: >=3.6.6,<4.6.0, installed: 4.5.0]
    └── wrapt [required: >=1.11.0, installed: 1.15.0]
``` @nitins17 @debrupf2946 @sushreebarsa ... 

@SuryanarayanaY i don't think this is a problem with `tensorflow-metal` since I'm not using that package. The problem happens when using `tensorflow` -> `tensorflow-macos` packages Hi @palminha ,

On Arm M1, even if you use command pip install tensorflow the package installed is still Apple's package.Please find the source here.

https://github.com/tensorflow/tensorflow/blob/f841394b1b714c5cc5366536411cf146c8c570df/tensorflow/tools/pip_package/setup.py#L153-L156

If you haven't installed tensorflow-metal then you must install it since Mac package needs it to work with GPU as the above code in setup.py installs Apples package automatically if the` platform_system=="Darwin" and platform_machine=="arm64"` . Please refer to the instructions of Apple metal plugin [here](https://developer.apple.com/metal/tensorflow-plugin/).

Thanks! > tensorflow==2.13.0
> └── tensorflow-macos [required: ==2.13.0, installed: 2.13.0]

HI @SuryanarayanaY, if you check my `pipenv` dependency graph, you can see that I have `tensorflow-macos` and not `tensorflow-metal`. And I don't need to install` tensorflow-metal` to make it work...  I know I won't take advantage of GPU, but nevertheless, it can run just with `tensorflow-macos`

 Hi @palminha ,

Tensorflow officially supports CPU only wheels which can be installable on Macos with explicit command `pip install tensorflow-macos`.This wheel is mainly intended for Intel Chip machines of Apple.

As confirmed in my above comment if you try `pip install tensorflow` and the machine is Darwin and arm64 it will install the Apple GPU package automatically as per my knowledge.With that package you might need tensorflow-metal for sure. This development happened since TF2.12v. However you can cross check the same with Apple team itself regarding whether the package installed is GPU or CPU since these packages or built and installed by Apple itself.

```
# Install the TensorFlow package built by Apple if the user is running 
# macOS on an Apple Silicon machine. 
 standard_or_nightly('tensorflow-macos', 'tf-nightly-macos') + '==' + 
 _VERSION + ';platform_system=="Darwin" and platform_machine=="arm64"', 
```

I request you to take confirmation from Apple dev [forum](https://developer.apple.com/forums/tags/tensorflow-metal/).

Thank you! @palminha ,

**Update**:  The issue got fixed in tf-nightly version. Please refer to attached logs below. Kindly note that I have metal-plugin installed so that works fine with GPU support.I am curious to know whether it works for you with CPU only.

```
(base) suryanarayanay-macbookpro:Downloads suryanarayanay$ python 61915_macos_new_optimzer.py
2.15.0.dev2023092207
WARNING:absl:At this time, the v2.11+ optimizer `tf.keras.optimizers.Adam` runs slowly on M1/M2 Macs, please use the legacy Keras optimizer instead, located at `tf.keras.optimizers.legacy.Adam`.
Metal device set to: Apple M1 Pro

systemMemory: 16.00 GB
maxCacheSize: 5.33 GB

2023-09-28 14:53:58.925275: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:306] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.
2023-09-28 14:53:58.925303: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:272] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)
WARNING:absl:At this time, the v2.11+ optimizer `tf.keras.optimizers.Adam` runs slowly on M1/M2 Macs, please use the legacy Keras optimizer instead, located at `tf.keras.optimizers.legacy.Adam`.
WARNING:absl:Skipping variable loading for optimizer 'Adam', because it has 9 variables whereas the saved optimizer has 1 variables. 
End of program
(base) suryanarayanay-macbookpro:Downloads suryanarayanay$ 
``` @SuryanarayanaY  i just tested with tf-nightly with CPU only version
```
% pipenv graph
...
tf-nightly==2.15.0.dev20230928
└── tf-nightly-macos [required: ==2.15.0-dev20230928, installed: 2.15.0.dev20230928]
```


it seems it solves the error
```
% pipenv run adam
WARNING:absl:At this time, the v2.11+ optimizer `tf.keras.optimizers.Adam` runs slowly on M1/M2 Macs, please use the legacy Keras optimizer instead, located at `tf.keras.optimizers.legacy.Adam`.
WARNING:absl:At this time, the v2.11+ optimizer `tf.keras.optimizers.Adam` runs slowly on M1/M2 Macs, please use the legacy Keras optimizer instead, located at `tf.keras.optimizers.legacy.Adam`.
WARNING:absl:Skipping variable loading for optimizer 'Adam', because it has 9 variables whereas the saved optimizer has 1 variables. 
``` @palminha ,

Thanks for confirmation. That means same package works for both. But to enable GPU support on Mac we need to install metal-plugin additionally and that works with GPU support as well.

Can we mark it as resolved now. Please feel free to close the issue if you don't have further queries on this.

Thank you!
  @SuryanarayanaY ... keras team closed the related issue: https://github.com/keras-team/tf-keras/issues/46 > @ palminha,
> 
> For MacOS we need to import optimizers from legacy space like keras.optimizers.legacy.Adam. I tried explicitly replacing the Adam with legacy .Adam and still its not working. Since there is no problem with Linux and Macos package was built and maintained by Apple itself, you need to report the issue at Apple developer forum [here](https://developer.apple.com/forums/tags/tensorflow-metal/).
> 
> Meanwhile I will also take it to the attention of concerned engineer.
> 
> Thank you!

Instead of the legacy.Adam, the tf.keras.optimizers.Adam() works well and there is no Error of 
"'Adam' object has no attribute 'build' ". Even though it may impact the running performance. I have not found any obvious performance impact from my personal test project with CNN. I have not had any comparing tests yet. 
I ignored the warning while training: "WARNING:absl:At this time, the v2.11+ optimizer `tf.keras.optimizers.Adam` runs slowly on M1/M2 Macs, please use the legacy Keras optimizer instead, located at `tf.keras.optimizers.legacy.Adam`."

Environment: OS: Macos, CPU: M2, Python3: 3.11.5, tensorflow: 2.14.0
 I have tensorflow 2.14, I use _tf.keras.optimizers.Adam_ but I still have the issue, when I try to load the model with pickle. :-/

Environment: OS: Macos, CPU: M2 Pro, Python 3.9.6, tensorflow: 2.14.0  @gszecsenyi it will be solved in version 2.15 The issue still exists with 2.15rc1.  > @gszecsenyi it will be solved in version 2.15

That's true.
`pip install tensorflow-macos==2.15.0`
solves this error.

I'm using M2 Max mac. @palminha ,

The issue fixed with this [PR](https://github.com/keras-team/keras/pull/18492) and it may reflect in TF2.15v onwards. Please feel free to close the issue. If still having any issue please let us know. 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/61915">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/61915">No</a>

### Issue type

Bug

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

No

### Source

binary

### TensorFlow version

v2.13.0-rc2-7-g1cb1a030a62 2.13.0

### Custom code

Yes

### OS platform and distribution

MacOS ARM M1

### Mobile device

_No response_

### Python version

3.10.13

### Bazel version

_No response_

### GCC/compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current behavior?

When running the code below we get the following error: **_AttributeError: 'Adam' object has no attribute 'build'_**

### Standalone code to reproduce the issue

```shell
from tensorflow import keras

if __name__ == '__main__':
  optimizer = keras.optimizers.Adam()
  vh  = keras.Input(shape=(2,3), name = 'vh')
  v1  = keras.layers.Dense(512)(vh)
  
  output  = keras.layers.Dense(1, activation='softmax', name='prediction')(v1)
  
  model = keras.Model(inputs=vh, outputs=[output], name="antibody_model")
  
  model.compile(optimizer=optimizer )
  
  model.save('nn_model.keras')
  test = keras.models.load_model('nn_model.keras')
```


### Relevant log output

```shell
Traceback (most recent call last):
  File "/opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/Users/palmito/Development/federated-demo/backend/app/test.py", line 15, in <module>
    test = keras.models.load_model('nn_model.keras')
  File "/Users/palmito/.local/share/virtualenvs/backend-tPS_SUas/lib/python3.10/site-packages/keras/src/saving/saving_api.py", line 230, in load_model
    return saving_lib.load_model(
  File "/Users/palmito/.local/share/virtualenvs/backend-tPS_SUas/lib/python3.10/site-packages/keras/src/saving/saving_lib.py", line 275, in load_model
    raise e
  File "/Users/palmito/.local/share/virtualenvs/backend-tPS_SUas/lib/python3.10/site-packages/keras/src/saving/saving_lib.py", line 240, in load_model
    model = deserialize_keras_object(
  File "/Users/palmito/.local/share/virtualenvs/backend-tPS_SUas/lib/python3.10/site-packages/keras/src/saving/serialization_lib.py", line 710, in deserialize_keras_object
    instance.compile_from_config(compile_config)
  File "/Users/palmito/.local/share/virtualenvs/backend-tPS_SUas/lib/python3.10/site-packages/keras/src/engine/training.py", line 3582, in compile_from_config
    self.optimizer.build(self.trainable_variables)
  File "/Users/palmito/.local/share/virtualenvs/backend-tPS_SUas/lib/python3.10/site-packages/keras/src/optimizers/legacy/optimizer_v2.py", line 997, in __getattribute__
    raise e
  File "/Users/palmito/.local/share/virtualenvs/backend-tPS_SUas/lib/python3.10/site-packages/keras/src/optimizers/legacy/optimizer_v2.py", line 987, in __getattribute__
    return super().__getattribute__(name)
AttributeError: 'Adam' object has no attribute 'build'
```
```
