Skip to content
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

'Adam' object has no attribute 'build' (saving and loading keras.optimizers.Adam) #61915

Closed
palminha opened this issue Sep 19, 2023 · 23 comments
Closed
Assignees
Labels
comp:keras Keras related issues Fixed in Nightly Issues that are resolved in nightly version stale This label marks the issue/pr stale - to be closed automatically if no activity stat:awaiting response Status - Awaiting response from author TF 2.13 For issues related to Tensorflow 2.13 type:bug Bug

Comments

@palminha
Copy link

palminha commented Sep 19, 2023

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

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

Traceback (most recent call last):
  File "/opt/homebrew/Cellar/[email protected]/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/[email protected]/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'
@google-ml-butler google-ml-butler bot added the type:bug Bug label Sep 19, 2023
@palminha palminha changed the title 'Adam' object has no attribute 'build' using tensorflow.keras 'Adam' object has no attribute 'build' (using tensorflow.keras) Sep 19, 2023
@palminha
Copy link
Author

same problem reported with Keras here: keras-team/tf-keras#46

@palminha palminha changed the title 'Adam' object has no attribute 'build' (using tensorflow.keras) 'Adam' object has no attribute 'build' (using tensorflow.keras save and load) Sep 19, 2023
@palminha palminha changed the title 'Adam' object has no attribute 'build' (using tensorflow.keras save and load) 'Adam' object has no attribute 'build' (saving and loading keras.optimizers.Adam) Sep 19, 2023
@sushreebarsa sushreebarsa added comp:keras Keras related issues TF 2.13 For issues related to Tensorflow 2.13 labels Sep 20, 2023
@sushreebarsa
Copy link
Contributor

@palminha Thank you for raising this issue!
@SuryanarayanaY I was able to replicate the issue on Macbook, please find the attached screenshot below;
image (3)

I was able to run the coed successfully on colab, please have a look at this gist. Thank you!

@debrupf2946
Copy link

Hi, I am Debrup. I want to work on this problem can I be pleased assigned to the problem?

@SuryanarayanaY
Copy link
Collaborator

@ 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.

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

Thank you!

@SuryanarayanaY
Copy link
Collaborator

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

Thanks!

@SuryanarayanaY SuryanarayanaY added the stat:awaiting tensorflower Status - Awaiting response from tensorflower label Sep 21, 2023
@palminha
Copy link
Author

palminha commented Sep 21, 2023

@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 nitins17 removed their assignment Sep 25, 2023
@palminha
Copy link
Author

@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

@SuryanarayanaY
Copy link
Collaborator

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.

# 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"',

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.

Thanks!

@SuryanarayanaY SuryanarayanaY added stat:awaiting response Status - Awaiting response from author and removed stat:awaiting tensorflower Status - Awaiting response from tensorflower labels Sep 26, 2023
@palminha
Copy link
Author

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

@google-ml-butler google-ml-butler bot removed the stat:awaiting response Status - Awaiting response from author label Sep 27, 2023
@SuryanarayanaY
Copy link
Collaborator

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.

Thank you!

@SuryanarayanaY
Copy link
Collaborator

@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 SuryanarayanaY added the stat:awaiting response Status - Awaiting response from author label Sep 28, 2023
@palminha
Copy link
Author

palminha commented Sep 28, 2023

@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. 

@SuryanarayanaY
Copy link
Collaborator

@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 SuryanarayanaY added stat:awaiting response Status - Awaiting response from author Fixed in Nightly Issues that are resolved in nightly version labels Sep 28, 2023
@palminha
Copy link
Author

@SuryanarayanaY ... keras team closed the related issue: keras-team/tf-keras#46

@google-ml-butler google-ml-butler bot removed the stat:awaiting response Status - Awaiting response from author label Sep 29, 2023
@guangyuxu
Copy link

@ 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.

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

@gszecsenyi
Copy link

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

@palminha
Copy link
Author

palminha commented Nov 1, 2023

@gszecsenyi it will be solved in version 2.15

@gszecsenyi
Copy link

The issue still exists with 2.15rc1.

@hanamurayuki
Copy link

hanamurayuki commented Nov 14, 2023

@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.

@SuryanarayanaY
Copy link
Collaborator

@palminha ,

The issue fixed with this PR 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!

@SuryanarayanaY SuryanarayanaY added the stat:awaiting response Status - Awaiting response from author label Nov 14, 2023
Copy link

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.

@github-actions github-actions bot added the stale This label marks the issue/pr stale - to be closed automatically if no activity label Nov 22, 2023
Copy link

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.

Copy link

Are you satisfied with the resolution of your issue?
Yes
No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:keras Keras related issues Fixed in Nightly Issues that are resolved in nightly version stale This label marks the issue/pr stale - to be closed automatically if no activity stat:awaiting response Status - Awaiting response from author TF 2.13 For issues related to Tensorflow 2.13 type:bug Bug
Projects
None yet
Development

No branches or pull requests

8 participants