Search code examples
pythonpytorchdropout

Why does Dropout not change my input tensor?


Please see the following code associated with output,

import torch
import torch.nn as nn
inputTensor = torch.tensor([1.0, 2.0, 3, 4, 5])

outplace_dropout = nn.Dropout(p=0.4)
print(inputTensor)
output_afterDropout = outplace_dropout(inputTensor)
print(output_afterDropout)
print(inputTensor)

The output is:

tensor([1., 2., 3., 4., 5.])
tensor([1.6667, 3.3333, 0.0000, 6.6667, 0.0000])
tensor([1., 2., 3., 4., 5.])

Could you please elaborate why the input tensor values are still unchanged?


Solution

  • From the documentation of torch.nn.Dropout, you can see that the inplace argument defaults to False. If you wish to change the input tensor in place, change the initialization to:

    outplace_dropout = nn.Dropout(p=0.4, inplace=True)