Created
September 1, 2014 07:20
-
-
Save tinybike/bed71f1109538df59d8b to your computer and use it in GitHub Desktop.
perceptron neural network
This file contains 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
Perceptron neural network. Can learn any linearly separable logic. | |
(c) Jack Peterson, 6/12/2008 | |
""" | |
from random import random | |
def hardlimit(row, col, bias): | |
""" | |
Hard limit evaluation function. | |
If dot product of the state and input vectors is greater than or equal | |
to the bias, then neuron fires. | |
""" | |
product = 0 | |
for r, c in zip(row, col): | |
product += r * c | |
if product >= bias: | |
fire = 1 | |
else: | |
fire = 0 | |
return fire | |
def transition(state, invec, outvec, goal, bias): | |
""" | |
Transition function. | |
Compare the binary values of the current output element to the goal | |
element, and adjust the state vector if different. | |
""" | |
# For every output element that is not equal to its corresponding goal | |
# element, adjust the state vector and the bias | |
num_states = len(state) | |
if outvec != goal: | |
for i in xrange(num_states): | |
state[i] += (goal - outvec) * invec[i] | |
bias -= goal - outvec | |
return state, bias | |
def main(): | |
invec = [[0, 0], [0, 1], [1, 0], [1, 1]] | |
# Randomize on (0, 1] both state vector elements | |
state = [random(), random()] | |
# Randomize on (0, 1] bias value | |
bias = random() | |
# Initial output list | |
outvec = [0, 0, 0, 0] | |
# Desired output list: simple OR | |
# (perceptron can learn any linearly separable set) | |
goal = [0, 1, 1, 1] | |
print 'Simple OR:', goal | |
print '----------------------------' | |
print 'Output State Bias' | |
print '----------------------------' | |
elements = len(invec) | |
for j in xrange(100): | |
# Iterate the 4 input vectors through the evaluation function | |
for i in xrange(elements): | |
outvec[i] = hardlimit(state, invec[i], bias) | |
# If the output vector has reached the goal vector, then quit | |
if outvec == goal: | |
print outvec, ' ', state, ' ', bias | |
print 'Training complete!' | |
return | |
# Iterate the output values through the transition function, and | |
# retrieve the adjusted values for the state vector and the bias | |
for i in xrange(elements): | |
state, bias = transition(state, invec[i], outvec[i], goal[i], bias) | |
print outvec, ' ', state, ' ', bias | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment