def spatial_dropout(x, keep_prob, seed=1234):
# x is a convnet activation with shape BxWxHxF where F is the
# number of feature maps for that layer
# keep_prob is the proportion of feature maps we want to keep
# get the batch size and number of feature maps
num_feature_maps = [tf.shape(x)[0], tf.shape(x)[3]]
# get some uniform noise between keep_prob and 1 + keep_prob
random_tensor = keep_prob
random_tensor += tf.random_uniform(num_feature_maps,
seed=seed,
dtype=x.dtype)
# if we take the floor of this, we get a binary matrix where
# (1-keep_prob)% of the values are 0 and the rest are 1
binary_tensor = tf.floor(random_tensor)
# Reshape to multiply our feature maps by this tensor correctly
binary_tensor = tf.reshape(binary_tensor,
[-1, 1, 1, tf.shape(x)[3]])
# Zero out feature maps where appropriate; scale up to compensate
ret = tf.div(x, keep_prob) * binary_tensor
return ret