mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +01:00
Here's the command I used to invoke autopep8 (in parallel!):
git ls-files | grep '\.py$' | xargs -n1 -P`nproc` autopep8 -i
Several rules are ignored in setup.cfg. The goal is to let autopep8
handle everything which it can handle safely, and to disable any rules
which are tricky or controversial to address. We may want to come back
and re-enable some of these rules later, but I'm trying to make this
patch as safe as possible.
Also configures flake8 to match pep8's behavior.
Also configures TravisCI to check the whole project for lint.
144 lines
4.8 KiB
Python
144 lines
4.8 KiB
Python
from .module import Module
|
|
from .. import functional as F
|
|
|
|
|
|
class Dropout(Module):
|
|
r"""Randomly zeroes some of the elements of the input tensor.
|
|
The elements to zero are randomized on every forward call.
|
|
|
|
Args:
|
|
p: probability of an element to be zeroed. Default: 0.5
|
|
inplace: If set to True, will do this operation in-place. Default: false
|
|
|
|
Shape:
|
|
- Input: `Any`. Input can be of any shape
|
|
- Output: `Same`. Output is of the same shape as input
|
|
|
|
Examples::
|
|
|
|
>>> m = nn.Dropout(p=0.2)
|
|
>>> input = autograd.Variable(torch.randn(20, 16))
|
|
>>> output = m(input)
|
|
"""
|
|
|
|
def __init__(self, p=0.5, inplace=False):
|
|
super(Dropout, self).__init__()
|
|
if p < 0 or p > 1:
|
|
raise ValueError("dropout probability has to be between 0 and 1, "
|
|
"but got {}".format(p))
|
|
self.p = p
|
|
self.inplace = inplace
|
|
|
|
def forward(self, input):
|
|
return F.dropout(input, self.p, self.training, self.inplace)
|
|
|
|
def __repr__(self):
|
|
inplace_str = ', inplace' if self.inplace else ''
|
|
return self.__class__.__name__ + ' (' \
|
|
+ 'p = ' + str(self.p) \
|
|
+ inplace_str + ')'
|
|
|
|
|
|
class Dropout2d(Module):
|
|
r"""Randomly zeroes whole channels of the input tensor.
|
|
The channels to zero-out are randomized on every forward call.
|
|
|
|
*Usually the input comes from Conv2d modules.*
|
|
|
|
As described in the paper
|
|
`Efficient Object Localization Using Convolutional Networks`_ ,
|
|
if adjacent pixels within feature maps are strongly correlated
|
|
(as is normally the case in early convolution layers) then iid dropout
|
|
will not regularize the activations and will otherwise just result
|
|
in an effective learning rate decrease.
|
|
|
|
In this case, :func:`nn.Dropout2d` will help promote independence between
|
|
feature maps and should be used instead.
|
|
|
|
Args:
|
|
p (float, optional): probability of an element to be zeroed.
|
|
inplace (bool, optional): If set to True, will do this operation in-place
|
|
|
|
Shape:
|
|
- Input: :math:`(N, C, H, W)`
|
|
- Output: :math:`(N, C, H, W)` (same shape as input)
|
|
|
|
Examples::
|
|
|
|
>>> m = nn.Dropout2d(p=0.2)
|
|
>>> input = autograd.Variable(torch.randn(20, 16, 32, 32))
|
|
>>> output = m(input)
|
|
|
|
.. _Efficient Object Localization Using Convolutional Networks:
|
|
http://arxiv.org/abs/1411.4280
|
|
"""
|
|
|
|
def __init__(self, p=0.5, inplace=False):
|
|
super(Dropout2d, self).__init__()
|
|
if p < 0 or p > 1:
|
|
raise ValueError("dropout probability has to be between 0 and 1, "
|
|
"but got {}".format(p))
|
|
self.p = p
|
|
self.inplace = inplace
|
|
|
|
def forward(self, input):
|
|
return self._backend.Dropout2d(self.p, self.training, self.inplace)(input)
|
|
|
|
def __repr__(self):
|
|
inplace_str = ', inplace' if self.inplace else ''
|
|
return self.__class__.__name__ + ' (' \
|
|
+ 'p=' + str(self.p) \
|
|
+ inplace_str + ')'
|
|
|
|
|
|
class Dropout3d(Module):
|
|
r"""Randomly zeroes whole channels of the input tensor.
|
|
The channels to zero are randomized on every forward call.
|
|
|
|
*Usually the input comes from Conv3d modules.*
|
|
|
|
As described in the paper
|
|
`Efficient Object Localization Using Convolutional Networks`_ ,
|
|
if adjacent pixels within feature maps are strongly correlated
|
|
(as is normally the case in early convolution layers) then iid dropout
|
|
will not regularize the activations and will otherwise just result
|
|
in an effective learning rate decrease.
|
|
|
|
In this case, :func:`nn.Dropout3d` will help promote independence between
|
|
feature maps and should be used instead.
|
|
|
|
Args:
|
|
p (float, optional): probability of an element to be zeroed.
|
|
inplace (bool, optional): If set to True, will do this operation in-place
|
|
|
|
Shape:
|
|
- Input: :math:`(N, C, D, H, W)`
|
|
- Output: :math:`(N, C, D, H, W)` (same shape as input)
|
|
|
|
Examples::
|
|
|
|
>>> m = nn.Dropout3d(p=0.2)
|
|
>>> input = autograd.Variable(torch.randn(20, 16, 4, 32, 32))
|
|
>>> output = m(input)
|
|
|
|
.. _Efficient Object Localization Using Convolutional Networks:
|
|
http://arxiv.org/abs/1411.4280
|
|
"""
|
|
|
|
def __init__(self, p=0.5, inplace=False):
|
|
super(Dropout3d, self).__init__()
|
|
if p < 0 or p > 1:
|
|
raise ValueError("dropout probability has to be between 0 and 1, "
|
|
"but got {}".format(p))
|
|
self.p = p
|
|
self.inplace = inplace
|
|
|
|
def forward(self, input):
|
|
return self._backend.Dropout3d(self.p, self.training, self.inplace)(input)
|
|
|
|
def __repr__(self):
|
|
inplace_str = ', inplace' if self.inplace else ''
|
|
return self.__class__.__name__ + ' (' \
|
|
+ 'p=' + str(self.p) \
|
|
+ inplace_str + ')'
|