From d959de526fd0df527f4934e0d42c54d51dcfb841 Mon Sep 17 00:00:00 2001 From: TAWSIF AHMED Date: Fri, 21 Oct 2022 11:05:31 +0600 Subject: [PATCH 1/4] Subclass method example Updated with an example on how to use keras-contrib activation functions in subclass method model architecture --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index 2b1ce92ad..22975d7a3 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,27 @@ model.fit(x=np.random.random((100, 10)), y=np.random.random((100, 100)), epochs= # Save our model model.save('example.h5') ``` +Modules from Keras-Contrib library in subclass method +```python +from tensorflow import keras +from tensorflow.keras import layers +from keras_contrib.layers.advanced_activations.sinerelu import SineReLU +from tensorflow.keras.layers import Conv2D, Dense, Flatten, MaxPool2D, Input, Dropout +from keras.utils.generic_utils import get_custom_objects +from tensorflow.keras import Model +from keras.layers import Activation + +#calling custom activation functions and adding them to arguments list +get_custom_objects().update({'SineReLU': Activation(SineReLU)}) + +class ExampleNet(tf.keras.Model): + def __init__(self): + super(ExampleNet, self).__init__() + self.conv1 = Conv2D(98, (3,3), activation='SineReLU', strides=(4,4)) + self.norm1 = BatchNormalization() +``` + ### A Common "Gotcha" From c21cc1f67853e8876acd493ea78a9cd8f820a664 Mon Sep 17 00:00:00 2001 From: TAWSIF AHMED Date: Fri, 21 Oct 2022 11:34:31 +0600 Subject: [PATCH 2/4] adding keras-contrib functions using string alias --- README.md | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 22975d7a3..3465fa77e 100644 --- a/README.md +++ b/README.md @@ -82,13 +82,14 @@ model.fit(x=np.random.random((100, 10)), y=np.random.random((100, 100)), epochs= # Save our model model.save('example.h5') ``` -Modules from Keras-Contrib library in subclass method + +### Adding keras-contrib library functions using string alias ```python from tensorflow import keras from tensorflow.keras import layers from keras_contrib.layers.advanced_activations.sinerelu import SineReLU -from tensorflow.keras.layers import Conv2D, Dense, Flatten, MaxPool2D, Input, Dropout +from tensorflow.keras.layers import Conv2D from keras.utils.generic_utils import get_custom_objects from tensorflow.keras import Model from keras.layers import Activation @@ -96,11 +97,8 @@ from keras.layers import Activation #calling custom activation functions and adding them to arguments list get_custom_objects().update({'SineReLU': Activation(SineReLU)}) -class ExampleNet(tf.keras.Model): - def __init__(self): - super(ExampleNet, self).__init__() - self.conv1 = Conv2D(98, (3,3), activation='SineReLU', strides=(4,4)) - self.norm1 = BatchNormalization() +model.add(Conv2D(256, (3,3), activation='SineReLU')) + ``` From 1aac6486d0c9c7f8c377ff6bae774f66776875f4 Mon Sep 17 00:00:00 2001 From: TAWSIF AHMED Date: Fri, 21 Oct 2022 11:35:36 +0600 Subject: [PATCH 3/4] adding keras-contrib functions using string alias --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 3465fa77e..fc3612920 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,6 @@ from tensorflow.keras import layers from keras_contrib.layers.advanced_activations.sinerelu import SineReLU from tensorflow.keras.layers import Conv2D from keras.utils.generic_utils import get_custom_objects -from tensorflow.keras import Model from keras.layers import Activation #calling custom activation functions and adding them to arguments list From 8dd5c0e33cbf92133e2b3cb404c9d771d21bef91 Mon Sep 17 00:00:00 2001 From: TAWSIF AHMED Date: Sat, 22 Oct 2022 01:02:43 +0600 Subject: [PATCH 4/4] PELU function import fixed In the recent version of Keras-contrib library you have to use name of the activation function in the calling itself to import it., That means now you have to write "keras_contrib.layers.advanced_activations.[function name, ex: pelu] import PELU --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fc3612920..9e7084940 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ import numpy as np # I wish Keras had the Parametric Exponential Linear activation.. # Oh, wait..! -from keras_contrib.layers.advanced_activations import PELU +from keras_contrib.layers.advanced_activations.pelu import PELU # Create the Keras model, including the PELU advanced activation model = Sequential()