-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Add StochasticDepth implementation #4301
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 all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4a4cec0
Adding operator.
datumbox 4170c07
Adding tests
datumbox df0344a
Merge branch 'master' into ops/stochastic_depth
datumbox 9c8de0e
switching order of `p` and `mode`.
datumbox bc58919
Remove seed setting.
datumbox 27e0b7b
Replace stats import with pytest.importorskip.
datumbox 4f83959
Fix doc
datumbox 2ca25a9
Apply suggestions from code review
datumbox be0bf04
Fixing indentation.
datumbox d050bb9
Adding operator in the documentation.
datumbox ae9be89
Fixing lint
datumbox 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
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
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
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import torch | ||
from torch import nn, Tensor | ||
|
||
|
||
def stochastic_depth(input: Tensor, p: float, mode: str, training: bool = True) -> Tensor: | ||
""" | ||
Implements the Stochastic Depth from `"Deep Networks with Stochastic Depth" | ||
<https://arxiv.org/abs/1603.09382>`_ used for randomly dropping residual | ||
branches of residual architectures. | ||
|
||
Args: | ||
input (Tensor[N, ...]): The input tensor or arbitrary dimensions with the first one | ||
being its batch i.e. a batch with ``N`` rows. | ||
p (float): probability of the input to be zeroed. | ||
mode (str): ``"batch"`` or ``"row"``. | ||
``"batch"`` randomly zeroes the entire input, ``"row"`` zeroes | ||
randomly selected rows from the batch. | ||
training: apply stochastic depth if is ``True``. Default: ``True`` | ||
|
||
Returns: | ||
Tensor[N, ...]: The randomly zeroed tensor. | ||
""" | ||
if p < 0.0 or p > 1.0: | ||
raise ValueError("drop probability has to be between 0 and 1, but got {}".format(p)) | ||
if not training or p == 0.0: | ||
return input | ||
|
||
survival_rate = 1.0 - p | ||
if mode not in ["batch", "row"]: | ||
raise ValueError("mode has to be either 'batch' or 'row', but got {}".format(mode)) | ||
size = [1] * input.ndim | ||
if mode == "row": | ||
size[0] = input.shape[0] | ||
noise = torch.empty(size, dtype=input.dtype, device=input.device) | ||
noise = noise.bernoulli_(survival_rate).div_(survival_rate) | ||
return input * noise | ||
|
||
|
||
class StochasticDepth(nn.Module): | ||
""" | ||
See :func:`stochastic_depth`. | ||
""" | ||
def __init__(self, p: float, mode: str) -> None: | ||
super().__init__() | ||
self.p = p | ||
self.mode = mode | ||
|
||
def forward(self, input: Tensor) -> Tensor: | ||
return stochastic_depth(input, self.p, self.mode, self.training) | ||
|
||
def __repr__(self) -> str: | ||
tmpstr = self.__class__.__name__ + '(' | ||
tmpstr += 'p=' + str(self.p) | ||
tmpstr += ', mode=' + str(self.mode) | ||
tmpstr += ')' | ||
return tmpstr |
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.