From 63bd82a633afaeb85feb182d31354249fba12994 Mon Sep 17 00:00:00 2001 From: josephyearsley Date: Wed, 25 Jan 2017 22:28:24 +0000 Subject: [PATCH] Added Tests and Updated Doc String --- keras_contrib/layers/advanced_activations.py | 24 ++++++++++++------- pytest.ini | 10 ++++---- .../layers/test_advanced_activations.py | 14 +++++++++++ 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/keras_contrib/layers/advanced_activations.py b/keras_contrib/layers/advanced_activations.py index 199600b..2678f19 100644 --- a/keras_contrib/layers/advanced_activations.py +++ b/keras_contrib/layers/advanced_activations.py @@ -1,5 +1,6 @@ from .. import initializations from keras.engine import Layer +from keras.utils.generic_utils import get_custom_objects from .. import backend as K import numpy as np @@ -7,9 +8,9 @@ import numpy as np class PELU(Layer): """Parametric Exponential Linear Unit. It follows: - `f(x) = alphas * x for x < 0`, - `f(x) = x for x >= 0`, - where `alphas` is a learned array with the same shape as x. + `f(x) = alphas * (exp(x / betas) - 1) for x < 0`, + `f(x) = (alphas / betas) * x for x >= 0`, + where `alphas` & `betas` are learned arrays with the same shape as x. # Input shape Arbitrary. Use the keyword argument `input_shape` (tuple of integers, does not include the samples axis) @@ -17,7 +18,8 @@ class PELU(Layer): # Output shape Same shape as the input. # Arguments - init: initialization function for the weights. + alphas_init: initialization function for the alpha variable weights. + betas_init: initialization function for the beta variable weights. weights: initial weights, as a list of a single Numpy array. shared_axes: the axes along which to share learnable parameters for the activation function. @@ -31,9 +33,10 @@ class PELU(Layer): - [PARAMETRIC EXPONENTIAL LINEAR UNIT FOR DEEP CONVOLUTIONAL NEURAL NETWORKS](https://arxiv.org/abs/1605.09332v3) """ - def __init__(self, init='zero', weights=None, shared_axes=None, **kwargs): + def __init__(self, alphas_init='one', betas_init='one', weights=None, shared_axes=None, **kwargs): self.supports_masking = True - self.init = initializations.get(init) + self.alphas_init = initializations.get(alphas_init) + self.betas_init = initializations.get(betas_init) self.initial_weights = weights if not isinstance(shared_axes, (list, tuple)): self.shared_axes = [shared_axes] @@ -50,9 +53,9 @@ class PELU(Layer): self.param_broadcast[i - 1] = True # Initialised as ones to emulate the default ELU - self.alphas = K.ones(param_shape, dtype=K.floatx(), + self.alphas = self.alphas_init(param_shape, name='{}_alphas'.format(self.name)) - self.betas = K.ones(param_shape, dtype=K.floatx(), + self.betas = self.betas_init(param_shape, name='{}_betas'.format(self.name)) self.trainable_weights = [self.alphas, self.betas] @@ -71,6 +74,9 @@ class PELU(Layer): return pos + neg def get_config(self): - config = {'init': self.init.__name__} + config = {'alphas_init': self.alphas_init.__name__, + 'betas_init': self.betas_init.__name__} base_config = super(PELU, self).get_config() return dict(list(base_config.items()) + list(config.items())) + +get_custom_objects().update({"PELU": PELU}) diff --git a/pytest.ini b/pytest.ini index 079ed86..29e995d 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,10 +1,10 @@ # Configuration of py.test [pytest] -addopts=-v - -n 2 - --durations=10 - --cov-report term-missing - --cov=keras +#addopts=-v +# -n 2 +# --durations=10 +# --cov-report term-missing +# --cov=keras # Do not run tests in the build folder norecursedirs= build diff --git a/tests/keras_contrib/layers/test_advanced_activations.py b/tests/keras_contrib/layers/test_advanced_activations.py index d510fc9..a235497 100644 --- a/tests/keras_contrib/layers/test_advanced_activations.py +++ b/tests/keras_contrib/layers/test_advanced_activations.py @@ -1,5 +1,19 @@ import pytest from keras.utils.test_utils import layer_test, keras_test +from keras_contrib import layers + +@keras_test +def test_pelu(): + from keras_contrib.layers.advanced_activations import PELU + layer_test(PELU, kwargs={}, + input_shape=(2, 3, 4)) + + +@keras_test +def test_pelu_share(): + from keras_contrib.layers.advanced_activations import PELU + layer_test(PELU, kwargs={'shared_axes': 1}, + input_shape=(2, 3, 4)) if __name__ == '__main__':