By using pyTorch there is two ways to dropout
torch.nn.Dropout
and torch.nn.F.Dropout
.
I struggle to see the difference between the use of them
- when to use what?
- Does it make a difference?
I don't see any performance difference when I switched them around.
The technical differences have already been shown in the other answer. However the main difference is that
nn.Dropout
is a torch Module itself which bears some convenience:A short example for illustration of some differences:
Output:
So which should I use?
Both are completely equivalent in terms of applying dropout and even though the differences in usage are not that big, there are some reasons to favour the
nn.Dropout
overnn.functional.dropout
:Dropout is designed to be only applied during training, so when doing predictions or evaluation of the model you want dropout to be turned off.
The dropout module
nn.Dropout
conveniently handles this and shuts dropout off as soon as your model enters evaluation mode, while the functional dropout does not care about the evaluation / prediction mode.Even though you can set functional dropout to
training=False
to turn it off, it is still not such a convenient solution like withnn.Dropout
.Also the drop rate is stored in the module, so you don't have to save it in an extra variable. In larger networks you might want to create different dropout layers with different drop rates - here
nn.Dropout
may increase readability and can bear also some convenience when using the layers multiple times.Finally, all modules which are assigned to your model are registered in your model. So you model class keeps track of them, that is why you can just turn off the dropout module by calling
eval()
. When using the functional dropout your model is not aware of it, thus it won't appear in any summary.If you look at the source code of nn.Dropout and Functional.Dropout, you can see
Functional
is an interface andnn
module implement functions with respect to this interface.Look at the implementations in
nn
class:And so on.
Implementation of
Functional
class:look at the example below to understand:
There is a
F.dropout
inforward()
function and ann.Dropout
in__init__()
function. Now this is the explanation:In PyTorch you define your Models as subclasses of torch.nn.Module.
In the init function, you are supposed to initialize the layers you want to use. Unlike keras, Pytorch goes more low level and you have to specify the sizes of your network so that everything matches.
In the forward method, you specify the connections of your layers. This means that you will use the layers you already initialized, in order to re-use the same layer for each forward pass of data you make.
torch.nn.Functional contains some useful functions like activation functions a convolution operations you can use. However, these are not full layers so if you want to specify a layer of any kind you should use torch.nn.Module.
You would use the torch.nn.Functional conv operations to define a custom layer for example with a convolution operation, but not to define a standard convolution layer.