Skip to content

Commit 4378fb2

Browse files
nouman-10norvig
authored andcommitted
Added activation functions (#968)
1 parent a99dfff commit 4378fb2

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

learning.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
removeall, unique, product, mode, argmax, argmax_random_tie, isclose, gaussian,
55
dotproduct, vector_add, scalar_vector_product, weighted_sample_with_replacement,
66
weighted_sampler, num_or_str, normalize, clip, sigmoid, print_table,
7-
open_data, sigmoid_derivative, probability, norm, matrix_multiplication, relu, relu_derivative
7+
open_data, sigmoid_derivative, probability, norm, matrix_multiplication, relu, relu_derivative,
8+
tanh, tanh_derivative, leaky_relu, leaky_relu_derivative, elu, elu_derivative
89
)
910

1011
import copy
@@ -746,8 +747,15 @@ def BackPropagationLearner(dataset, net, learning_rate, epochs, activation=sigmo
746747
# The activation function used is relu or sigmoid function
747748
if node.activation == sigmoid:
748749
delta[-1] = [sigmoid_derivative(o_nodes[i].value) * err[i] for i in range(o_units)]
749-
else:
750+
elif node.activation == relu:
750751
delta[-1] = [relu_derivative(o_nodes[i].value) * err[i] for i in range(o_units)]
752+
elif node.activation == tanh:
753+
delta[-1] = [tanh_derivative(o_nodes[i].value) * err[i] for i in range(o_units)]
754+
elif node.activation == elu:
755+
delta[-1] = [elu_derivative(o_nodes[i].value) * err[i] for i in range(o_units)]
756+
else:
757+
delta[-1] = [leaky_relu_derivative(o_nodes[i].value) * err[i] for i in range(o_units)]
758+
751759

752760
# Backward pass
753761
h_layers = n_layers - 2
@@ -762,9 +770,18 @@ def BackPropagationLearner(dataset, net, learning_rate, epochs, activation=sigmo
762770
if activation == sigmoid:
763771
delta[i] = [sigmoid_derivative(layer[j].value) * dotproduct(w[j], delta[i+1])
764772
for j in range(h_units)]
765-
else:
773+
elif activation == relu:
766774
delta[i] = [relu_derivative(layer[j].value) * dotproduct(w[j], delta[i+1])
767775
for j in range(h_units)]
776+
elif activation == tanh:
777+
delta[i] = [tanh_derivative(layer[j].value) * dotproduct(w[j], delta[i+1])
778+
for j in range(h_units)]
779+
elif activation == elu:
780+
delta[i] = [elu_derivative(layer[j].value) * dotproduct(w[j], delta[i+1])
781+
for j in range(h_units)]
782+
else:
783+
delta[i] = [leaky_relu_derivative(layer[j].value) * dotproduct(w[j], delta[i+1])
784+
for j in range(h_units)]
768785

769786
# Update weights
770787
for i in range(1, n_layers):

utils.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import random
1010
import math
1111
import functools
12+
import numpy as np
1213
from itertools import chain, combinations
1314

1415

@@ -273,9 +274,47 @@ def sigmoid(x):
273274
"""Return activation value of x with sigmoid function"""
274275
return 1 / (1 + math.exp(-x))
275276

277+
278+
279+
def relu_derivative(value):
280+
if value > 0:
281+
return 1
282+
else:
283+
return 0
284+
285+
def elu(x, alpha=0.01):
286+
if x > 0:
287+
return x
288+
else:
289+
return alpha * (math.exp(x) - 1)
290+
291+
def elu_derivative(value, alpha = 0.01):
292+
if value > 0:
293+
return 1
294+
else:
295+
return alpha * math.exp(value)
296+
297+
def tanh(x):
298+
return np.tanh(x)
299+
300+
def tanh_derivative(value):
301+
return (1 - (value ** 2))
302+
303+
def leaky_relu(x, alpha = 0.01):
304+
if x > 0:
305+
return x
306+
else:
307+
return alpha * x
308+
309+
def leaky_relu_derivative(value, alpha=0.01):
310+
if value > 0:
311+
return 1
312+
else:
313+
return alpha
314+
276315
def relu(x):
277316
return max(0, x)
278-
317+
279318
def relu_derivative(value):
280319
if value > 0:
281320
return 1

0 commit comments

Comments
 (0)