-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRolfNet.py
144 lines (80 loc) · 2.45 KB
/
RolfNet.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from math import *
import pickle
def Distance(P,Q):
if len(P)==len(Q):
return sqrt(sum([(P[k]-Q[k])**2 for k in range(len(P)) if P[k]!=None and Q[k]!=None ]))
return "Error missmath lenght"
def MeanRadius( Neurons):
Mean=0
if len(Neurons)==0:
return 1
for idn in Neurons:
sigma=Neurons[idn][1]
Mean=Mean+sigma
return Mean
# Find the next Neuron with the shortest distance to the in put x
# input:
# x sampel
# Neurons set of Neurons
# rho constant
# valid +
def FindNextNeuron( x , Neurons, rho ):
BestNeuron="none"
ShortDis=100000
if len(Neurons)==0:
return [ BestNeuron, ShortDis ]
for idn in Neurons:
ck=Neurons[idn][0]
sigma=Neurons[idn][1]
dis=Distance(ck,x)
if dis<=rho*sigma and dis<=ShortDis :
BestNeuron=idn
ShortDis=dis
return [ BestNeuron, ShortDis ]
# Chnage the position of the neurons or place a new one
# input:
# x sampel
# Neurons set of Neurons
# rho constant
# etac, etas learning rate
# Valid +
def ChangeNeurons(Neurons, x ,rho, etac, etas):
[idn, dis]=FindNextNeuron( x , Neurons, rho )
N=len(Neurons)
if idn=="none" and dis==100000:
sigma0=MeanRadius( Neurons)
Neurons[f"{N}"]=[x, sigma0]
else:
ck=Neurons[idn][0]
sigma=Neurons[idn][1]
Neurons[idn][0]=[ ck[k]*(1-etac)+x[k]*etac for k in range(len(x)) ]
Neurons[idn][1]=sigma*(1-etas)+etas*dis
##########
def TrainModell(Neurons, Sampels, rho, etac, etas, Nrun ):
for k in range(Nrun):
for x in Sampels:
ChangeNeurons(Neurons, x ,rho, etac, etas)
###########Manage Model#############
def StoreModel(Neurons, rho,etac, etas,Nrun,info, file):
Parameter={
"rho" :rho,
"etac": etac,
"etas": etas,
"Nrun": Nrun
}
Model={
"Neurons": Neurons,
"Parameter": Parameter,
"info": info
}
with open(file, 'wb') as file:
pickle.dump(Model, file)
def LoadModel(filename):
with open(filename,'rb') as file: Model=pickle.load(file)
return Model
def MakePrediction(Model,x):
[idn, dis]=FindNextNeuron(x ,
Model["Neurons"],
Model["Parameter"]["rho"]
)
return Model["Neurons"][idn][0]