-
Notifications
You must be signed in to change notification settings - Fork 0
/
Perceptron.py
107 lines (83 loc) · 3.13 KB
/
Perceptron.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""
#################################
Perceptron.py by Mauro E. Dinardo
#################################
"""
from math import sqrt
from Neuron import Neuron
"""
####################################################
Nneurons = number of neurons of the perceptron
Nvars = number of input variables for each neuron
aFunType = type of activation function
####################################################
"""
class Perceptron(object):
def __init__(self,Nneurons,Nvars,aFunType):
self.Nneurons = Nneurons
self.neurons = [ Neuron(Nvars,aFunType) for i in range(self.Nneurons) ]
def eval(self,invec):
return [ N.eval(invec) for N in self.neurons ]
def adapt(self,invec,dCdZ):
for a,N in zip(dCdZ,self.neurons):
N.adapt(invec,a)
def cFun(self,target):
return sum(N.cFun(a) for a,N in zip(target,self.neurons))
def dcFunDz(self,target):
return [ N.dcFunDz(a) for a,N in zip(target,self.neurons) ]
def speed(self):
return sqrt(sum(N.afun * N.afun for N in self.neurons))
def reset(self):
for N in self.neurons:
N.reset()
def sum2W(self):
return sum(N.sum2W() for N in self.neurons)
def scramble(self,who):
if who[0] == -1:
who = [ i for i in range(self.Nneurons) ]
for i in who:
self.neurons[i].scramble()
def removeW(self,who):
for N in self.neurons:
N.removeW(who) if type(who) is list else N.__init__(who,N.aFunType)
def addW(self,who):
for N in self.neurons:
N.addW(who) if type(who) is list else N.__init__(who,N.aFunType)
def fixAllBut(self,who):
genExp = (N for (i,N) in enumerate(self.neurons) if i not in who)
for N in genExp:
N.amIfixed = True
def release(self,who):
if who[0] == -1:
who = [ i for i in range(self.Nneurons) ]
genExp = (N for (i,N) in enumerate(self.neurons) if i in who)
for N in genExp:
N.amIfixed = False
def removeN(self,who):
if who[0] == -1:
who = [ i for i in range(self.Nneurons) ]
self.neurons = [ N for i,N in enumerate(self.neurons) if i not in who ]
self.Nneurons = len(self.neurons[:])
def addN(self,who):
self.Nneurons += len(who[:])
for i,pos in enumerate(who):
self.neurons.insert(pos+i,Neuron(self.neurons[0].Nvars,self.neurons[0].aFunType))
def copy(self,P,amIminiB):
for Nfrom,Nto in zip(self.neurons,P.neurons):
Nfrom.copy(Nto,amIminiB)
def printParams(self):
for i,N in enumerate(self.neurons):
print(' Neuron[', i, '] -->', end='')
N.printParams()
def save(self,f):
for i,N in enumerate(self.neurons):
f.write(' Neuron[ {0:d} ] --> '.format(i))
N.save(f)
def read(self,f):
line = f.readline()
lele = line.split()
while len(lele) == 0 or (len(lele) > 0 and ('#' in lele[0] or 'Perceptron[' not in line)):
line = f.readline()
lele = line.split()
for N in self.neurons:
N.read(f)