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 651
Added Inverse Square Root Linear Unit (ISRLU) activation layer #455
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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,71 @@ | ||
# -*- 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_square(x): | ||
return x / K.sqrt(1 + self.alphas * K.square(x)) | ||
|
||
def identity(x): | ||
return x | ||
|
||
return K.switch(K.less(x, K.zeros_like(x)), inverse_square(x), identity(x)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why introducing the |
||
|
||
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())) |
15 changes: 15 additions & 0 deletions
15
tests/keras_contrib/layers/advanced_activations/test_isrlu.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 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__]) |
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.
Name
inverse_square
might not be appropriate as the function does not exactly return1/x^2