I am having some troubles implementing this backprop network. I'm not really understanding how to start this off because in this network my first layer only has 8 nodes. But my prompt gives me 10 in the training set.
In the first group for example, I have
[0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0]
but only 8 nodes so I can assign a starting activation for each node or I would have 2 left over.
#######################################################################################
#
# Preparations
#
#######################################################################################
import random
import math
import pygame
import sys
network1 = []
network2 = []
layer1 = []
layer2 = []
layer3 = []
set1 = []
set2 = []
set3 = []
mu = .5
eta = .1
#######################################################################################
#
# Node Class
#
#######################################################################################
class Node(object):
def __init__(self,name=None):
self.name = name
self.error = None
self.activation_threshold = 0.0
self.net_input = 0.0
self.net_output = 0.0
self.outgoing_connections = []
self.incoming_connections = []
self.activation = None
def __str__(self):
return self.name
def addin(self,sender,weight=random.random):
self.incoming_connections.append(Connection(sender,self,weight=random.random))
def addout(self,sender,weight=random.random):
self.outgoing_connections.append(Connection(sender,self,weight=random.random))
def update_input(self):
self.net_input=0.0
for conn in self.incoming_connections:
self.net_input += conn.weight * conn.sender.activation
def update_output(self):
self.net_output=0.0
for conn in self.outgoing_connections:
self.net_output += conn.weight * self.activation
def update_activation(self):
self.activation = 1 / (1 + math.exp(-self.net_input))
def update_weight(self):
for i in self.incoming_connections:
i.weight = (2*i.reciever.activation - 1)*(2*i.sender.activation-1)
for i in self.outgoing_connections:
i.weight = (2*i.reciever.activation - 1)*(2*i.sender.activation-1)
def update_error(self):
pass
#######################################################################################
#
# Connection Class
#
#######################################################################################
class Connection(object):
def __init__(self, sender, reciever, weight=random.random):
self.weight=weight
self.sender=sender
self.reciever=reciever
def __str__(self):
string = "Connection from " + str(self.sender) + " to " + str(self.reciever) + ", weight = " + str(self.weight)
return string
#######################################################################################
#
# Creating Nodes & Connections
#
#######################################################################################
for i in xrange(8):
layer1.append(Node(str(i)))
network1.append(layer1)
for i in xrange(3):
layer2.append(Node(str(i)))
network1.append(layer2)
for i in xrange(8):
layer3.append(Node(str(i)))
network1.append(layer3)
for i in xrange(8):
for j in xrange(3):
layer1[i].addin(layer2[j])
for i in xrange(3):
for j in xrange(8):
layer2[i].addin(layer3[j])
#######################################################################################
#
# Training Patterns
#
#######################################################################################
"""Non-Overlapping Categories"""
cata11=[0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0]
set1.append(cata11)
cata12=[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0]
set1.append(cata12)
catb11=[0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0,0.0]
set1.append(catb11)
catb12=[0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0]
set1.append(catb12)
catc11=[0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0]
set1.append(catc11)
catc12=[0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]
set1.append(catc12)
catd11=[0.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0]
set1.append(catd11)
catd12=[1.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0]
set1.append(catd12)
"""Linearly Independent Instances and Linearly Separable Categories"""
cata21=[1.0,1.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,1.0]
set2.append(cata21)
cata22=[1.0,1.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0]
set2.append(cata22)
catb21=[1.0,1.0,0.0,0.0,0.0,1.0,0.0,0.0,1.0,0.0]
set2.append(catb21)
catb22=[1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0]
set2.append(catb22)
catc21=[1.0,0.0,1.0,1.0,0.0,0.0,0.0,1.0,0.0,0.0]
set2.append(catc21)
catc22=[1.0,0.0,1.0,0.0,1.0,0.0,0.0,1.0,0.0,0.0]
set2.append(catc22)
catd21=[1.0,0.0,1.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0]
set2.append(catd21)
catd22=[1.0,0.0,1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0]
set2.append(catd22)
"""Not Linearly Separable Categories"""
cata31=[1.0,1.0,1.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0]
set3.append(cata31)
cata32=[0.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,1.0]
set3.append(cata32)
catb31=[1.0,1.0,0.0,1.0,1.0,0.0,0.0,1.0,0.0,0.0]
set3.append(catb31)
catb32=[0.0,0.0,1.0,0.0,0.0,1.0,1.0,0.0,1.0,1.0]
set3.append(catb32)
##set_activations(cata11)
for i in network1:
i.update_weight()
##for thing in node1:
## thing.update_weight()
##set_activations(cata12)
##for thing in node1:
## thing.update_weight()
##set_activations(catb11)
##for thing in node1:
## thing.update_weight()
##set_activations(catb12)
##for thing in node1:
## thing.update_weight()
##set_activations(catc11)
##for thing in node1:
## thing.update_weight()
##set_activations(catc12)
##for thing in node1:
## thing.update_weight()
##set_activations(catd11)
##for thing in node1:
## thing.update_weight()
##set_activations(catd12)
##for thing in node1:
## thing.update_weight()
#######################################################################################
#
# Updating Network
#
#######################################################################################
for i in layer1:
i.update_input()
print 'Node', str(i), 'Input : ', i.net_input
for i in layer1:
i.update_activation()
print 'Act:', i.activation
for i in layer1:
i.update_output()
print 'Output', i.net_output
for i in layer2:
i.update_input()
print 'Node', str(i), 'Input : ', i.net_input
for i in layer2:
i.update_activation()
print 'Act:', i.activation