-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeuron.py
executable file
·81 lines (56 loc) · 1.6 KB
/
Neuron.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
""" Class Neuron represents a neuron model in the Brain Antenna theory
"""
from numpy import *
import random
class Neuron:
def __init__(self,id):
self.id = id
self.level = 0.
self.outNeurons = []
self.inNeurons = []
self.locked = False
self.memory = []
def lock(self):
self.locked = True
def unlock(self):
self.locked = False
def addInNeuron(self,neuron):
if type(neuron) == type(self):
self.inNeurons.append(neuron)
else:
print "invalid object type"
def addOutNeuron(self,neuron):
if type(neuron) == type(self):
self.outNeurons.append(neuron)
else:
print "invalid object type"
def setLevel(self,level):
self.level = level
def getInputLevel(self):
if self.locked:
return self.level,self.level
inLevel = 0.
for n in self.inNeurons:
inLevel += n.level
oldlevel = self.level
self.level = inLevel/len(self.inNeurons)
return oldlevel,self.level
def addRandomMemory(self):
marray = array([ random.random() for x in range(len(self.inNeurons)) ])
self.memory.append(marray)
def addInputToMemory(self):
marray = array([ x.level for x in self.inNeurons])
stddev = std(marray)
if stddev > 0.1:
#print ">>>",self.id,stddev,marray
self.memory.append(marray)
def getBestMatchToMemory(self):
if self.locked:
return self.level,self.level
input = array([ x.level for x in self.inNeurons])
mcorr = 0.
for marray in self.memory:
corr = corrcoef(marray,input)[1,0]
if abs(mcorr) < abs(corr):
mcorr = corr
return self.level,mcorr