-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
24 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from tensorflow.keras import Model | ||
from tensorflow.python.keras.layers import deserialize, serialize | ||
from tensorflow.python.keras.saving import saving_utils | ||
|
||
def unpack(model, training_config, weights): | ||
restored_model = deserialize(model) | ||
if training_config is not None: | ||
restored_model.compile(**saving_utils.compile_args_from_training_config(training_config)) | ||
restored_model.set_weights(weights) | ||
return restored_model | ||
|
||
def make_keras_picklable(): | ||
def __reduce__(self): | ||
model_metadata = saving_utils.model_metadata(self) | ||
training_config = model_metadata.get("training_config", None) | ||
model = serialize(self) | ||
weights = self.get_weights() | ||
return (unpack, (model, training_config, weights)) | ||
|
||
cls = Model | ||
cls.__reduce__=__reduce__ |