Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mish #1068

Merged
merged 7 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/modules/activation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ For more complex activation, TensorFlow API will be required.
sign
hard_tanh
pixel_wise_softmax
mish

Ramp
------
Expand Down Expand Up @@ -68,6 +69,10 @@ Pixel-wise softmax
--------------------
.. autofunction:: pixel_wise_softmax

mish
---------
.. autofunction:: mish

Parametric activation
------------------------------
See ``tensorlayer.layers``.
24 changes: 24 additions & 0 deletions tensorlayer/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'htanh',
'hard_tanh',
'pixel_wise_softmax',
'mish',
]


Expand Down Expand Up @@ -339,6 +340,29 @@ def pixel_wise_softmax(x, name='pixel_wise_softmax'):
return tf.nn.softmax(x)


def mish(x):
"""Mish activation function.

Mish is a novel smooth and non-monotonic neural activation function.

Parameters
----------
x : Tensor
input.

Returns
-------
Tensor
A ``Tensor`` in the same type as ``x``.

References
----------
- `Mish: A Self Regularized Non-Monotonic Neural Activation Function [Diganta Misra, 2019]<https://arxiv.org/abs/1908.08681>`__

"""
return x * tf.math.tanh(tf.math.softplus(x))


# Alias
lrelu = leaky_relu
lrelu6 = leaky_relu6
Expand Down
10 changes: 9 additions & 1 deletion tests/test_activations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import unittest

import tensorflow as tf

import numpy as np
import tensorlayer as tl
from tests.utils import CustomTestCase

Expand Down Expand Up @@ -116,6 +116,14 @@ def test_swish(self):

self.assertAlmostEqual(computed_output.numpy(), good_output, places=5)

def test_mish(self):
for i in range(-5, 15):
good_output = i * np.tanh(np.math.log(1 + np.math.exp(i)))

computed_output = tl.act.mish(float(i))

self.assertAlmostEqual(computed_output.numpy(), good_output, places=5)


if __name__ == '__main__':

Expand Down