megengine.functional.nn.dropout

dropout(inp, drop_prob, training=True)[源代码]

返回一个新张量,其中每个元素按概率 P = drop_prob 随机被设置为零。可以选择是否重新缩放输出张量。

参数
  • inp (Tensor) – 输入张量。

  • drop_prob (float) – 丢弃单个元素(将其设置为0)的概率。

  • training (bool) – dropout 在训练期间的默认行为是重新调整输出,在推理期间可以将其替换为 Identity.

返回类型

Tensor

返回

输出张量

实际案例

import numpy as np
from megengine import tensor
import megengine.functional as F

# test training mode
data = tensor(np.ones(10000000, dtype=np.float32))
out = F.nn.dropout(data, 1.0 / 3.0, training=True)
assert not out.numpy().all()

# test eval mode
out = F.nn.dropout(data, 1.0 / 3.0, training=False)
assert out.numpy().all()

输出:

[1.5 1.5 0.  1.5 1.5 1.5 1.5 1.5 1.5 1.5]