This repository has been archived by the owner on Nov 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 650
Add Parametric Soft Exponential Unit (PSEU) activation layer #459
Open
SriRangaTarun
wants to merge
25
commits into
keras-team:master
Choose a base branch
from
SriRangaTarun:pseu
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
f0020bf
Update __init__.py
SriRangaTarun e2f6d3b
Create isrlu.py
SriRangaTarun 3737f28
Update isrlu.py
SriRangaTarun 6e9d6ef
Create test_isrlu.py
SriRangaTarun 28a3a8a
Update test_isrlu.py
SriRangaTarun 19fc88e
Update CODEOWNERS
SriRangaTarun 11daeea
Fix small mistake in docs
SriRangaTarun 01f25dd
Set self.trainable = False
SriRangaTarun 27362e0
Merge pull request #1 from keras-team/master
SriRangaTarun 338bf79
Delete isrlu.py
SriRangaTarun fbc1a72
Create pseu.py
SriRangaTarun 6190c53
Update __init__.py
SriRangaTarun cdb5f94
Create test_pseu.py
SriRangaTarun 8afecc6
Delete test_isrlu.py
SriRangaTarun e45bf66
Update CODEOWNERS
SriRangaTarun f05a503
Fix tf.keras compatibility
SriRangaTarun 9ce8c54
Make PSEU compatible with tf.keras
SriRangaTarun cd6e760
Set self.trainable=True in PSEU
SriRangaTarun 3b92f42
Make PSEU tf.keras compatible
SriRangaTarun 3f1888e
Make PSEU tf.keras compatible
SriRangaTarun 567c009
Make PSEU tf.keras compatible
SriRangaTarun 7067a31
Use self.trainable instead of False
SriRangaTarun 5ad1db1
Add **kwargs
SriRangaTarun 6cd2e78
Update pseu.py
SriRangaTarun 0726198
Update pseu.py
SriRangaTarun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# -*- coding: utf-8 -*- | ||
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): | ||
"""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 | ||
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 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), | ||
initializer=self.alpha_initializer, | ||
trainable=self.trainable) | ||
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, | ||
'trainable': self.trainable} | ||
base_config = super(PSEU, self).get_config() | ||
return dict(list(base_config.items()) + list(config.items())) |
15 changes: 15 additions & 0 deletions
15
tests/keras_contrib/layers/advanced_activations/test_pseu.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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__]) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
partition_info is a useless arg
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to separate is_tf_keras case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@RaphaelMeudec The initializer does not work in tf.keras without the partition_info argument.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you can use
**kwargs
to have only one declaration.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gabrieldemarmiesse Added **kwargs