MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / dropout

Function dropout

imperative/python/megengine/functional/nn.py:1612–1641  ·  view source on GitHub ↗

r"""Returns a new tensor where each of the elements are randomly set to zero with probability P = ``drop_prob``. Optionally rescale the output tensor if ``training`` is True. Args: inp: input tensor. drop_prob: probability to drop (set to zero) a single element. trai

(inp: Tensor, drop_prob: float, training: bool = True)

Source from the content-addressed store, hash-verified

1610
1611
1612def dropout(inp: Tensor, drop_prob: float, training: bool = True) -> Tensor:
1613 r"""Returns a new tensor where each of the elements are randomly set to zero
1614 with probability P = ``drop_prob``. Optionally rescale the output tensor if ``training`` is True.
1615
1616 Args:
1617 inp: input tensor.
1618 drop_prob: probability to drop (set to zero) a single element.
1619 training: the default behavior of ``dropout`` during training is to rescale the output,
1620 then it can be replaced by an :class:`~.module.identify.Identity` during inference. Default: True
1621 Returns:
1622 the ouput tensor
1623
1624 Examples:
1625 >>> import numpy as np
1626 >>> data = Tensor(np.ones(10000000, dtype=np.float32))
1627 >>> out = F.nn.dropout(data, 1.0 / 3.0, training=True)
1628 >>> assert not out.numpy().all()
1629 >>> out = F.nn.dropout(data, 1.0 / 3.0, training=False)
1630 >>> assert out.numpy().all()
1631 >>> out.numpy()
1632 array([1., 1., 1., ..., 1., 1., 1.], dtype=float32)
1633 """
1634 assert 0 <= drop_prob < 1
1635 if not training or drop_prob == 0:
1636 return inp
1637
1638 # model in training mode, e.g.model.train()
1639 op = Dropout(drop_prob=drop_prob, seed=_get_global_rng_seed(), handle=0)
1640 outputs = apply(op, inp)
1641 return outputs[0]
1642
1643
1644def one_hot(inp: Tensor, num_classes: int) -> Tensor:

Callers 1

forwardMethod · 0.85

Calls 2

DropoutClass · 0.85
applyFunction · 0.50

Tested by

no test coverage detected