Skip to content

Instantly share code, notes, and snippets.

@ypeleg
Last active October 6, 2020 06:05
Show Gist options
  • Save ypeleg/3af35d07d7f659f387952c9843849772 to your computer and use it in GitHub Desktop.
Save ypeleg/3af35d07d7f659f387952c9843849772 to your computer and use it in GitHub Desktop.
import tensorflow as tf
from tensorflow.keras.layers import Activation
from tensorflow.keras.utils import get_custom_objects
class Mish(Activation):
'''
Mish Activation Function.
.. math::
mish(x) = x * tanh(softplus(x)) = x * tanh(ln(1 + e^{x}))
Shape:
- Input: 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: Same shape as the input.
Examples:
>>> X = Activation('Mish', name="conv1_act")(X_input)
'''
def __init__(self, activation, **kwargs):
super(Mish, self).__init__(activation, **kwargs)
self.__name__ = 'Mish'
def mish(inputs):
return inputs * tf.math.tanh(tf.math.softplus(inputs))
get_custom_objects().update({'Mish': Mish(mish)})
@yuvraj091
Copy link

When I write your code and call the activation function this comes out. Can you please help me write the code ?


ValueError Traceback (most recent call last)
in
8 Flatten(),
9 Dropout(0.5),
---> 10 Dense(50, activation='Mish'),
11 Dense(2, activation='softmax')
12 ])

C:\User\Anaconda\envs\python-cvcourse\lib\site-packages\keras\legacy\interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your ' + object_name + 90 ' call to the Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper

C:\Users\Anaconda\envs\python-cvcourse\lib\site-packages\keras\layers\core.py in init(self, units, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, **kwargs)
839 super(Dense, self).init(**kwargs)
840 self.units = units
--> 841 self.activation = activations.get(activation)
842 self.use_bias = use_bias
843 self.kernel_initializer = initializers.get(kernel_initializer)

C:\Users\Anaconda\envs\python-cvcourse\lib\site-packages\keras\activations.py in get(identifier)
187 if isinstance(identifier, six.string_types):
188 identifier = str(identifier)
--> 189 return deserialize(identifier)
190 elif callable(identifier):
191 if isinstance(identifier, Layer):

C:\Users\Anaconda\envs\python-cvcourse\lib\site-packages\keras\activations.py in deserialize(name, custom_objects)
168 module_objects=globals(),
169 custom_objects=custom_objects,
--> 170 printable_module_name='activation function')
171
172

C:\Users\Anaconda\envs\python-cvcourse\lib\site-packages\keras\utils\generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
163 if fn is None:
164 raise ValueError('Unknown ' + printable_module_name +
--> 165 ':' + function_name)
166 return fn
167 else:

ValueError: Unknown activation function:Mish

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment