alr.modules.dropout¶
Modify Dropout torch.nn.Modules to _always_ activate in training and inference.
The classes in this module are taken from PyTorch as-is.
The main function you should be concerned with is replace_dropout().
Classses¶
PersistentDropout¶
-
class
alr.modules.dropout.PersistentDropout(p: float = 0.5, inplace: bool = False)[source]¶ Bases:
torch.nn.modules.dropout._DropoutNdDuring training, randomly zeroes some of the elements of the input tensor with probability
pusing samples from a Bernoulli distribution. Each channel will be zeroed out independently on every forward call. This has proven to be an effective technique for regularization and preventing the co-adaptation of neurons as described in the paper Improving neural networks by preventing co-adaptation of feature detectors . Furthermore, the outputs are scaled by a factor of \(\frac{1}{1-p}\) during training. This means that during evaluation the module simply computes an identity function.Parameters: - 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: \((*)\). Input can be of any shape
- Output: \((*)\). Output is of the same shape as input
- Examples::
>>> m = nn.Dropout(p=0.2) >>> input = torch.randn(20, 16) >>> output = m(input)
-
forward(input)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
PersistentDropout2d¶
-
class
alr.modules.dropout.PersistentDropout2d(p: float = 0.5, inplace: bool = False)[source]¶ Bases:
torch.nn.modules.dropout._DropoutNdRandomly zero out entire channels (a channel is a 2D feature map, e.g., the \(j\)-th channel of the \(i\)-th sample in the batched input is a 2D tensor \(\text{input}[i, j]\)). Each channel will be zeroed out independently on every forward call with probability
pusing samples from a Bernoulli distribution. Usually the input comes fromnn.Conv2dmodules. 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 i.i.d. dropout will not regularize the activations and will otherwise just result in an effective learning rate decrease. In this case,nn.Dropout2d()will help promote independence between feature maps and should be used instead.Parameters: - p (float, optional) – probability of an element to be zero-ed.
- inplace (bool, optional) – If set to
True, will do this operation in-place
- Shape:
- Input: \((N, C, H, W)\)
- Output: \((N, C, H, W)\) (same shape as input)
- Examples::
>>> m = nn.Dropout2d(p=0.2) >>> input = torch.randn(20, 16, 32, 32) >>> output = m(input)
-
forward(input)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
PersistentDropout3d¶
-
class
alr.modules.dropout.PersistentDropout3d(p: float = 0.5, inplace: bool = False)[source]¶ Bases:
torch.nn.modules.dropout._DropoutNdRandomly zero out entire channels (a channel is a 3D feature map, e.g., the \(j\)-th channel of the \(i\)-th sample in the batched input is a 3D tensor \(\text{input}[i, j]\)). Each channel will be zeroed out independently on every forward call with probability
pusing samples from a Bernoulli distribution. Usually the input comes fromnn.Conv3dmodules. 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 i.i.d. dropout will not regularize the activations and will otherwise just result in an effective learning rate decrease. In this case,nn.Dropout3d()will help promote independence between feature maps and should be used instead.Parameters: - 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: \((N, C, D, H, W)\)
- Output: \((N, C, D, H, W)\) (same shape as input)
- Examples::
>>> m = nn.Dropout3d(p=0.2) >>> input = torch.randn(20, 16, 4, 32, 32) >>> output = m(input)
-
forward(input)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
PersistentAlphaDropout¶
-
class
alr.modules.dropout.PersistentAlphaDropout(p: float = 0.5, inplace: bool = False)[source]¶ Bases:
torch.nn.modules.dropout._DropoutNdApplies Alpha Dropout over the input. Alpha Dropout is a type of Dropout that maintains the self-normalizing property. For an input with zero mean and unit standard deviation, the output of Alpha Dropout maintains the original mean and standard deviation of the input. Alpha Dropout goes hand-in-hand with SELU activation function, which ensures that the outputs have zero mean and unit standard deviation. During training, it randomly masks some of the elements of the input tensor with probability p using samples from a bernoulli distribution. The elements to masked are randomized on every forward call, and scaled and shifted to maintain zero mean and unit standard deviation. During evaluation the module simply computes an identity function. More details can be found in the paper Self-Normalizing Neural Networks .
Parameters: - p (float) – probability of an element to be dropped. Default: 0.5
- inplace (bool, optional) – If set to
True, will do this operation in-place
- Shape:
- Input: \((*)\). Input can be of any shape
- Output: \((*)\). Output is of the same shape as input
- Examples::
>>> m = nn.AlphaDropout(p=0.2) >>> input = torch.randn(20, 16) >>> output = m(input)
-
forward(input)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
PersistentFeatureAlphaDropout¶
-
class
alr.modules.dropout.PersistentFeatureAlphaDropout(p: float = 0.5, inplace: bool = False)[source]¶ Bases:
torch.nn.modules.dropout._DropoutNd-
forward(input)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
ConsistentDropout¶
-
class
alr.modules.dropout.ConsistentDropout(p=0.5)[source]¶ Bases:
torch.nn.modules.dropout._DropoutNdConsistentDropout is useful when doing research. It guarantees that while the masks are the same between batches during inference. The masks are different inside the batch. This is slower than using regular Dropout, but it is useful when you want to use the same set of weights for each sample used in inference. From BatchBALD (Kirsch et al, 2019), this is necessary to use BatchBALD and remove noise from the prediction. :param p: probability of an element to be zeroed. Default: 0.5 :type p: float
Notes
For optimal results, you should use a batch size of one during inference time. Furthermore, to guarantee that each sample uses the same set of weights, you must use replicate_in_memory=True in ModelWrapper, which is the default.
-
eval()[source]¶ Sets the module in evaluation mode.
This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g.
Dropout,BatchNorm, etc.This is equivalent with
self.train(False).Returns: self Return type: Module
-
forward(x)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
train(mode=True)[source]¶ Sets the module in training mode.
This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g.
Dropout,BatchNorm, etc.Parameters: mode (bool) – whether to set training mode ( True) or evaluation mode (False). Default:True.Returns: self Return type: Module
-
ConsistentDropout2d¶
-
class
alr.modules.dropout.ConsistentDropout2d(p=0.5)[source]¶ Bases:
torch.nn.modules.dropout._DropoutNdConsistentDropout is useful when doing research. It guarantees that while the mask are the same between batches, they are different inside the batch. This is slower than using regular Dropout, but it is useful when you want to use the same set of weights for each unlabelled sample. :param p: probability of an element to be zeroed. Default: 0.5 :type p: float
Notes
For optimal results, you should use a batch size of one during inference time. Furthermore, to guarantee that each sample uses the same set of weights, you must use replicate_in_memory=True in ModelWrapper, which is the default.
-
eval()[source]¶ Sets the module in evaluation mode.
This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g.
Dropout,BatchNorm, etc.This is equivalent with
self.train(False).Returns: self Return type: Module
-
forward(x)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
train(mode=True)[source]¶ Sets the module in training mode.
This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g.
Dropout,BatchNorm, etc.Parameters: mode (bool) – whether to set training mode ( True) or evaluation mode (False). Default:True.Returns: self Return type: Module
-
Functions¶
replace_dropout¶
-
alr.modules.dropout.replace_dropout(module: torch.nn.modules.module.Module, inplace: Optional[bool] = True) → torch.nn.modules.module.Module[source]¶ Recursively replaces dropout modules in module such that dropout is performed regardless of the model’s mode. That is, dropout is performed during training (model.train()) and inference (model.eval()) modes.
Parameters: - module (torch.nn.Module) – PyTorch module object
- inplace (bool, optional) – If True, the model is modified in-place. If False, model is not modified and a new model is cloned.
Returns: Same module instance if inplace is False, else a brand new module.
Return type: torch.nn.Module
replace_consistent_dropout¶
-
alr.modules.dropout.replace_consistent_dropout(module: torch.nn.modules.module.Module, inplace: Optional[bool] = True) → torch.nn.modules.module.Module[source]¶ Recursively replaces dropout modules in module such that dropout is performed regardless of the model’s mode and uses the same mask across batches. The mask is refreshed each time model.eval() is invoked but the mask is guaranteed to be consistent across all batch (different masks for each item within the batch).
Parameters: - module (torch.nn.Module) – PyTorch module object
- inplace (bool, optional) – If True, the model is modified in-place. If False, model is not modified and a new model is cloned.
Returns: Same module instance if inplace is False, else a brand new module.
Return type: torch.nn.Module