-
Notifications
You must be signed in to change notification settings - Fork 86
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
#189 Add unpatch for dropout and consistent dropout #194
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1006861
#189 Add unpatch for dropout and consistent dropout
f6f933c
#189 remove -n 2 for xdist
ef1afd9
Merge branch 'master' into BAAL-189/unpatch
Dref360 7e2a997
Merge branch 'master' into BAAL-189/unpatch
Dref360 2488f08
Merge branch 'master' into BAAL-189/unpatch
Dref360 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 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,22 @@ | ||
from typing import Callable | ||
from torch import nn | ||
|
||
|
||
def replace_layers_in_module(module: nn.Module, mapping_fn: Callable) -> bool: | ||
""" | ||
Recursively iterate over the children of a module and replace them according to `mapping_fn`. | ||
|
||
Returns: | ||
True if a layer has been changed. | ||
""" | ||
changed = False | ||
for name, child in module.named_children(): | ||
new_module = mapping_fn(child) | ||
|
||
if new_module is not None: | ||
changed = True | ||
module.add_module(name, new_module) | ||
|
||
# recursively apply to child | ||
changed |= replace_layers_in_module(child, mapping_fn) | ||
return changed |
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
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
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,64 @@ | ||
import pytest | ||
from torch import nn | ||
|
||
from baal.bayesian.common import replace_layers_in_module | ||
|
||
|
||
@pytest.fixture | ||
def a_model_deep(): | ||
return nn.Sequential( | ||
nn.Linear(32, 32), | ||
nn.Sequential( | ||
nn.Linear(32, 3), | ||
nn.ReLU(), | ||
nn.Linear(10, 3), | ||
nn.ReLU(), | ||
nn.Linear(3, 3) | ||
)) | ||
|
||
|
||
@pytest.fixture | ||
def a_model(): | ||
return nn.Sequential( | ||
nn.Linear(32, 3), | ||
nn.ReLU(), | ||
nn.Linear(10, 3), | ||
nn.ReLU(), | ||
nn.Linear(3, 3) | ||
) | ||
|
||
|
||
def test_replace_layers_in_module_swap_all_relu(a_model): | ||
mapping = lambda mod: None if not isinstance(mod, nn.ReLU) else nn.Identity() | ||
changed = replace_layers_in_module(a_model, mapping) | ||
assert changed | ||
assert not any(isinstance(m, nn.ReLU) for m in a_model.modules()) | ||
assert any(isinstance(m, nn.Identity) for m in a_model.modules()) | ||
|
||
|
||
def test_replace_layers_in_module_swap_all_relu_deep(a_model_deep): | ||
mapping = lambda mod: None if not isinstance(mod, nn.ReLU) else nn.Identity() | ||
changed = replace_layers_in_module(a_model_deep, mapping) | ||
assert changed | ||
assert not any(isinstance(m, nn.ReLU) for m in a_model_deep.modules()) | ||
assert any(isinstance(m, nn.Identity) for m in a_model_deep.modules()) | ||
|
||
|
||
def test_replace_layers_in_module_swap_no_relu_deep(a_model_deep): | ||
mapping = lambda mod: None if not isinstance(mod, nn.ReLU6) else nn.Identity() | ||
changed = replace_layers_in_module(a_model_deep, mapping) | ||
assert not changed | ||
assert any(isinstance(m, nn.ReLU) for m in a_model_deep.modules()) | ||
assert not any(isinstance(m, nn.Identity) for m in a_model_deep.modules()) | ||
|
||
def test_replace_layers_in_module_swap_no_relu_deep(a_model): | ||
mapping = lambda mod: None if not isinstance(mod, nn.ReLU6) else nn.Identity() | ||
changed = replace_layers_in_module(a_model, mapping) | ||
assert not changed | ||
assert any(isinstance(m, nn.ReLU) for m in a_model.modules()) | ||
assert not any(isinstance(m, nn.Identity) for m in a_model.modules()) | ||
|
||
|
||
|
||
if __name__ == '__main__': | ||
pytest.main() |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will make sure that
self.parent_module
is always patched but at every step the user can ask for an unpatched version right? its very useful just wanna make sure that if you callunpatch
the internal module remains patched.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes