Skip to content

Commit

Permalink
Added support for dynamic noise_shape in Dropout (keras-team#7999)
Browse files Browse the repository at this point in the history
* Added support for dynamic noise_shape in Dropout

* handle the case noise_shape=None
  • Loading branch information
MartinXPN authored and ozabluda committed Oct 2, 2017
1 parent 582f8f8 commit 487e62c
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions keras/layers/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,14 @@ def __init__(self, rate, noise_shape=None, seed=None, **kwargs):
self.seed = seed
self.supports_masking = True

def _get_noise_shape(self, _):
return self.noise_shape
def _get_noise_shape(self, inputs):
if self.noise_shape is None:
return self.noise_shape

symbolic_shape = K.shape(inputs)
noise_shape = [symbolic_shape[axis] if shape is None else shape
for axis, shape in enumerate(self.noise_shape)]
return tuple(noise_shape)

def call(self, inputs, training=None):
if 0. < self.rate < 1.:
Expand All @@ -112,7 +118,9 @@ def dropped_inputs():
return inputs

def get_config(self):
config = {'rate': self.rate}
config = {'rate': self.rate,
'noise_shape': self.noise_shape,
'seed': self.seed}
base_config = super(Dropout, self).get_config()
return dict(list(base_config.items()) + list(config.items()))

Expand Down

0 comments on commit 487e62c

Please sign in to comment.