# Created by Leo from: C:\Python22\Tom\concious.leo # << lonegg declarations >> import sched import random import time import pickle import math timmerthing = sched.scheduler(time.time, time.sleep) g = random.Random() h = random.Random() NuralGuess = range(86399) for i in range(86399): NuralGuess[i] = 0.0 SeccondsinDay = range(86399) for i in range(86399): SeccondsinDay[i] = 0.0 nuralcounter = 0 Testpattern = [] timenow = 0 savecounter = 0 # -- end -- << lonegg declarations >> # << lonegg methods >> (1 of 7) def rand(a, b): return (b-a)*random.random() + a # << lonegg methods >> (2 of 7) def makeMatrix(I, J, fill=0.0): m = [] for i in range(I): m.append([fill]*J) return m # << lonegg methods >> (3 of 7) class NN: # << class NN methods >> (1 of 6) def __init__(self, ni, nh, no): self.ni = ni + 1 self.nh = nh self.no = no self.NNerror = '' self.testpattern = [] self.ai = [1.0]*self.ni self.ah = [1.0]*self.nh self.ao = [1.0]*self.no self.wi = makeMatrix(self.ni, self.nh) self.wo = makeMatrix(self.nh, self.no) for i in range(self.ni): for j in range(self.nh): self.wi[i][j] = rand(-2.0, 2.0) for j in range(self.nh): for k in range(self.no): self.wo[j][k] = rand(-2.0, 2.0) self.ci = makeMatrix(self.ni, self.nh) self.co = makeMatrix(self.nh, self.no) # << class NN methods >> (2 of 6) def update(self, inputs): if len(inputs) != self.ni-1: raise ValueError, 'wrong number of inputs' for i in range(self.ni-1): self.ai[i] = inputs[i] for j in range(self.nh): sum = 0.0 for i in range(self.ni): sum = sum + self.ai[i] * self.wi[i][j] self.ah[j] = 1.0/(1.0+math.exp(-sum)) for k in range(self.no): sum = 0.0 for j in range(self.nh): sum = sum + self.ah[j] * self.wo[j][k] self.ao[k] = 1.0/(1.0+math.exp(-sum)) return self.ao[:] # << class NN methods >> (3 of 6) def backPropagate(self, targets, N, M): if len(targets) != self.no: raise ValueError, 'wrong number of target values' output_deltas = [0.0] * self.no for k in range(self.no): ao = self.ao[k] output_deltas[k] = ao*(1-ao)*(targets[k]-ao) hidden_deltas = [0.0] * self.nh for j in range(self.nh): sum = 0.0 for k in range(self.no): sum = sum + output_deltas[k]*self.wo[j][k] hidden_deltas[j] = self.ah[j]*(1-self.ah[j])*sum for j in range(self.nh): for k in range(self.no): change = output_deltas[k]*self.ah[j] self.wo[j][k] = self.wo[j][k] + N*change + M*self.co[j][k] self.co[j][k] = change for i in range(self.ni): for j in range(self.nh): change = hidden_deltas[j]*self.ai[i] self.wi[i][j] = self.wi[i][j] + N*change + M*self.ci[i][j] self.ci[i][j] = change error = 0.0 for k in range(len(targets)): error = error + 0.5*(targets[k]-self.ao[k])**2 return error # << class NN methods >> (4 of 6) def test(self, patterns): self.testpattern = [] for p in patterns: self.testpattern.append(self.update(p[0])) return self.testpattern # << class NN methods >> (5 of 6) def weights(self): print 'Input weights:' for i in range(self.ni): print self.wi[i] print print 'Output weights:' for j in range(self.nh): print self.wo[j] # << class NN methods >> (6 of 6) def train(self, patterns, iterations=2000, N=0.5, M=0.1): for i in xrange(iterations): error = 0.0 for p in patterns: inputs = p[0] targets = p[1] self.update(inputs) error = error + self.backPropagate(targets, N, M) if i % 100 == 0: self.NNerror = 'error %-14f' % error # -- end -- << class NN methods >> # << lonegg methods >> (4 of 7) def plant(): g.seed() h.seed(g) return abs(h.random()- g.random()) # << lonegg methods >> (5 of 7) def OnSaveData(): global nuralcounter, savecounter,NuralGuess, SeccondsinDay, Testpattern, timenow PlotWindow_data = [] tom = open('c:\\python22\\tom\\concious_data2.txt','r+') try: PlotWindow_data = [ self.PlotWindow_mon.SeccondsinDay, self.PlotWindow_tues.SeccondsinDay, self.PlotWindow_wed.SeccondsinDay, self.PlotWindow_thurs.SeccondsinDay, self.PlotWindow_fri.SeccondsinDay, self.PlotWindow_sat.SeccondsinDay, self.PlotWindow_sun.SeccondsinDay, self.PlotWindow_mon.NuralGuess, self.PlotWindow_tues.NuralGuess, self.PlotWindow_wed.NuralGuess, self.PlotWindow_thurs.NuralGuess, self.PlotWindow_fri.NuralGuess, self.PlotWindow_sat.NuralGuess, self.PlotWindow_sun.NuralGuess] pickle.dump(PlotWindow_data, tom) except: pass self.tom.close() # << lonegg methods >> (6 of 7) def OnTimer(): global nuralcounter, savecounter,NuralGuess, SeccondsinDay, Testpattern, timenow nuralcounter = nuralcounter + 1 savecounter = savecounter + 1 timenow = time.localtime()[3]*3600 + time.localtime()[4] * 60 + time.localtime()[5] SeccondsinDay[timenow] = plant() Testpattern = NuralNet.test([ [ [ SeccondsinDay[timenow - 1] ], [0] ],[ [ SeccondsinDay[timenow] ], [0] ] ]) if (timenow + 60) < 86399: NuralGuess[timenow + 60] = Testpattern[1][0] if nuralcounter == 104: nuralcounter = 0 OnNuralTimer() if savecounter == 3600: savecounter = 0 OnSaveData() # << lonegg methods >> (7 of 7) def OnNuralTimer(): global nuralcounter, savecounter,NuralGuess, SeccondsinDay, Testpattern, timenow self.Trainpattern = [] if time.localtime()[3] * 3600 + time.localtime()[4] * 60 + time.localtime()[5] - 60 >= 3600: for i in range(time.localtime()[3] * 3600 + time.localtime()[4] * 60 + time.localtime()[5] - 60, time.localtime()[3] * 3600 + time.localtime()[4] * 60 + time.localtime()[5]): self.Trainpattern.append([ [ self.SeccondsinDay[i - 3600] ], [ self.SeccondsinDay[i] ] ]) else: for i in range(3600,3660): self.Trainpattern.append([ [ self.SeccondsinDay[i - 3600] ], [ self.SeccondsinDay[i]] ]) self.NuralNet.train(self.Trainpattern) # -- end -- << lonegg methods >> timmerthing.enter(1, 1, OnTimer, ()) NuralNet = NN(1, 7, 1) while 1: timmerthing.run()