-
Notifications
You must be signed in to change notification settings - Fork 894
Modify Tensorflow regularizers #1864
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7329fe6
Modify Tensorflow regularizers
vl-dud 37acb87
Make regularization factor required
vl-dud 65d1208
Fix checking for empty argument
vl-dud 3de1044
Simplify the code
vl-dud 571233a
Change the order of regularizers
vl-dud 67d1091
Change the order of regularizers
vl-dud f4c648b
Remove the orthogonal regularizer
vl-dud 3d3d47e
Refactor docstring
vl-dud File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,17 +1,61 @@ | ||
| from ..backend import tf | ||
|
|
||
| REGULARIZER_DICT = { | ||
| "l1": tf.keras.regularizers.L1, | ||
| "l2": tf.keras.regularizers.L2, | ||
| "l1l2": tf.keras.regularizers.L1L2, | ||
| "l1+l2": tf.keras.regularizers.L1L2, | ||
| } | ||
| if hasattr(tf.keras.regularizers, "OrthogonalRegularizer"): | ||
| REGULARIZER_DICT["orthogonal"] = tf.keras.regularizers.OrthogonalRegularizer | ||
|
|
||
|
|
||
| def get(identifier): | ||
| """Retrieves a TensorFlow regularizer instance based on the given identifier. | ||
| Args: | ||
| identifier (str or list/tuple): Specifies the type of regularizer and | ||
| the optional scale values. If a string, it should be one of "l1", | ||
| "l2", "orthogonal", or "l1l2" ("l1+l2"); default scale values will | ||
| be used. If a list or tuple, the first element should be one of | ||
| the above strings, followed by scale values. For "l1", "l2", or | ||
| "orthogonal", you can provide a single scale value. For "l1l2" ("l1+l2"), | ||
| you can provide both "l1" and "l2" scale values. | ||
| """ | ||
|
|
||
| # TODO: other backends | ||
| if identifier is None: | ||
| return None | ||
| name, scales = identifier[0].lower(), identifier[1:] | ||
| return ( | ||
| tf.keras.regularizers.L1(l1=scales[0]) | ||
| if name == "l1" | ||
| else tf.keras.regularizers.L2(l2=scales[0]) | ||
| if name == "l2" | ||
| else tf.keras.regularizers.L1L2(l1=scales[0], l2=scales[1]) | ||
| if name in ("l1+l2", "l1l2") | ||
| else None | ||
| ) | ||
|
|
||
| if isinstance(identifier, str): | ||
| name = identifier.lower() | ||
| scales = [] | ||
| elif isinstance(identifier, (list, tuple)) and identifier: | ||
| name = identifier[0].lower() | ||
| scales = identifier[1:] | ||
| else: | ||
| raise ValueError("Identifier must be a string or a non-empty list or tuple.") | ||
|
|
||
| regularizer_class = REGULARIZER_DICT.get(name) | ||
| if not regularizer_class: | ||
| if name == "orthogonal": | ||
| raise ValueError( | ||
| "The 'orthogonal' regularizer is not available " | ||
| "in your version of TensorFlow" | ||
| ) | ||
| raise ValueError(f"Unknown regularizer name: {name}") | ||
|
|
||
| regularizer_kwargs = {} | ||
| if scales: | ||
| if name == "l1": | ||
| regularizer_kwargs["l1"] = scales[0] | ||
| elif name == "l2": | ||
| regularizer_kwargs["l2"] = scales[0] | ||
| elif name == "orthogonal": | ||
| regularizer_kwargs["factor"] = scales[0] | ||
| elif name in ("l1l2", "l1+l2"): | ||
| if len(scales) < 2: | ||
| raise ValueError("L1L2 regularizer requires both L1/L2 penalties.") | ||
| regularizer_kwargs["l1"] = scales[0] | ||
| regularizer_kwargs["l2"] = scales[1] | ||
| return regularizer_class(**regularizer_kwargs) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.