Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated bias values while training #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions neural-network.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
import random
import math

#
# Shorthand:
# "pd_" as a variable prefix means "partial derivative"
# "d_" as a variable prefix means "derivative"
# "_wrt_" is shorthand for "with respect to"
# "w_ho" and "w_ih" are the index of weights from hidden to output layer neurons and input to hidden layer neurons respectively
#
# Comment references:
#
# [1] Wikipedia article on Backpropagation
# http://en.wikipedia.org/wiki/Backpropagation#Finding_the_derivative_of_the_error
# [2] Neural Networks for Machine Learning course on Coursera by Geoffrey Hinton
# https://class.coursera.org/neuralnets-2012-001/lecture/39
# [3] The Back Propagation Algorithm
# https://www4.rgu.ac.uk/files/chapter3%20-%20bp.pdf

class NeuralNetwork:
LEARNING_RATE = 0.5
Expand Down Expand Up @@ -98,6 +83,9 @@ def train(self, training_inputs, training_outputs):
# Δw = α * ∂Eⱼ/∂wᵢ
self.output_layer.neurons[o].weights[w_ho] -= self.LEARNING_RATE * pd_error_wrt_weight

pd_errors_wrt_weight = pd_errors_wrt_output_neuron_total_net_input[o] * 1
self.output_layer.neurons[o].bias -= self.LEARNING_RATE * pd_error_wrt_weight

# 4. Update hidden neuron weights
for h in range(len(self.hidden_layer.neurons)):
for w_ih in range(len(self.hidden_layer.neurons[h].weights)):
Expand All @@ -108,6 +96,9 @@ def train(self, training_inputs, training_outputs):
# Δw = α * ∂Eⱼ/∂wᵢ
self.hidden_layer.neurons[h].weights[w_ih] -= self.LEARNING_RATE * pd_error_wrt_weight

pd_errors_wrt_weight = pd_errors_wrt_hidden_neuron_total_net_input[o] * 1
self.hidden_layer.neurons[o].bias -= self.LEARNING_RATE * pd_error_wrt_weight

def calculate_total_error(self, training_sets):
total_error = 0
for t in range(len(training_sets)):
Expand Down