-
Notifications
You must be signed in to change notification settings - Fork 0
/
som_train_struct
executable file
·152 lines (129 loc) · 3.21 KB
/
som_train_struct
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
141
142
143
144
145
146
147
148
149
150
151
152
class som_train_struct:
def __init__(self,data=[],data_name="",time="",dim=[],dlen=[],msize=[],munits=[],neigh="",phase="",algorithm="",mask=mat(""),previous=[]):
self.type="som_train"
self.algorithm=algorithm
self.data_name=data_name
self.neigh=neigh
self.mask=mask
self.radius_ini=[]
self.radius_fin=[]
self.alpha_ini=[]
self.alpha_type="inv"
self.trainlen=[]
self.time=time
if previous!=[]:
sTprev=previous
else:
sTprev=[]
if data==[]:
D=[]
else:
D=data
dlen=D.shape[0]
dim=D.shape[1]
#dim
if sTprev!=[] and dim==[]:
dim=sTprev.mask.shape[0]
#mask
if self.mask.shape[1]==0 and dim!=[]:
self.mask=ones((dim,1))
#msize, munits
if msize==[]:
msize = zeros(2)
msize[0]=10.0
msize[1]=10.0
else:
s=round(sqrt(munits))
msize[0]=s
msize[1]=round(munits/s)
munits=msize[0]*msize[1]
#previous training
prevalg=""
if sTprev!=[]:
if sTprev.algorithm=="lininit":
prevalg="init"
else:
prevalg=sTprev.algorithm
#determine phase based on previous training
if phase=="":
if self.algorithm=="lininit" or self.algorithm=="randinit":
phase="init"
elif self.algorithm=="batch" or self.algorithm=="seq" or self.algorithm=="":
if sTprev==[]:
phase="rough"
elif prevalg=="init":
phase="rough"
else:
phase="finetune"
else:
phase="train"
#determine the algorithm
if self.algorithm=="":
if phase=="init":
self.algorithm="lininit"
elif prevalg=="init" or prevalg=="":
self.algorithm="batch"
else:
self.algorithm = sTprev.algorithm
#mask
if self.mask.shape[1]==0:
if sTprev!=[]:
self.mask=sTprev.mask
elif dim!=[]:
self.mask=ones((dim,1))
#neighborhood function
if self.neigh=="":
if sTprev!=[] and sTprev.neigh!="":
self.neigh=sTprev.neigh
else:
self.neigh="gaussian"
if phase=="init":
self.alpha_ini=[]
self.alpha_type=""
self.radius_ini=[]
self.radius_fin=[]
self.trainlen=[]
self.neigh=""
else:
mode=phase + '-' + self.algorithm
#learning rate
if self.alpha_ini==[]:
if self.algorithm=="batch":
self.alpha_ini=[]
else:
if phase=="train" or phase=="rough":
self.alpha_ini=0.5
if phase=="finetune":
self.alpha_ini=0.05
if self.alpha_type=="":
if sTprev!=[] and self.alpha_type!="" and self.algorithm!="batch":
self.alpha_type = sTprev.alpha_type
elif self.algorithm=="seq":
self.alpha_type="inv"
#radius
ms=max(msize)
if self.radius_ini==[]:
if sTprev==[] or sTprev.algorithm=="randinit":
self.radius_ini=max(1.0,ceil(ms/4))
elif sTprev.algorithm=="lininit" or sTprev.radius_fin==[]:
self.radius_ini=max(1.0,ceil(ms/8))
else:
self.radius_ini=sTprev.radius_fin
if self.radius_fin==[]:
if phase=="rough":
self.radius_fin=max(1.0,self.radius_ini/4.0)
else:
self.radius_fin=1.0
#trainlen
if self.trainlen==[]:
if munits==[] or dlen==[]:
mpd=0.5
else:
mpd=float(munits/dlen)
if phase=="train":
self.trainlen=ceil(50.0*mpd)
elif phase=="rough":
self.trainlen=ceil(10.0*mpd)
elif phase=="finetune":
self.trainlen=ceil(40.0*mpd)
self.trainlen=max(1.0,self.trainlen)