为什么 Dropout 不改变我的输入张量?

Why does Dropout not change my input tensor?

请查看以下与输出相关的代码,

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)

输出为:

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

能否请您详细说明为什么输入张量值仍然没有变化?

torch.nn.Dropout 的文档中,您可以看到 inplace 参数默认为 False。如果您希望就地更改输入张量,请将初始化更改为:

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