Skip to content

Commit f309bd7

Browse files
authored
Added Activation Functions project files
1 parent 29ec911 commit f309bd7

File tree

6 files changed

+119
-0
lines changed

6 files changed

+119
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Agrover112
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# ActivationFunctions using Custom Layers in Keras
2+
Activation functions are an important are of deep learning research .Many new activation functions are being developed ,these include *bio-inspired* activtions, *purely mathematical activation functions* including others . Despite, such advancements we usually find ourselves using RELU and LeakyRELU commonly without using/thinking about others.
3+
In the following notebooks I showcase how easy/difficult it is to port an activation function using **Custom Layers in Keras and Tensorflow!**
4+
5+
6+
Link to main notebook --> [Activations.ipynb](https://github.com/Agrover112/ActivationFunctions/blob/master/src/Activation-Functions(GELU%2CSELU%2CELU%2CLeakyReLU%2CPRELU).ipynb)
7+
8+
### Implemented activations:
9+
10+
- LeakyReLu
11+
- ParametricReLu
12+
- Elu
13+
- SElu
14+
- GELU
15+
16+
17+
18+
### Structure
19+
```
20+
src
21+
|
22+
|-- Activations.ipynb
23+
|-- utils
24+
|-- Utils.ipynb
25+
|-- utils.py
26+
27+
references
28+
|
29+
|--Ref1
30+
|--Refn
31+
32+
```
33+
34+
### Usage
35+
```
36+
git clone https://github.com/Agrover112/ActivationFunctions.git
37+
```
38+
39+
### References
40+
- [References:D](https://github.com/Agrover112/ActivationFunctions/tree/master/references)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## References
2+
- SELU : [Self Normailizing Neural Networks](https://arxiv.org/abs/1706.02515)
3+
- GELU : [Gaussian Error Linear Units (GELUs)- Elu ](https://arxiv.org/abs/1606.08415)
4+
- PRELU : [Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification](https://arxiv.org/abs/1502.01852)
5+
- Leaky ReLU : [Rectifier Nonlinearities Improve Neural Network Acoustic Models](http://robotics.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf)
6+
7+
- [Activation Functions Blog](https://mlfromscratch.com/activation-functions-explained/#elu)

Scripts/Miscellaneous/Activation_Functions_from_scratch_in_Keras/src/Activation-Functions(GELU,SELU,ELU,LeakyReLU,PRELU).ipynb

+1
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"Utils","provenance":[],"collapsed_sections":[],"authorship_tag":"ABX9TyMYHshcegvtTBQwBygO/eoj"},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"code","metadata":{"id":"_ImvYera1GfS","colab_type":"code","colab":{},"executionInfo":{"status":"ok","timestamp":1595963420315,"user_tz":-330,"elapsed":2543,"user":{"displayName":"Agrover112","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiMJACGAX3kCfRjB2hgzdG8w9zL1lAAKbPPMz0qLA=s64","userId":"09574164879083471944"}}},"source":["import tensorflow as tf\n","import numpy as np\n","import matplotlib.pyplot as plt\n","def load_data():\n"," (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()\n"," x_train = np.reshape(x_train, (x_train.shape[0], 784))/255.\n"," x_test = np.reshape(x_test, (x_test.shape[0], 784))/255.\n"," y_train = tf.keras.utils.to_categorical(y_train)\n"," y_test = tf.keras.utils.to_categorical(y_test)\n"," return (x_train, y_train), (x_test, y_test)\n","\n","def plot_random_examples(x, y, p=None):\n"," indices = np.random.choice(range(0, x.shape[0]), 10)\n"," y = np.argmax(y, axis=1)\n"," if p is None:\n"," p = y\n"," plt.figure(figsize=(10, 5))\n"," for i, index in enumerate(indices):\n"," plt.subplot(2, 5, i+1)\n"," plt.imshow(x[index].reshape((28, 28)), cmap='binary')\n"," plt.xticks([])\n"," plt.yticks([])\n"," if y[index] == p[index]:\n"," col = 'g'\n"," else:\n"," col = 'r'\n"," plt.xlabel(str(p[index]), color=col)\n"," return plt\n","\n","def plot_results(history):\n"," history = history.history\n"," plt.figure(figsize=(12, 4))\n"," epochs = len(history['val_loss'])\n"," plt.subplot(1, 2, 1)\n"," plt.plot(range(epochs), history['val_loss'], label='Val Loss')\n"," plt.plot(range(epochs), history['loss'], label='Train Loss')\n"," plt.xticks(list(range(epochs)))\n"," plt.xlabel('Epochs')\n"," plt.ylabel('Loss')\n"," plt.legend()\n"," plt.subplot(1, 2, 2)\n"," plt.plot(range(epochs), history['val_accuracy'], label='Val Acc')\n"," plt.plot(range(epochs), history['accuracy'], label='Acc')\n"," plt.xticks(list(range(epochs)))\n"," plt.xlabel('Epochs')\n"," plt.ylabel('Accuracy')\n"," plt.legend()\n"," return plt"],"execution_count":1,"outputs":[]},{"cell_type":"code","metadata":{"id":"iRwkOk0p1SPt","colab_type":"code","colab":{}},"source":[""],"execution_count":null,"outputs":[]}]}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import tensorflow as tf
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
5+
def load_data():
6+
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
7+
x_train = np.reshape(x_train, (x_train.shape[0], 784))/255.
8+
x_test = np.reshape(x_test, (x_test.shape[0], 784))/255.
9+
y_train = tf.keras.utils.to_categorical(y_train)
10+
y_test = tf.keras.utils.to_categorical(y_test)
11+
return (x_train, y_train), (x_test, y_test)
12+
13+
def plot_random_examples(x, y, p=None):
14+
indices = np.random.choice(range(0, x.shape[0]), 10)
15+
y = np.argmax(y, axis=1)
16+
if p is None:
17+
p = y
18+
plt.figure(figsize=(10, 5))
19+
for i, index in enumerate(indices):
20+
plt.subplot(2, 5, i+1)
21+
plt.imshow(x[index].reshape((28, 28)), cmap='binary')
22+
plt.xticks([])
23+
plt.yticks([])
24+
if y[index] == p[index]:
25+
col = 'g'
26+
else:
27+
col = 'r'
28+
plt.xlabel(str(p[index]), color=col)
29+
return plt
30+
31+
def plot_results(history):
32+
history = history.history
33+
plt.figure(figsize=(12, 4))
34+
epochs = len(history['val_loss'])
35+
plt.subplot(1, 2, 1)
36+
plt.plot(range(epochs), history['val_loss'], label='Val Loss')
37+
plt.plot(range(epochs), history['loss'], label='Train Loss')
38+
plt.xticks(list(range(epochs)))
39+
plt.xlabel('Epochs')
40+
plt.ylabel('Loss')
41+
plt.legend()
42+
plt.subplot(1, 2, 2)
43+
plt.plot(range(epochs), history['val_accuracy'], label='Val Acc')
44+
plt.plot(range(epochs), history['accuracy'], label='Acc')
45+
plt.xticks(list(range(epochs)))
46+
plt.xlabel('Epochs')
47+
plt.ylabel('Accuracy')
48+
plt.legend()
49+
return plt

0 commit comments

Comments
 (0)