Last active
September 21, 2017 10:58
-
-
Save zhpmatrix/c122adadcd76334a06f5c84f1ede4b23 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dropout(x, level, noise_shape=None, seed=None): | |
"""Sets entries in `x` to zero at random, | |
while scaling the entire tensor. | |
# Arguments | |
x: tensor | |
level: fraction of the entries in the tensor | |
that will be set to 0. | |
noise_shape: shape for randomly generated keep/drop flags, | |
must be broadcastable to the shape of `x` | |
seed: random seed to ensure determinism. | |
""" | |
if level < 0. or level >= 1: | |
raise ValueError('Dropout level must be in interval [0, 1[.') | |
if seed is None: | |
seed = np.random.randint(1, 10e6) | |
if isinstance(noise_shape, list): | |
noise_shape = tuple(noise_shape) | |
rng = RandomStreams(seed=seed) | |
retain_prob = 1. - level | |
if noise_shape is None: | |
random_tensor = rng.binomial(x.shape, p=retain_prob, dtype=x.dtype) | |
else: | |
random_tensor = rng.binomial(noise_shape, p=retain_prob, dtype=x.dtype) | |
random_tensor = T.patternbroadcast(random_tensor, | |
[dim == 1 for dim in noise_shape]) | |
x *= random_tensor | |
# 保证网络的每一层在训练阶段和测试阶段数据分布相同 | |
x /= retain_prob | |
return x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment