Skip to content

Commit

Permalink
Add compute_output_shape methods explicitly when shape unchanged.
Browse files Browse the repository at this point in the history
  • Loading branch information
fchollet committed Jan 5, 2018
1 parent 0916859 commit 2b34969
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
17 changes: 16 additions & 1 deletion keras/layers/advanced_activations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
"""Advanced activation layers.
"""Layers that act as activation functions.
"""
from __future__ import absolute_import
from __future__ import division
Expand Down Expand Up @@ -50,6 +50,9 @@ def get_config(self):
base_config = super(LeakyReLU, self).get_config()
return dict(list(base_config.items()) + list(config.items()))

def compute_output_shape(self, input_shape):
return input_shape


class PReLU(Layer):
"""Parametric Rectified Linear Unit.
Expand Down Expand Up @@ -142,6 +145,9 @@ def get_config(self):
base_config = super(PReLU, self).get_config()
return dict(list(base_config.items()) + list(config.items()))

def compute_output_shape(self, input_shape):
return input_shape


class ELU(Layer):
"""Exponential Linear Unit.
Expand Down Expand Up @@ -178,6 +184,9 @@ def get_config(self):
base_config = super(ELU, self).get_config()
return dict(list(base_config.items()) + list(config.items()))

def compute_output_shape(self, input_shape):
return input_shape


class ThresholdedReLU(Layer):
"""Thresholded Rectified Linear Unit.
Expand Down Expand Up @@ -214,6 +223,9 @@ def get_config(self):
base_config = super(ThresholdedReLU, self).get_config()
return dict(list(base_config.items()) + list(config.items()))

def compute_output_shape(self, input_shape):
return input_shape


class Softmax(Layer):
"""Softmax activation function.
Expand Down Expand Up @@ -242,3 +254,6 @@ def get_config(self):
config = {'axis': self.axis}
base_config = super(Softmax, self).get_config()
return dict(list(base_config.items()) + list(config.items()))

def compute_output_shape(self, input_shape):
return input_shape
12 changes: 12 additions & 0 deletions keras/layers/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ def get_config(self):
base_config = super(Masking, self).get_config()
return dict(list(base_config.items()) + list(config.items()))

def compute_output_shape(self, input_shape):
return input_shape


class Dropout(Layer):
"""Applies Dropout to the input.
Expand Down Expand Up @@ -127,6 +130,9 @@ def get_config(self):
base_config = super(Dropout, self).get_config()
return dict(list(base_config.items()) + list(config.items()))

def compute_output_shape(self, input_shape):
return input_shape


class SpatialDropout1D(Dropout):
"""Spatial 1D version of Dropout.
Expand Down Expand Up @@ -301,6 +307,9 @@ def get_config(self):
base_config = super(Activation, self).get_config()
return dict(list(base_config.items()) + list(config.items()))

def compute_output_shape(self, input_shape):
return input_shape


class Reshape(Layer):
"""Reshapes an output to a certain shape.
Expand Down Expand Up @@ -902,3 +911,6 @@ def get_config(self):
'l2': self.l2}
base_config = super(ActivityRegularization, self).get_config()
return dict(list(base_config.items()) + list(config.items()))

def compute_output_shape(self, input_shape):
return input_shape
15 changes: 12 additions & 3 deletions keras/layers/noise.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
"""Noise regularization layers.
"""Layers that operate regularization via the addition of noise.
"""
from __future__ import absolute_import
from __future__ import division
Expand All @@ -9,7 +9,6 @@
from .. import backend as K
import numpy as np
from ..legacy import interfaces
from ..engine import InputSpec


class GaussianNoise(Layer):
Expand Down Expand Up @@ -52,6 +51,9 @@ def get_config(self):
base_config = super(GaussianNoise, self).get_config()
return dict(list(base_config.items()) + list(config.items()))

def compute_output_shape(self, input_shape):
return input_shape


class GaussianDropout(Layer):
"""Apply multiplicative 1-centered Gaussian noise.
Expand Down Expand Up @@ -96,6 +98,9 @@ def get_config(self):
base_config = super(GaussianDropout, self).get_config()
return dict(list(base_config.items()) + list(config.items()))

def compute_output_shape(self, input_shape):
return input_shape


class AlphaDropout(Layer):
"""Applies Alpha Dropout to the input.
Expand Down Expand Up @@ -142,7 +147,8 @@ def dropped_inputs(inputs=inputs, rate=self.rate, seed=self.seed):
scale = 1.0507009873554804934193349852946
alpha_p = -alpha * scale

kept_idx = K.greater_equal(K.random_uniform(noise_shape, seed=seed), rate)
kept_idx = K.greater_equal(K.random_uniform(noise_shape,
seed=seed), rate)
kept_idx = K.cast(kept_idx, K.floatx())

# Get affine transformation params
Expand All @@ -162,3 +168,6 @@ def get_config(self):
config = {'rate': self.rate}
base_config = super(AlphaDropout, self).get_config()
return dict(list(base_config.items()) + list(config.items()))

def compute_output_shape(self, input_shape):
return input_shape
3 changes: 3 additions & 0 deletions keras/layers/normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,6 @@ def get_config(self):
}
base_config = super(BatchNormalization, self).get_config()
return dict(list(base_config.items()) + list(config.items()))

def compute_output_shape(self, input_shape):
return input_shape

0 comments on commit 2b34969

Please sign in to comment.