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

include_optimizer parameter introduced to push_to_hub_keras() #616

Merged
merged 21 commits into from
Feb 2, 2022
Merged
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/huggingface_hub/keras_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@


def save_pretrained_keras(
model, save_directory: str, config: Optional[Dict[str, Any]] = None
model,
save_directory: str,
config: Optional[Dict[str, Any]] = None,
include_optimizer: Optional[bool] = False,
**model_save_kwargs,
):
"""Saves a Keras model to save_directory in SavedModel format. Use this if you're using the Functional or Sequential APIs.

Expand All @@ -27,6 +31,10 @@ def save_pretrained_keras(
Specify directory in which you want to save the Keras model.
config (:obj:`dict`, `optional`):
Configuration object to be saved alongside the model weights.
include_optimizer(:obj:`bool`, `optional`):
Whether or not to include optimizer in serialization.
model_save_kwargs(:obj:`dict`, `optional`):
Arguments other than default arguments that can be passed to tf.keras.models.save_model().
merveenoyan marked this conversation as resolved.
Show resolved Hide resolved
"""
if is_tf_available():
import tensorflow as tf
Expand All @@ -50,7 +58,9 @@ def save_pretrained_keras(
with open(path, "w") as f:
json.dump(config, f)

tf.keras.models.save_model(model, save_directory)
tf.keras.models.save_model(
model, save_directory, include_optimizer=include_optimizer, **model_save_kwargs
)


def from_pretrained_keras(*args, **kwargs):
Expand All @@ -69,6 +79,8 @@ def push_to_hub_keras(
git_user: Optional[str] = None,
git_email: Optional[str] = None,
config: Optional[dict] = None,
include_optimizer: Optional[bool] = False,
**model_save_kwargs,
):
"""
Upload model checkpoint or tokenizer files to the 🤗 Model Hub while synchronizing a local clone of the repo in
Expand Down Expand Up @@ -104,6 +116,10 @@ def push_to_hub_keras(
will override the ``git config user.email`` for committing and pushing files to the hub.
config (:obj:`dict`, `optional`):
Configuration object to be saved alongside the model weights.
include_optimizer (:obj:`bool`, `optional`):
Whether or not to include optimizer during serialization.
model_save_kwargs(:obj:`dict`, `optional`):
Arguments other than default arguments that can be passed to tf.keras.models.save_model().
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here :)


Returns:
The url of the commit of your model in the given repository.
Expand Down Expand Up @@ -150,7 +166,13 @@ def push_to_hub_keras(
)
repo.git_pull(rebase=True)

save_pretrained_keras(model, repo_path_or_name, config=config)
save_pretrained_keras(
model,
repo_path_or_name,
config=config,
include_optimizer=include_optimizer,
**model_save_kwargs,
)

# Commit and push!
repo.git_add(auto_lfs_track=True)
Expand Down