# Class notes for Wednesday, March 22, 2006

# A Neuron object represents a simple model of a neuron

class Neuron:

    def __init__(self, weights):
        self.activation = 0.0
        self.weights = weights
        self.threshold = 0.5

    def present(self, pattern):
        # make sure pattern size matches number of weights
        if len(pattern) != len(self.weights):
            print "Wrong size pattern!"
            return None

        # calculate total incoming activation
        total = 0.0
        for i in range(len(pattern)):
            total = total + pattern[i] * self.weights[i]

        # compute new activation level
        if total > self.threshold:
            self.activation = 1.0
        else:
            self.activation = 0.0

        return self.activation


#--------------------------------------------------------------
# Example

def main():
    # these neurons implement the logical AND and OR functions
    andNeuron = Neuron([0.3, 0.4])
    orNeuron = Neuron([0.6, 0.7])
    # test their behavior on all possible binary input patterns
    print "Testing OR neuron..."
    print orNeuron.present([0, 0])
    print orNeuron.present([0, 1])
    print orNeuron.present([1, 0])
    print orNeuron.present([1, 1])
    print "Testing AND neuron..."
    print andNeuron.present([0, 0])
    print andNeuron.present([0, 1])
    print andNeuron.present([1, 0])
    print andNeuron.present([1, 1])

# >>> main()
# Testing OR neuron...
# 0.0
# 1.0
# 1.0
# 1.0
# Testing AND neuron...
# 0.0
# 0.0
# 0.0
# 1.0

