This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[Retiarii] Use ValueChoice inline in a serializable instance #3382
Merged
ultmaster
merged 4 commits into
microsoft:master
from
ultmaster:retiarii/value-choice-as-parameter
Feb 24, 2021
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
import torch | ||
import torch.nn as nn | ||
|
||
from ...utils import uid, add_record, del_record | ||
from ...utils import uid, add_record, del_record, Translatable | ||
|
||
|
||
__all__ = ['LayerChoice', 'InputChoice', 'ValueChoice', 'Placeholder', 'ChosenInputs'] | ||
|
@@ -130,6 +130,9 @@ def forward(self, x): | |
warnings.warn('You should not run forward of this module directly.') | ||
return x | ||
|
||
def __repr__(self): | ||
return f'LayerChoice({self.candidates}, label={repr(self.label)})' | ||
|
||
|
||
class InputChoice(nn.Module): | ||
""" | ||
|
@@ -188,33 +191,66 @@ def forward(self, candidate_inputs: List[torch.Tensor]) -> torch.Tensor: | |
warnings.warn('You should not run forward of this module directly.') | ||
return candidate_inputs[0] | ||
|
||
def __repr__(self): | ||
return f'InputChoice(n_candidates={self.n_candidates}, n_chosen={self.n_chosen}, ' \ | ||
f'reduction={repr(self.reduction)}, label={repr(self.label)})' | ||
|
||
|
||
class ValueChoice(nn.Module): | ||
class ValueChoice(Translatable, nn.Module): | ||
""" | ||
ValueChoice is to choose one from ``candidates``. | ||
|
||
Should initialize the values to choose from in init and call the module in forward to get the chosen value. | ||
In most use scenarios, ValueChoice should be passed to the init parameters of a serializable module. For example, | ||
|
||
.. code-block:: python | ||
|
||
class Net(nn.Module): | ||
def __init__(self): | ||
super().__init__() | ||
self.conv = nn.Conv2d(3, nn.ValueChoice([32, 64]), kernel_size=nn.ValueChoice([3, 5, 7])) | ||
|
||
def forward(self, x): | ||
return self.conv(x) | ||
|
||
A common use is to pass a mutable value to a functional API like ``torch.xxx`` or ``nn.functional.xxx```. For example, | ||
In case, you want to search a parameter that is used repeatedly, this is also possible by sharing the same value choice instance. | ||
(Sharing the label should have the same effect.) For example, | ||
|
||
.. code-block:: python | ||
|
||
class Net(nn.Module): | ||
def __init__(self): | ||
super().__init__() | ||
self.dropout_rate = nn.ValueChoice([0., 1.]) | ||
hidden_dim = nn.ValueChoice([128, 512]) | ||
self.fc = nn.Sequential( | ||
nn.Linear(64, hidden_dim), | ||
nn.Linear(hidden_dim, 10) | ||
) | ||
|
||
# the following code has the same effect. | ||
# self.fc = nn.Sequential( | ||
# nn.Linear(64, nn.ValueChoice([128, 512], label='dim')), | ||
# nn.Linear(nn.ValueChoice([128, 512], label='dim'), 10) | ||
# ) | ||
|
||
def forward(self, x): | ||
return F.dropout(x, self.dropout_rate()) | ||
return self.fc(x) | ||
|
||
The following use case is currently not supported because ValueChoice cannot be called in ``__init__``. | ||
Please use LayerChoice as a workaround. | ||
Note that ValueChoice should be used directly. Transformations like ``nn.Linear(32, nn.ValueChoice([64, 128]) * 2)`` | ||
are not supported. | ||
|
||
Another common use case is to initialize the values to choose from in init and call the module in forward to get the chosen value. | ||
Usually, this is used to pass a mutable value to a functional API like ``torch.xxx`` or ``nn.functional.xxx```. | ||
For example, | ||
|
||
.. code-block:: python | ||
|
||
# in __init__ code | ||
self.kernel_size = nn.ValueChoice([3, 5]) | ||
self.conv = nn.Conv2d(3, self.out_channels, kernel_size=self.kernel_size()) | ||
class Net(nn.Module): | ||
def __init__(self): | ||
super().__init__() | ||
self.dropout_rate = nn.ValueChoice([0., 1.]) | ||
|
||
def forward(self, x): | ||
return F.dropout(x, self.dropout_rate()) | ||
|
||
Parameters | ||
---------- | ||
|
@@ -237,6 +273,13 @@ def forward(self): | |
warnings.warn('You should not run forward of this module directly.') | ||
return self.candidates[0] | ||
|
||
def __translate__(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it proper to use |
||
# Will function as a value when used in serializer. | ||
return self.candidates[0] | ||
|
||
def __repr__(self): | ||
return f'ValueChoice({self.candidates}, label={repr(self.label)})' | ||
|
||
|
||
class Placeholder(nn.Module): | ||
# TODO: docstring | ||
|
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.
The return type is
Iterable
in this case.