User:Ping/Python Perceptron

From Noisebridge
< User:Ping
Revision as of 19:24, 18 March 2009 by Ping (Talk | contribs)

Jump to: navigation, search

This Perceptron does no damping. It succeeds for the included training set, but will run forever if you add the training case ([0, 1, 0], 1).

#!/usr/bin/env python

__author__ = 'Ka-Ping Yee <ping@zesty.ca>'

def dot_product(inputs, weights):
    return sum(i*w for i, w in zip(inputs, weights))

class Perceptron:
    def __init__(self, weights, threshold):
        self.weights = weights
        self.threshold = threshold

    def __repr__(self):
        weights = '[%s]' % (', '.join('%.3g' % w for w in self.weights))
        return 'Perceptron(%s, %r)' % (weights, self.threshold)

    def evaluate(self, inputs):
        return int(dot_product(self.weights, inputs) > self.threshold)

    def adjust(self, inputs, delta):
        for i in range(len(inputs)):
            self.weights[i] += delta*inputs[i]

def train(perceptron, inputs, expected_output, delta):
    output = perceptron.evaluate(inputs)
    perceptron.adjust(inputs, delta * (expected_output - output))

def train_set(perceptron, training_set, delta):
    for inputs, expected_output in training_set:
        train(perceptron, inputs, expected_output, delta)

def check_set(perceptron, training_set):
    print perceptron
    failures = 0
    for inputs, expected_output in training_set:
        output = perceptron.evaluate(inputs)
        print '    %r -> %r (should be %r)' % (inputs, output, expected_output)
        if output != expected_output:
            failures += 1
    return not failures

training_set = [
    ([1, 0, 0], 1),
    ([1, 0, 1], 1),
    ([1, 1, 0], 1),
    ([1, 1, 1], 0),
]

perceptron = Perceptron([0.0, 0.0, 0.0], 0.5)
while not check_set(perceptron, training_set):
    train_set(perceptron, training_set, 0.1)

print
print 'Success:', perceptron
Personal tools