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

can't save compiled model as .tf #61279

Closed
Imacder opened this issue Jul 14, 2023 · 4 comments
Closed

can't save compiled model as .tf #61279

Imacder opened this issue Jul 14, 2023 · 4 comments
Assignees
Labels
comp:apis Highlevel API related issues comp:keras Keras related issues 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

@Imacder
Copy link

Imacder commented Jul 14, 2023

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

Yes

Source

source

TensorFlow version

2.13.0

Custom code

Yes

OS platform and distribution

Mac mini m1

Mobile device

No response

Python version

3.9

Bazel version

No response

GCC/compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

m1 gpu and 8GB Ram

Current behavior?

can't save compile model as a .tf file.

Standalone code to reproduce the issue

import tensorflow as tf


class CustomSchedule(tf.keras.optimizers.schedules.LearningRateSchedule):
    def __init__(self, d_model, f=0.5, warmup_steps=4_000):
        super().__init__()

        self.d_model = d_model
        self.d_model = tf.cast(self.d_model, tf.float32)

        self.warmup_steps = warmup_steps
        self.f = f

    def __call__(self, step):
        step = tf.cast(step, dtype=tf.float32)
        arg1 = tf.math.rsqrt(step)
        arg2 = step * (self.warmup_steps ** -1.5)

        return self.f * tf.math.rsqrt(self.d_model) * tf.math.minimum(arg1, arg2)

    def get_config(self):
        config = {
            "d_model": self.d_model,
            "warmup_steps": self.warmup_steps,
            "f": self.f
        }
        return config


model = tf.keras.Sequential([
    tf.keras.layers.Input(shape=(784,)),
    tf.keras.layers.Dense(10)
])
learning_rate = CustomSchedule(1024)
optimizer = tf.keras.optimizers.legacy.Adam(learning_rate, beta_1=0.9, beta_2=0.98,
                                            epsilon=1e-5)
model.compile(
    optimizer=optimizer,
    loss="mse", metrics=['accuracy'])
model.save("model.tf")

Relevant log output

/opt/homebrew/Caskroom/miniconda/base/envs/pythonProject/bin/python /Users/albert/PycharmProjects/pythonProject/AI/test.py 
Traceback (most recent call last):
  File "/Users/albert/PycharmProjects/pythonProject/AI/test.py", line 40, in <module>
    model.save("model.tf")
  File "/opt/homebrew/Caskroom/miniconda/base/envs/pythonProject/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/opt/homebrew/Caskroom/miniconda/base/envs/pythonProject/lib/python3.10/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/pythonProject/lib/python3.10/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
TypeError: Unable to serialize 1024.0 to JSON. Unrecognized type <class 'tensorflow.python.framework.ops.EagerTensor'>.
@google-ml-butler google-ml-butler bot added the type:bug Bug label Jul 14, 2023
@sushreebarsa sushreebarsa added comp:apis Highlevel API related issues comp:keras Keras related issues labels Jul 18, 2023
@sushreebarsa
Copy link
Contributor

@Imacder Keras separates the concerns of saving your model architecture and saving your model weights.
The model structure can be described and saved using two different formats: JSON and YAML. You can save a model with model.save() or keras.models.save_model() (which is equivalent). You can load it back with keras.models.load_model(). Please make sure to follow this guide here to save a compiled model,
Thank you!

@sushreebarsa sushreebarsa added stat:awaiting response Status - Awaiting response from author TF 2.13 For issues related to Tensorflow 2.13 labels Jul 19, 2023
@github-actions
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 Jul 27, 2023
@github-actions
Copy link

github-actions bot commented Aug 3, 2023

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.

@github-actions github-actions bot closed this as completed Aug 3, 2023
@google-ml-butler
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:apis Highlevel API related issues comp:keras Keras related issues 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

2 participants