From f0020bf86f52b0cdbd6b8c35bc94c327fde83def Mon Sep 17 00:00:00 2001 From: Tarun S Paparaju Date: Thu, 14 Feb 2019 15:35:44 +0530 Subject: [PATCH 01/24] Update __init__.py --- keras_contrib/layers/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/keras_contrib/layers/__init__.py b/keras_contrib/layers/__init__.py index bc460c6e6..3968c09b7 100644 --- a/keras_contrib/layers/__init__.py +++ b/keras_contrib/layers/__init__.py @@ -1,5 +1,6 @@ from __future__ import absolute_import +from .advanced_activations.isrlu import ISRLU from .advanced_activations.pelu import PELU from .advanced_activations.srelu import SReLU from .advanced_activations.swish import Swish From e2f6d3b044497d2c4b76e207bf195df8aa8c0422 Mon Sep 17 00:00:00 2001 From: Tarun S Paparaju Date: Thu, 14 Feb 2019 15:36:13 +0530 Subject: [PATCH 02/24] Create isrlu.py --- keras_contrib/layers/advanced_activations/isrlu.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 keras_contrib/layers/advanced_activations/isrlu.py diff --git a/keras_contrib/layers/advanced_activations/isrlu.py b/keras_contrib/layers/advanced_activations/isrlu.py new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/keras_contrib/layers/advanced_activations/isrlu.py @@ -0,0 +1 @@ + From 3737f2895cc7ff7ae997b02b2f89e18f33ee84d9 Mon Sep 17 00:00:00 2001 From: Tarun S Paparaju Date: Thu, 14 Feb 2019 15:50:34 +0530 Subject: [PATCH 03/24] Update isrlu.py --- .../layers/advanced_activations/isrlu.py | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/keras_contrib/layers/advanced_activations/isrlu.py b/keras_contrib/layers/advanced_activations/isrlu.py index 8b1378917..5019f29ce 100644 --- a/keras_contrib/layers/advanced_activations/isrlu.py +++ b/keras_contrib/layers/advanced_activations/isrlu.py @@ -1 +1,68 @@ +# -*- coding: utf-8 -*- +from keras import backend as K +from keras.layers import Layer + +class ISRLU(Layer): + """Inverse Square Root Linear Unit + See: https://arxiv.org/pdf/1710.09967.pdf by AI Perf + Reference: https://en.wikipedia.org/wiki/Activation_function + Inverse Square Root Linear activation f(α, x): + x >= 0: x + x < 0: x / sqrt(1 + α * x^2) + # Input shape + Arbitrary. Use the keyword argument `input_shape` + (tuple of integers, does not include the samples axis) + when using this layer as the first layer in a model. + # Output shape + Same shape as the input. + # Arguments + alpha: Value of the alpha weights (float) + NOTE : This function can become unstable for + negative values of α (it may return + NaNs). In particular, this happens when + α < 0 and x < -1/sqrt(α) or x > 1/sqrt(α). + If this happens, try limiting the magnitude + of α below a certain threshold, such that + 1 + α * x^2 is always positive. + Alternatively, you can normalize the inputs + into fixed ranges before passing them to ISRLU. + Adjust the value of α based on your specific + dataset and use-case. + # Example + model = Sequential() + model.add(Dense(5, input_shape=(15,)) + model.add(ISRLU(alpha=-0.3)) + """ + def __init__(self, + alpha=0.1, + **kwargs): + + super(ISRLU, self).__init__(**kwargs) + self.alpha = alpha + + def alpha_initializer(self, input_shape): + return self.alpha * K.ones(input_shape) + + def build(self, input_shape): + new_input_shape = input_shape[1:] + self.alphas = self.add_weight(shape=new_input_shape, + name='{}_alphas'.format(self.name), + initializer=self.alpha_initializer, + trainable=False) + self.build = True + + def call(self, x): + def inverse_quadratic_square_root(x): + return x / K.sqrt(1 + self.alphas * K.square(x)) + + return K.switch(K.less(x, K.zeros_like(x)), inverse_quadratic_square_root(x), x) + + def compute_output_shape(self, input_shape): + return input_shape + + def get_config(self): + config = {'alpha': self.alpha} + base_config = super(ISRLU, self).get_config() + base_config['trainable'] = False + return dict(list(base_config.items()) + list(config.items())) From 6e9d6efe81e312dc17bc0750657cf0dbbcda2899 Mon Sep 17 00:00:00 2001 From: Tarun S Paparaju Date: Thu, 14 Feb 2019 15:51:14 +0530 Subject: [PATCH 04/24] Create test_isrlu.py --- tests/keras_contrib/layers/advanced_activations/test_isrlu.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 tests/keras_contrib/layers/advanced_activations/test_isrlu.py diff --git a/tests/keras_contrib/layers/advanced_activations/test_isrlu.py b/tests/keras_contrib/layers/advanced_activations/test_isrlu.py new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/tests/keras_contrib/layers/advanced_activations/test_isrlu.py @@ -0,0 +1 @@ + From 28a3a8a569c0c085a51ad378a7f12c4160224519 Mon Sep 17 00:00:00 2001 From: Tarun S Paparaju Date: Thu, 14 Feb 2019 15:51:33 +0530 Subject: [PATCH 05/24] Update test_isrlu.py --- .../layers/advanced_activations/test_isrlu.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/keras_contrib/layers/advanced_activations/test_isrlu.py b/tests/keras_contrib/layers/advanced_activations/test_isrlu.py index 8b1378917..ffbad40bb 100644 --- a/tests/keras_contrib/layers/advanced_activations/test_isrlu.py +++ b/tests/keras_contrib/layers/advanced_activations/test_isrlu.py @@ -1 +1,15 @@ +# -*- coding: utf-8 -*- +import pytest +from keras_contrib.utils.test_utils import layer_test +from keras_contrib.layers import ISRLU + +@pytest.mark.parametrize('alpha', [0.2, 0.3, -0.01]) +def test_isrlu(alpha): + layer_test(ISRLU, + kwargs={'alpha': alpha}, + input_shape=(2, 3, 4)) + + +if __name__ == '__main__': + pytest.main([__file__]) From 19fc88eb3315492b81a22d8f13cf53b12e4d3180 Mon Sep 17 00:00:00 2001 From: Tarun S Paparaju Date: Thu, 14 Feb 2019 15:53:36 +0530 Subject: [PATCH 06/24] Update CODEOWNERS --- CODEOWNERS | 1 + 1 file changed, 1 insertion(+) diff --git a/CODEOWNERS b/CODEOWNERS index afa98df95..cd1b1b2c9 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -26,6 +26,7 @@ keras_contrib/callbacks/snapshot.py @titu1994 # layers +keras_contrib/layers/advanced_activations/isrlu.py @SriRangaTarun keras_contrib/layers/advanced_activations/sinerelu.py @wilderrodrigues keras_contrib/layers/advanced_activations/swish.py @gabrieldemarmiesse keras_contrib/layers/convolutional/subpixelupscaling.py @titu1994 From 11daeea2d2568d1dce0b79cc044fdf12292ff658 Mon Sep 17 00:00:00 2001 From: Tarun S Paparaju Date: Thu, 14 Feb 2019 20:45:17 +0530 Subject: [PATCH 07/24] Fix small mistake in docs --- keras_contrib/layers/advanced_activations/isrlu.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keras_contrib/layers/advanced_activations/isrlu.py b/keras_contrib/layers/advanced_activations/isrlu.py index 5019f29ce..6efbb120f 100644 --- a/keras_contrib/layers/advanced_activations/isrlu.py +++ b/keras_contrib/layers/advanced_activations/isrlu.py @@ -21,7 +21,7 @@ class ISRLU(Layer): NOTE : This function can become unstable for negative values of α (it may return NaNs). In particular, this happens when - α < 0 and x < -1/sqrt(α) or x > 1/sqrt(α). + α < 0 and x < -1/sqrt(α). If this happens, try limiting the magnitude of α below a certain threshold, such that 1 + α * x^2 is always positive. From 01f25ddc1e397f386ada2108034918a5642a3011 Mon Sep 17 00:00:00 2001 From: Tarun S Paparaju Date: Fri, 15 Feb 2019 16:50:22 +0530 Subject: [PATCH 08/24] Set self.trainable = False --- keras_contrib/layers/advanced_activations/isrlu.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/keras_contrib/layers/advanced_activations/isrlu.py b/keras_contrib/layers/advanced_activations/isrlu.py index 6efbb120f..454122d98 100644 --- a/keras_contrib/layers/advanced_activations/isrlu.py +++ b/keras_contrib/layers/advanced_activations/isrlu.py @@ -40,6 +40,7 @@ def __init__(self, super(ISRLU, self).__init__(**kwargs) self.alpha = alpha + self.trainable = False def alpha_initializer(self, input_shape): return self.alpha * K.ones(input_shape) @@ -62,7 +63,7 @@ def compute_output_shape(self, input_shape): return input_shape def get_config(self): - config = {'alpha': self.alpha} + config = {'alpha': self.alpha, + 'trainable': self.trainable} base_config = super(ISRLU, self).get_config() - base_config['trainable'] = False return dict(list(base_config.items()) + list(config.items())) From 338bf7954bc285651f3140a745253cba1baa2982 Mon Sep 17 00:00:00 2001 From: Tarun S Paparaju Date: Sat, 16 Feb 2019 15:02:20 +0530 Subject: [PATCH 09/24] Delete isrlu.py --- .../layers/advanced_activations/isrlu.py | 69 ------------------- 1 file changed, 69 deletions(-) delete mode 100644 keras_contrib/layers/advanced_activations/isrlu.py diff --git a/keras_contrib/layers/advanced_activations/isrlu.py b/keras_contrib/layers/advanced_activations/isrlu.py deleted file mode 100644 index 454122d98..000000000 --- a/keras_contrib/layers/advanced_activations/isrlu.py +++ /dev/null @@ -1,69 +0,0 @@ -# -*- coding: utf-8 -*- -from keras import backend as K -from keras.layers import Layer - - -class ISRLU(Layer): - """Inverse Square Root Linear Unit - See: https://arxiv.org/pdf/1710.09967.pdf by AI Perf - Reference: https://en.wikipedia.org/wiki/Activation_function - Inverse Square Root Linear activation f(α, x): - x >= 0: x - x < 0: x / sqrt(1 + α * x^2) - # Input shape - Arbitrary. Use the keyword argument `input_shape` - (tuple of integers, does not include the samples axis) - when using this layer as the first layer in a model. - # Output shape - Same shape as the input. - # Arguments - alpha: Value of the alpha weights (float) - NOTE : This function can become unstable for - negative values of α (it may return - NaNs). In particular, this happens when - α < 0 and x < -1/sqrt(α). - If this happens, try limiting the magnitude - of α below a certain threshold, such that - 1 + α * x^2 is always positive. - Alternatively, you can normalize the inputs - into fixed ranges before passing them to ISRLU. - Adjust the value of α based on your specific - dataset and use-case. - # Example - model = Sequential() - model.add(Dense(5, input_shape=(15,)) - model.add(ISRLU(alpha=-0.3)) - """ - def __init__(self, - alpha=0.1, - **kwargs): - - super(ISRLU, self).__init__(**kwargs) - self.alpha = alpha - self.trainable = False - - def alpha_initializer(self, input_shape): - return self.alpha * K.ones(input_shape) - - def build(self, input_shape): - new_input_shape = input_shape[1:] - self.alphas = self.add_weight(shape=new_input_shape, - name='{}_alphas'.format(self.name), - initializer=self.alpha_initializer, - trainable=False) - self.build = True - - def call(self, x): - def inverse_quadratic_square_root(x): - return x / K.sqrt(1 + self.alphas * K.square(x)) - - return K.switch(K.less(x, K.zeros_like(x)), inverse_quadratic_square_root(x), x) - - def compute_output_shape(self, input_shape): - return input_shape - - def get_config(self): - config = {'alpha': self.alpha, - 'trainable': self.trainable} - base_config = super(ISRLU, self).get_config() - return dict(list(base_config.items()) + list(config.items())) From fbc1a72b79ae0994e94ff8b99cf915947ee5681f Mon Sep 17 00:00:00 2001 From: Tarun S Paparaju Date: Sat, 16 Feb 2019 15:03:03 +0530 Subject: [PATCH 10/24] Create pseu.py --- .../layers/advanced_activations/pseu.py | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 keras_contrib/layers/advanced_activations/pseu.py diff --git a/keras_contrib/layers/advanced_activations/pseu.py b/keras_contrib/layers/advanced_activations/pseu.py new file mode 100644 index 000000000..7e8800228 --- /dev/null +++ b/keras_contrib/layers/advanced_activations/pseu.py @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- +from keras import backend as K +from keras.layers import Layer + + +class PSEU(Layer): + """Parametric Soft Exponential Unit + See: https://arxiv.org/pdf/1602.01321.pdf by Godfrey and Gashler + Reference: https://github.com/keras-team/keras/issues/3842 (@hobson) + Soft Exponential f(α, x): + α == 0: x + α > 0: (exp(αx)-1) / α + α + α < 0: -ln(1-α(x + α)) / α + # Input shape + Arbitrary. Use the keyword argument `input_shape` + (tuple of integers, does not include the samples axis) + when using this layer as the first layer in a model. + # Output shape + Same shape as the input. + # Arguments + alpha: Value of the alpha weights (float) + NOTE : This function can become unstable for + negative values of α. In particular, the + function returns NaNs when α < 0 and x <= 1/α + (where x is the input). + If the function starts returning NaNs for α < 0, + try decreasing the magnitude of α. + Alternatively, you can normalize the data into fixed + ranges before passing it to PSEU. + Adjust α based on your specific dataset + and use-case. + # Example + model = Sequential() + model.add(Dense(10, input_shape=(5,)) + model.add(PSEU(alpha=0.2)) + """ + def __init__(self, + alpha=0.1, + **kwargs): + + super(PSEU, self).__init__(**kwargs) + self.alpha = alpha + + def alpha_initializer(self, input_shape): + return self.alpha * K.ones(input_shape) + + def build(self, input_shape): + new_input_shape = input_shape[1:] + self.alphas = self.add_weight(shape=new_input_shape, + name='{}_alphas'.format(self.name), + initializer=self.alpha_initializer, + trainable=False) + self.build = True + + def call(self, x): + if self.alpha < 0: + return - K.log(1 - self.alphas * (x + self.alphas)) / self.alphas + elif self.alpha > 0: + return self.alphas + (K.exp(self.alphas * x) - 1.) / self.alphas + else: + return x + + def compute_output_shape(self, input_shape): + return input_shape + + def get_config(self): + config = {'alpha': self.alpha} + base_config = super(PSEU, self).get_config() + return dict(list(base_config.items()) + list(config.items())) From 6190c53fd20cd5c4b577e53416ca7158829f8aaa Mon Sep 17 00:00:00 2001 From: Tarun S Paparaju Date: Sat, 16 Feb 2019 15:03:50 +0530 Subject: [PATCH 11/24] Update __init__.py --- keras_contrib/layers/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keras_contrib/layers/__init__.py b/keras_contrib/layers/__init__.py index 3968c09b7..0607d41f8 100644 --- a/keras_contrib/layers/__init__.py +++ b/keras_contrib/layers/__init__.py @@ -1,7 +1,7 @@ from __future__ import absolute_import -from .advanced_activations.isrlu import ISRLU from .advanced_activations.pelu import PELU +from .advanced_activations.pseu import PSEU from .advanced_activations.srelu import SReLU from .advanced_activations.swish import Swish from .advanced_activations.sinerelu import SineReLU From cdb5f944c055e6c1ea631bcf6094f436395efde8 Mon Sep 17 00:00:00 2001 From: Tarun S Paparaju Date: Sat, 16 Feb 2019 15:04:28 +0530 Subject: [PATCH 12/24] Create test_pseu.py --- .../layers/advanced_activations/test_pseu.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 tests/keras_contrib/layers/advanced_activations/test_pseu.py diff --git a/tests/keras_contrib/layers/advanced_activations/test_pseu.py b/tests/keras_contrib/layers/advanced_activations/test_pseu.py new file mode 100644 index 000000000..8fea9d295 --- /dev/null +++ b/tests/keras_contrib/layers/advanced_activations/test_pseu.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +import pytest +from keras_contrib.utils.test_utils import layer_test +from keras_contrib.layers import PSEU + + +@pytest.mark.parametrize('alpha', [-0.1, 0., 0.1]) +def test_pseu(alpha): + layer_test(PSEU, + kwargs={'alpha': alpha}, + input_shape=(2, 3, 4)) + + +if __name__ == '__main__': + pytest.main([__file__]) From 8afecc62e7f31015fd50f4f3540e41675fef2ce5 Mon Sep 17 00:00:00 2001 From: Tarun S Paparaju Date: Sat, 16 Feb 2019 15:04:44 +0530 Subject: [PATCH 13/24] Delete test_isrlu.py --- .../layers/advanced_activations/test_isrlu.py | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 tests/keras_contrib/layers/advanced_activations/test_isrlu.py diff --git a/tests/keras_contrib/layers/advanced_activations/test_isrlu.py b/tests/keras_contrib/layers/advanced_activations/test_isrlu.py deleted file mode 100644 index ffbad40bb..000000000 --- a/tests/keras_contrib/layers/advanced_activations/test_isrlu.py +++ /dev/null @@ -1,15 +0,0 @@ -# -*- coding: utf-8 -*- -import pytest -from keras_contrib.utils.test_utils import layer_test -from keras_contrib.layers import ISRLU - - -@pytest.mark.parametrize('alpha', [0.2, 0.3, -0.01]) -def test_isrlu(alpha): - layer_test(ISRLU, - kwargs={'alpha': alpha}, - input_shape=(2, 3, 4)) - - -if __name__ == '__main__': - pytest.main([__file__]) From e45bf669666b0b8186205a0dbcbc6b85c1edfdf1 Mon Sep 17 00:00:00 2001 From: Tarun S Paparaju Date: Sat, 16 Feb 2019 15:05:04 +0530 Subject: [PATCH 14/24] Update CODEOWNERS --- CODEOWNERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CODEOWNERS b/CODEOWNERS index cd1b1b2c9..a233ecdef 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -26,7 +26,7 @@ keras_contrib/callbacks/snapshot.py @titu1994 # layers -keras_contrib/layers/advanced_activations/isrlu.py @SriRangaTarun +keras_contrib/layers/advanced_activations/pseu.py @SriRangaTarun keras_contrib/layers/advanced_activations/sinerelu.py @wilderrodrigues keras_contrib/layers/advanced_activations/swish.py @gabrieldemarmiesse keras_contrib/layers/convolutional/subpixelupscaling.py @titu1994 From f05a50308fab4883c91bb57f6b933a153b1e1a4d Mon Sep 17 00:00:00 2001 From: Tarun S Paparaju Date: Sat, 16 Feb 2019 15:16:13 +0530 Subject: [PATCH 15/24] Fix tf.keras compatibility --- keras_contrib/layers/advanced_activations/pseu.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/keras_contrib/layers/advanced_activations/pseu.py b/keras_contrib/layers/advanced_activations/pseu.py index 7e8800228..7c28f7bd2 100644 --- a/keras_contrib/layers/advanced_activations/pseu.py +++ b/keras_contrib/layers/advanced_activations/pseu.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from keras import backend as K from keras.layers import Layer +from keras_contrib.utils.test_utils import to_tuple class PSEU(Layer): @@ -45,6 +46,7 @@ def alpha_initializer(self, input_shape): return self.alpha * K.ones(input_shape) def build(self, input_shape): + input_shape = to_tuple(input_shape) new_input_shape = input_shape[1:] self.alphas = self.add_weight(shape=new_input_shape, name='{}_alphas'.format(self.name), From 9ce8c549c1d1e0cdc72d8c42169a7467180baa18 Mon Sep 17 00:00:00 2001 From: Tarun S Paparaju Date: Sat, 16 Feb 2019 15:28:16 +0530 Subject: [PATCH 16/24] Make PSEU compatible with tf.keras --- keras_contrib/layers/advanced_activations/pseu.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/keras_contrib/layers/advanced_activations/pseu.py b/keras_contrib/layers/advanced_activations/pseu.py index 7c28f7bd2..af521993b 100644 --- a/keras_contrib/layers/advanced_activations/pseu.py +++ b/keras_contrib/layers/advanced_activations/pseu.py @@ -42,8 +42,8 @@ def __init__(self, super(PSEU, self).__init__(**kwargs) self.alpha = alpha - def alpha_initializer(self, input_shape): - return self.alpha * K.ones(input_shape) + def alpha_initializer(self, input_shape, dtype='float32'): + return self.alpha * K.ones(input_shape, dtype=dtype) def build(self, input_shape): input_shape = to_tuple(input_shape) From cd6e7601df2b436160a2dbf7d9cbd605876d15b6 Mon Sep 17 00:00:00 2001 From: Tarun S Paparaju Date: Sat, 16 Feb 2019 15:30:48 +0530 Subject: [PATCH 17/24] Set self.trainable=True in PSEU --- keras_contrib/layers/advanced_activations/pseu.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/keras_contrib/layers/advanced_activations/pseu.py b/keras_contrib/layers/advanced_activations/pseu.py index af521993b..805df19bf 100644 --- a/keras_contrib/layers/advanced_activations/pseu.py +++ b/keras_contrib/layers/advanced_activations/pseu.py @@ -41,6 +41,7 @@ def __init__(self, super(PSEU, self).__init__(**kwargs) self.alpha = alpha + self.trainable=False def alpha_initializer(self, input_shape, dtype='float32'): return self.alpha * K.ones(input_shape, dtype=dtype) @@ -66,6 +67,7 @@ def compute_output_shape(self, input_shape): return input_shape def get_config(self): - config = {'alpha': self.alpha} + config = {'alpha': self.alpha, + 'trainable': self.trainable} base_config = super(PSEU, self).get_config() return dict(list(base_config.items()) + list(config.items())) From 3b92f4255d619ad43e64d8bfda7bbd534232fd14 Mon Sep 17 00:00:00 2001 From: Tarun S Paparaju Date: Sat, 16 Feb 2019 15:49:15 +0530 Subject: [PATCH 18/24] Make PSEU tf.keras compatible --- keras_contrib/layers/advanced_activations/pseu.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/keras_contrib/layers/advanced_activations/pseu.py b/keras_contrib/layers/advanced_activations/pseu.py index 805df19bf..13bf66ade 100644 --- a/keras_contrib/layers/advanced_activations/pseu.py +++ b/keras_contrib/layers/advanced_activations/pseu.py @@ -41,10 +41,12 @@ def __init__(self, super(PSEU, self).__init__(**kwargs) self.alpha = alpha - self.trainable=False + self.trainable = False - def alpha_initializer(self, input_shape, dtype='float32'): - return self.alpha * K.ones(input_shape, dtype=dtype) + def alpha_initializer(self, input_shape, dtype='float32', partition_info=None): + return self.alpha * K.ones(input_shape, + dtype=dtype, + partition_info=partition_info) def build(self, input_shape): input_shape = to_tuple(input_shape) From 3f1888ebcb6d0beed2f46482e7c486284ac9ac30 Mon Sep 17 00:00:00 2001 From: Tarun S Paparaju Date: Sat, 16 Feb 2019 16:04:50 +0530 Subject: [PATCH 19/24] Make PSEU tf.keras compatible --- keras_contrib/layers/advanced_activations/pseu.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/keras_contrib/layers/advanced_activations/pseu.py b/keras_contrib/layers/advanced_activations/pseu.py index 13bf66ade..7af8200f9 100644 --- a/keras_contrib/layers/advanced_activations/pseu.py +++ b/keras_contrib/layers/advanced_activations/pseu.py @@ -2,6 +2,7 @@ from keras import backend as K from keras.layers import Layer from keras_contrib.utils.test_utils import to_tuple +from keras_contrib.utils.test_utils import is_tf_keras class PSEU(Layer): @@ -43,10 +44,16 @@ def __init__(self, self.alpha = alpha self.trainable = False - def alpha_initializer(self, input_shape, dtype='float32', partition_info=None): - return self.alpha * K.ones(input_shape, - dtype=dtype, - partition_info=partition_info) + if is_tf_keras: + def alpha_initializer(self, input_shape, dtype='float32', partition_info=None): + return self.alpha * K.ones(input_shape, + dtype=dtype, + partition_info=partition_info) + + else: + def alpha_initializer(self, input_shape, dtype='float32'): + return self.alpha * K.ones(input_shape, + dtype=dtype) def build(self, input_shape): input_shape = to_tuple(input_shape) From 567c009a2e9276e67f748bcdb7641a1e0701ccf5 Mon Sep 17 00:00:00 2001 From: Tarun S Paparaju Date: Sat, 16 Feb 2019 16:15:35 +0530 Subject: [PATCH 20/24] Make PSEU tf.keras compatible --- keras_contrib/layers/advanced_activations/pseu.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/keras_contrib/layers/advanced_activations/pseu.py b/keras_contrib/layers/advanced_activations/pseu.py index 7af8200f9..7d913eefc 100644 --- a/keras_contrib/layers/advanced_activations/pseu.py +++ b/keras_contrib/layers/advanced_activations/pseu.py @@ -47,8 +47,7 @@ def __init__(self, if is_tf_keras: def alpha_initializer(self, input_shape, dtype='float32', partition_info=None): return self.alpha * K.ones(input_shape, - dtype=dtype, - partition_info=partition_info) + dtype=dtype) else: def alpha_initializer(self, input_shape, dtype='float32'): From 7067a3100b4a781d5a3233f76e788d168eb0d963 Mon Sep 17 00:00:00 2001 From: Tarun S Paparaju Date: Sat, 16 Feb 2019 16:59:05 +0530 Subject: [PATCH 21/24] Use self.trainable instead of False --- keras_contrib/layers/advanced_activations/pseu.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keras_contrib/layers/advanced_activations/pseu.py b/keras_contrib/layers/advanced_activations/pseu.py index 7d913eefc..0ce5913d9 100644 --- a/keras_contrib/layers/advanced_activations/pseu.py +++ b/keras_contrib/layers/advanced_activations/pseu.py @@ -60,7 +60,7 @@ def build(self, input_shape): self.alphas = self.add_weight(shape=new_input_shape, name='{}_alphas'.format(self.name), initializer=self.alpha_initializer, - trainable=False) + trainable=self.trainable) self.build = True def call(self, x): From 5ad1db13bf379d024ca22cbd1c0af25aeb8c5b07 Mon Sep 17 00:00:00 2001 From: Tarun S Paparaju Date: Mon, 4 Mar 2019 17:39:54 +0530 Subject: [PATCH 22/24] Add **kwargs --- keras_contrib/layers/advanced_activations/pseu.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/keras_contrib/layers/advanced_activations/pseu.py b/keras_contrib/layers/advanced_activations/pseu.py index 0ce5913d9..07cac50f3 100644 --- a/keras_contrib/layers/advanced_activations/pseu.py +++ b/keras_contrib/layers/advanced_activations/pseu.py @@ -44,15 +44,9 @@ def __init__(self, self.alpha = alpha self.trainable = False - if is_tf_keras: - def alpha_initializer(self, input_shape, dtype='float32', partition_info=None): - return self.alpha * K.ones(input_shape, - dtype=dtype) - - else: - def alpha_initializer(self, input_shape, dtype='float32'): - return self.alpha * K.ones(input_shape, - dtype=dtype) + def alpha_initializer(self, input_shape, dtype='float32', **kwargs): + return self.alpha * K.ones(input_shape, + dtype=dtype) def build(self, input_shape): input_shape = to_tuple(input_shape) From 6cd2e7870da0219e33c65ef8ac5b915fbdb1ae86 Mon Sep 17 00:00:00 2001 From: Tarun S Paparaju Date: Mon, 4 Mar 2019 18:14:20 +0530 Subject: [PATCH 23/24] Update pseu.py --- keras_contrib/layers/advanced_activations/pseu.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keras_contrib/layers/advanced_activations/pseu.py b/keras_contrib/layers/advanced_activations/pseu.py index 07cac50f3..4a4fab288 100644 --- a/keras_contrib/layers/advanced_activations/pseu.py +++ b/keras_contrib/layers/advanced_activations/pseu.py @@ -61,7 +61,7 @@ def call(self, x): if self.alpha < 0: return - K.log(1 - self.alphas * (x + self.alphas)) / self.alphas elif self.alpha > 0: - return self.alphas + (K.exp(self.alphas * x) - 1.) / self.alphas + return self.alphas + (K.exp(self.alphas * x) - 1.) / self.alphasa else: return x From 072619823adde9434dbaa441bc42761ffd237b5c Mon Sep 17 00:00:00 2001 From: Tarun S Paparaju Date: Mon, 4 Mar 2019 18:14:30 +0530 Subject: [PATCH 24/24] Update pseu.py --- keras_contrib/layers/advanced_activations/pseu.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keras_contrib/layers/advanced_activations/pseu.py b/keras_contrib/layers/advanced_activations/pseu.py index 4a4fab288..07cac50f3 100644 --- a/keras_contrib/layers/advanced_activations/pseu.py +++ b/keras_contrib/layers/advanced_activations/pseu.py @@ -61,7 +61,7 @@ def call(self, x): if self.alpha < 0: return - K.log(1 - self.alphas * (x + self.alphas)) / self.alphas elif self.alpha > 0: - return self.alphas + (K.exp(self.alphas * x) - 1.) / self.alphasa + return self.alphas + (K.exp(self.alphas * x) - 1.) / self.alphas else: return x