chainer.functions.dropout¶
-
chainer.functions.
dropout
(x, ratio=.5, *, mask=None, return_mask=False)[source]¶ Drops elements of input variable randomly.
This function drops input elements randomly with probability
ratio
and scales the remaining elements by factor1 / (1 - ratio)
. In testing mode (i.e.,chainer.config.train
is set toFalse
), it does nothing and just returnsx
.Warning
train
argument is not supported anymore since v2. Instead, usechainer.using_config('train', boolean)
. Seechainer.using_config()
.Parameters: - x (
Variable
ornumpy.ndarray
orcupy.ndarray
) – Input variable. A \((s_1, s_2, ..., s_N)\) -shaped float array. - ratio (float) – Dropout ratio. The
ratio
must be0.0 <= ratio < 1.0
. - mask (ndarray or None) – The mask to be used for dropout.
You do not have to specify this value, unless you need to make
results deterministic.
If
mask
is not specified or set toNone
, a mask will be generated randomly according to the givenratio
. Ifmask
is specified,ratio
will be ignored. The shape and dtype must be the same asx
and should be on the same device. Note that iDeep will not be used for this function if mask is specified, as iDeep does not support it. - return_mask (bool) – If
True
, the mask used for dropout is returned together with the output variable. The returned mask can later be reused by passing it tomask
argument.
Returns: When
return_mask
isFalse
(default), returns the output variable. WhenTrue
, returns the tuple of the output variable and mask (ndarray). The mask will be on the same device as the input. The mask will becomeNone
whenchainer.config.train
is set toFalse
.Return type: See the paper by G. Hinton: Improving neural networks by preventing co-adaptation of feature detectors.
Example
>>> x = np.array([[-1, 0], [2, -3], [-2, 1]], np.float32) >>> with chainer.using_config('train', True): ... y = F.dropout(x) >>> y.data array([[-2., 0.], [ 4., -6.], [-0., 2.]], dtype=float32) >>> with chainer.using_config('train', True): ... y = F.dropout(x, ratio=0.0) # dropout returns original input if ratio=0.0 >>> (x == y.data).all() True >>> with chainer.using_config('train', False): ... y = F.dropout(x) # dropout in test mode returns original input >>> (x == y.data).all() True
- x (