Python Backpropagation:没有输出值

时间:2014-03-18 20:38:25

标签: python connection neural-network backpropagation

所以我现在正试图进行反向传播,但出于某种原因,我得到了一个输出,一个激活,但没有输出值。现在我只是在使用单层网络,只是一些简单的事情,但它似乎没有按照它的想法去做。

我创建了2组节点,node1和node2,添加了连接1到2的连接。在下面的模式上训练它,更新权重,更新输入,更新激活,然后输出。但我得到了0。

# 
#                               Preparations 
# 

import random
import math
import pygame
import sys
node1 = []
node2 = []
node3 = []
node4 = []
node5 = []
set1 = []
set2 = []
set3 = []
NUMNODES = 10
mu = .5
eta = .1

# 
#                                   Node Class
# 

class Node(object): 

    def __init__(self,name=None): 
        self.name=name 
        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 addconnection(self,sender,weight=0.0):
        self.incoming_connections.append(Connection(sender,self,weight))
        self.outgoing_connections.append(Connection(self,sender,weight)) 

    def update_input(self): 
        self.net_input=0.0
        for conn in self.incoming_connections: 
            self.net_input += conn.weight * conn.sender.activation
##        print 'Input for node', str(self), 'is', self.net_input


############################# Is this necessary? ####################
    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):
        if self.net_input > self.activation_threshold:
            self.activation = 1.0
        else:
            self.activation = 0.0
##        print 'Activation :', self.activation
##    def update_weight(self):
##        for i in self.incoming_connections:
##            i.weight += self.delta_wt
##            self.delta_wt = mu
    def update_weight(self):
        for i in self.incoming_connections:
            i.weight += (2*i.reciever.activation - 1)*(2*i.sender.activation-1)
##            print 'Weight of', str(i.reciever), 'to', str(i.sender), 'is', i.weight

    def update_error(self):
        pass

# 
#                                   Connection Class
# 

class Connection(object): 
    def __init__(self, sender, reciever, weight): 
        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 
# 
#######################################################################################

def set_activations(act_vector): 
    for i in xrange(len(act_vector)): 
        node1[i].activation = act_vector[i]
        node2[i].activation = act_vector[i]

for i in xrange(NUMNODES): 
    node1.append(Node(str(i)))
    node2.append(Node(str(i)))
    node3.append(Node(str(i)))
    node4.append(Node(str(i)))
    node5.append(Node(str(i)))

for i in xrange(NUMNODES):
    for j in xrange(NUMNODES):
        if i!=j:
            node1[i].addconnection(node2[j])
            node3[i].addconnection(node4[j])
            node4[i].addconnection(node5[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 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 node1:
    i.update_input()
    print 'Node', str(i), 'Input : ', i.net_input
    i.update_activation()
    print 'Act:', i.activation
    i.update_output()
    print 'Output', i.net_output

out_file=open('output.txt','w')
out_file.close()

0 个答案:

没有答案