Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3a83335

Browse files
nouman-10norvig
authored andcommitted
Added relu Activation (aimacode#960)
* added relu activation * added default parameters
1 parent a28bf5a commit 3a83335

File tree

3 files changed

+367
-25
lines changed

3 files changed

+367
-25
lines changed

learning.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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
7+
open_data, sigmoid_derivative, probability, norm, matrix_multiplication, relu, relu_derivative
88
)
99

1010
import copy
@@ -652,7 +652,7 @@ def predict(example):
652652

653653

654654
def NeuralNetLearner(dataset, hidden_layer_sizes=None,
655-
learning_rate=0.01, epochs=100):
655+
learning_rate=0.01, epochs=100, activation = sigmoid):
656656
"""Layered feed-forward network.
657657
hidden_layer_sizes: List of number of hidden units per hidden layer
658658
learning_rate: Learning rate of gradient descent
@@ -664,9 +664,9 @@ def NeuralNetLearner(dataset, hidden_layer_sizes=None,
664664
o_units = len(dataset.values[dataset.target])
665665

666666
# construct a network
667-
raw_net = network(i_units, hidden_layer_sizes, o_units)
667+
raw_net = network(i_units, hidden_layer_sizes, o_units, activation)
668668
learned_net = BackPropagationLearner(dataset, raw_net,
669-
learning_rate, epochs)
669+
learning_rate, epochs, activation)
670670

671671
def predict(example):
672672
# Input nodes
@@ -695,7 +695,7 @@ def random_weights(min_value, max_value, num_weights):
695695
return [random.uniform(min_value, max_value) for _ in range(num_weights)]
696696

697697

698-
def BackPropagationLearner(dataset, net, learning_rate, epochs):
698+
def BackPropagationLearner(dataset, net, learning_rate, epochs, activation=sigmoid):
699699
"""[Figure 18.23] The back-propagation algorithm for multilayer networks"""
700700
# Initialise weights
701701
for layer in net:
@@ -743,8 +743,11 @@ def BackPropagationLearner(dataset, net, learning_rate, epochs):
743743
# Error for the MSE cost function
744744
err = [t_val[i] - o_nodes[i].value for i in range(o_units)]
745745

746-
# The activation function used is the sigmoid function
747-
delta[-1] = [sigmoid_derivative(o_nodes[i].value) * err[i] for i in range(o_units)]
746+
# The activation function used is relu or sigmoid function
747+
if node.activation == sigmoid:
748+
delta[-1] = [sigmoid_derivative(o_nodes[i].value) * err[i] for i in range(o_units)]
749+
else:
750+
delta[-1] = [relu_derivative(o_nodes[i].value) * err[i] for i in range(o_units)]
748751

749752
# Backward pass
750753
h_layers = n_layers - 2
@@ -756,7 +759,11 @@ def BackPropagationLearner(dataset, net, learning_rate, epochs):
756759
# weights from each ith layer node to each i + 1th layer node
757760
w = [[node.weights[k] for node in nx_layer] for k in range(h_units)]
758761

759-
delta[i] = [sigmoid_derivative(layer[j].value) * dotproduct(w[j], delta[i+1])
762+
if activation == sigmoid:
763+
delta[i] = [sigmoid_derivative(layer[j].value) * dotproduct(w[j], delta[i+1])
764+
for j in range(h_units)]
765+
else:
766+
delta[i] = [relu_derivative(layer[j].value) * dotproduct(w[j], delta[i+1])
760767
for j in range(h_units)]
761768

762769
# Update weights
@@ -800,14 +807,14 @@ class NNUnit:
800807
weights: Weights to incoming connections
801808
"""
802809

803-
def __init__(self, weights=None, inputs=None):
810+
def __init__(self, activation=sigmoid, weights=None, inputs=None):
804811
self.weights = weights or []
805812
self.inputs = inputs or []
806813
self.value = None
807-
self.activation = sigmoid
814+
self.activation = activation
808815

809816

810-
def network(input_units, hidden_layer_sizes, output_units):
817+
def network(input_units, hidden_layer_sizes, output_units, activation=sigmoid):
811818
"""Create Directed Acyclic Network of given number layers.
812819
hidden_layers_sizes : List number of neuron units in each hidden layer
813820
excluding input and output layers
@@ -818,7 +825,7 @@ def network(input_units, hidden_layer_sizes, output_units):
818825
else:
819826
layers_sizes = [input_units] + [output_units]
820827

821-
net = [[NNUnit() for n in range(size)]
828+
net = [[NNUnit(activation) for n in range(size)]
822829
for size in layers_sizes]
823830
n_layers = len(net)
824831

0 commit comments

Comments
 (0)