Created
October 12, 2013 11:50
-
-
Save PirosB3/6949184 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import random | |
from functools import partial | |
N_NEURONS = 2 | |
N_ITERATIONS = 1000 | |
DOMAIN_FROM = -5 | |
DOMAIN_TO = 5 | |
class OutputNeuron(object): | |
def __init__(self, inputs): | |
self.inputs = inputs | |
def _activate(self, n): | |
'''Compute the sigmoid function''' | |
den = 1.0 + math.e ** (-1.0 * n) | |
d = 1.0 / den | |
return d | |
def process_output(self): | |
total_out = sum([i.get_output() for i in self.inputs]) | |
return self._activate(total_out) | |
def calculate_error(self, output, expected): | |
return (expected - output) * (1 - output) * output | |
class InputNeuron(object): | |
def __init__(self, weight): | |
self.weight = weight | |
def set_input(self, input): | |
self.input = input | |
def get_output(self): | |
return self.input * self.weight | |
def adjust_weight(self, error): | |
self.weight = self.weight + (error * self.input) | |
def get_training_data(n): | |
_r = partial(random.uniform, DOMAIN_FROM, DOMAIN_TO) | |
training = [_r() for _ in range(N_NEURONS)] | |
result = sum(training) | |
#result = sum([math.sin(x) for x in training]) | |
return training, result | |
def main(): | |
training, result = get_training_data(N_NEURONS) | |
_r = partial(random.uniform, 0, 1) | |
input_neurons = [InputNeuron(_r()) for _ in xrange(N_NEURONS)] | |
output_neuron = OutputNeuron(input_neurons) | |
# Set inputs from training | |
for i, input in enumerate(training): | |
print input | |
input_neurons[i].set_input(input) | |
while True: | |
# Calculate output | |
output = output_neuron.process_output() | |
error = output_neuron.calculate_error(output, result) | |
print "Output is: %s. Expected: %s. Error: %s" % (output, result, error) | |
# Correct Errors | |
for input in input_neurons: | |
input.adjust_weight(error) | |
if __name__== '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment