Skip to content

Commit

Permalink
Finishing Colincsl's SpatialDropout1D (keras-team#4416)
Browse files Browse the repository at this point in the history
* Added SpatialDropout1D

This is a straightforward modification of SpatialDropout2D but for 1D data.

* Added SpatialDropout1D to docs

* SpatialDropout1D test

* Fixed indent issue

* Combined TF and TH dimension conditions

Use the same 1D dimensions for TensorFlow and Theano in SpatialDropout1D.

* trailing whitespace

* Removed dim_ordering variable

* Removing dim_ordering values

removing dim_ordering values as requested
  • Loading branch information
EdwardRaff authored and fchollet committed Nov 19, 2016
1 parent 6b04add commit 97484ec
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/autogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
core.Dense,
core.Activation,
core.Dropout,
core.SpatialDropout1D,
core.SpatialDropout2D,
core.SpatialDropout3D,
core.Flatten,
Expand Down
31 changes: 31 additions & 0 deletions keras/layers/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,37 @@ def get_config(self):
return dict(list(base_config.items()) + list(config.items()))


class SpatialDropout1D(Dropout):
'''This version performs the same function as Dropout, however it drops
entire 1D feature maps instead of individual elements. If adjacent frames
within feature maps are strongly correlated (as is normally the case in
early convolution layers) then regular dropout will not regularize the
activations and will otherwise just result in an effective learning rate
decrease. In this case, SpatialDropout1D will help promote independence
between feature maps and should be used instead.
# Arguments
p: float between 0 and 1. Fraction of the input units to drop.
# Input shape
3D tensor with shape:
`(samples, timesteps, channels)`
# Output shape
Same as input
# References
- [Efficient Object Localization Using Convolutional Networks](https://arxiv.org/pdf/1411.4280.pdf)
'''
def __init__(self, p, **kwargs):
super(SpatialDropout1D, self).__init__(p, **kwargs)

def _get_noise_shape(self, x):
input_shape = K.shape(x)
noise_shape = (input_shape[0], 1, input_shape[2])
return noise_shape


class SpatialDropout2D(Dropout):
'''This version performs the same function as Dropout, however it drops
entire 2D feature maps instead of individual elements. If adjacent pixels
Expand Down
4 changes: 4 additions & 0 deletions tests/keras/layers/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ def test_dropout():
kwargs={'p': 0.5},
input_shape=(3, 2))

layer_test(core.SpatialDropout1D,
kwargs={'p': 0.5},
input_shape=(2, 3, 4))

layer_test(core.SpatialDropout2D,
kwargs={'p': 0.5},
input_shape=(2, 3, 4, 5))
Expand Down

0 comments on commit 97484ec

Please sign in to comment.