-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
clone_initializer
work better with type checking. (#18)
Also, `str` is preferred to `Text` for type hints.
- Loading branch information
Showing
1 changed file
with
15 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,27 @@ | ||
from typing import Text, Union | ||
from typing import Union | ||
|
||
import keras | ||
|
||
|
||
def clone_initializer( | ||
initializer: Union[Text, keras.initializers.Initializer], | ||
initializer: Union[str, keras.initializers.Initializer], | ||
) -> keras.initializers.Initializer: | ||
"""Clones an initializer to ensure a new seed. | ||
Args: | ||
initializer: The initializer to clone. | ||
Returns: | ||
A cloned initializer if it is clonable, otherwise the original one. | ||
As of tensorflow 2.10, we need to clone user passed initializers when | ||
invoking them twice to avoid creating the same randomized initialization. | ||
""" | ||
if isinstance(initializer, keras.initializers.Initializer): | ||
config = initializer.get_config() | ||
initializer_class: type[keras.initializers.Initializer] = ( | ||
initializer.__class__ | ||
) | ||
return initializer_class.from_config(config) | ||
# If we get a string or dict, just return as we cannot and should not clone. | ||
if not isinstance(initializer, keras.initializers.Initializer): | ||
return initializer | ||
config = initializer.get_config() | ||
return initializer.__class__.from_config(config) | ||
return initializer |