-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounterprop.cpp
More file actions
243 lines (217 loc) · 6.82 KB
/
Copy pathcounterprop.cpp
File metadata and controls
243 lines (217 loc) · 6.82 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "counterprop.h"
#define to_counterprop_input(_neuron) dynamic_cast<neuron_counterprop_input *>(_neuron)
#define to_counterprop_kohonen(_neuron) dynamic_cast<neuron_counterprop_kohonen *>(_neuron)
#define to_counterprop_grossberg(_neuron) dynamic_cast<neuron_counterprop_grossberg *>(_neuron)
neuron_counterprop_input::neuron_counterprop_input() : neuron_input()
{
}
neuron_counterprop_input::~neuron_counterprop_input()
{
}
neuron_counterprop_kohonen::neuron_counterprop_kohonen() : neuron_hide()
{
}
neuron_counterprop_kohonen::~neuron_counterprop_kohonen()
{
}
void neuron_counterprop_kohonen::set_active()
{
_status = 1;
}
void neuron_counterprop_kohonen::set_nonactive()
{
_status = 0;
}
neuron_counterprop_grossberg::neuron_counterprop_grossberg() : neuron_output()
{
}
neuron_counterprop_grossberg::~neuron_counterprop_grossberg()
{
}
counter_propagation_net::counter_propagation_net(int inplen, int kohlen, int groslen)
{
//TODO: Verifying
_inplen = inplen;
_input = new neuron *[_inplen];
for (int i = 0; i < _inplen; i++)
{
_input[i] = new neuron_counterprop_input;
}
_kohlen = kohlen;
_kohonen = new neuron *[_kohlen];
_threshold = new neuron_threshold;
_thr_to_koh = new synapse *[_kohlen];
for (int i = 0; i < _kohlen; i++)
{
_kohonen[i] = new neuron_counterprop_kohonen;
to_counterprop_kohonen(_kohonen[i]) -> set_function(linear_func);
_thr_to_koh[i] = new synapse;
_thr_to_koh[i] -> set_weight(0);
_thr_to_koh[i] -> set_start(_threshold);
_thr_to_koh[i] -> set_end(_kohonen[i]);
to_threshold(_threshold) -> add_output(_thr_to_koh[i]);
to_counterprop_kohonen(_kohonen[i]) -> add_input(_thr_to_koh[i]);
}
_active_kohonen = 0;
_groslen = groslen;
_grossberg = new neuron *[_groslen];
for (int i = 0; i < _groslen; i++)
{
_grossberg[i] = new neuron_counterprop_grossberg;
to_counterprop_grossberg(_grossberg[i]) -> set_function(linear_func);
}
_inp_to_koh = new synapse *[_inplen * _kohlen];
_koh_to_gros = new synapse *[_kohlen * _groslen];
for (int j = 0; j < _kohlen; j++)
{
for (int i = 0; i < _inplen; i++)
{
_inp_to_koh[i * _kohlen + j] = new synapse;
_inp_to_koh[i * _kohlen + j] -> set_start(_input[i]);
_inp_to_koh[i * _kohlen + j] -> set_end(_kohonen[j]);
_inp_to_koh[i * _kohlen + j] -> set_weight(1 / sqrt(_inplen));
// _inp_to_koh[i * _kohlen + j] -> set_weight(rand() % 1000);
to_counterprop_input(_input[i]) -> add_output(_inp_to_koh[i * _kohlen + j]);
to_counterprop_kohonen(_kohonen[j]) -> add_input(_inp_to_koh[i * _kohlen + j]);
}
for (int k = 0; k < _groslen; k++)
{
_koh_to_gros[j * _groslen + k] = new synapse;
_koh_to_gros[j * _groslen + k] -> set_start(_kohonen[j]);
_koh_to_gros[j * _groslen + k] -> set_end(_grossberg[k]);
_koh_to_gros[j * _groslen + k] -> set_weight(0);
// _koh_to_gros[j * _groslen + k] -> set_weight((rand() % 2) * 2 - 1);
// printf("%d -> %d: %f\n", j, k, _koh_to_gros[j * _groslen + k] -> get_weight());
to_counterprop_kohonen(_kohonen[j]) -> add_output(_koh_to_gros[j * _groslen + k]);
to_counterprop_grossberg(_grossberg[k]) -> add_input(_koh_to_gros[j * _groslen + k]);
}
// neuron_counterprop_kohonen *cur_koh;
// synapse *cur;
// int s;
// for (int i = 0; i < _kohlen; i++)
// {
// cur_koh = to_counterprop_kohonen(_kohonen[i]);
// s = 0;
// for (int j = 0; j < _inplen; j++)
// {
// cur = cur_koh->get_input_at(j);
// s += cur->get_weight() * cur->get_weight();
// }
// s = sqrt(s);
// for (int j = 0; j < _inplen; j++)
// {
// cur = cur_koh->get_input_at(j);
// cur->set_weight(cur->get_weight() / s);
// }
// }
}
}
counter_propagation_net::~counter_propagation_net()
{
}
void counter_propagation_net::set_active_kohonen(neuron_counterprop_kohonen *active)
{
_active_kohonen = active;
for (int i = 0; i < _kohlen; i++)
{
to_counterprop_kohonen(_kohonen[i]) -> set_nonactive();
}
active -> set_active();
}
neuron_counterprop_kohonen *counter_propagation_net::get_active_cohonen()
{
return _active_kohonen;
}
float *counter_propagation_net::get_result(float* input)
{
for (int i = 0; i < _inplen; i++)
{
to_counterprop_input(_input[i]) -> set_status(input[i]);
}
for (int i = 0; i < _kohlen; i++)
{
to_counterprop_kohonen(_kohonen[i]) -> refresh_status();
}
int active_koh = 0;
for (int i = 1; i < _kohlen; i++)
{
if (_kohonen[i] -> get_status() > _kohonen[active_koh] -> get_status()) active_koh = i;
}
set_active_kohonen(to_counterprop_kohonen(_kohonen[active_koh]));
float *res = new float [_groslen];
for (int i = 0; i < _groslen; i++)
{
to_counterprop_grossberg(_grossberg[i]) -> refresh_status();
res[i] = _grossberg[i] -> get_status();
}
return res;
}
void counter_propagation_net::learn(float** learning_set, float** results, int n, float speedkoh, float speedgros)
{
//TODO: Veryfying
float koef_of_inp = 0.0001;
float koef_multiplier = 1;
float koef_addition = .0001;
float threshold = 1;
float *tmp_inp = new float [_inplen];
float *tmp_res;
neuron_counterprop_kohonen *active_koh;
synapse *cur;
float s;
// for (int i = 0; i < _kohlen; i++)
// {
// active_koh = to_counterprop_kohonen(_kohonen[i]);
// s = 0;
// for (int j = 0; j < _inplen; j++)
// {
// cur = active_koh->get_input_at(j);
// s += cur->get_weight() * cur->get_weight();
// }
// s = sqrt(s);
// // printf("%f\n", s);
// for (int j = 0; j < _inplen; j++)
// {
// cur = active_koh->get_input_at(j);
// printf("%10f ", cur -> get_weight());
// cur -> set_weight(cur->get_weight() / s /* * koef_of_inp + (1 - koef_of_inp) / sqrt(_inplen)*/);
// }
// printf("\n");
// for (int j = 0; j < _inplen; j++)
// {
// cur = active_koh->get_input_at(j);
// printf("%10f ", cur -> get_weight());
// }
// printf("\n");
// }
for (int x = 0; x < 10000; x++)
{
for (int k = 0; k < n; k++)
{
for (int i = 0; i < _inplen; i++)
{
tmp_inp[i] = learning_set[k][i] /** koef_of_inp + (1 - koef_of_inp) / sqrt(_inplen)*/;
}
tmp_res = get_result(tmp_inp);
active_koh = get_active_cohonen();
// printf("%d: %d - %x \n", x, k, active_koh);
for (int i = 0; i < active_koh->get_input_length(); i++)
{
cur = active_koh->get_input_at(i);
// cur->set_weight(cur->get_start()->get_status() * speed + cur->get_weight()*(1 - speed));
if (to_threshold(cur -> get_start())) cur -> add_weight(-threshold);
else cur -> add_weight((cur -> get_start() -> get_status() - cur -> get_weight()) * speedkoh * (1 - x / 10000.0));
}
for (int i = 0; i < _kohlen; i++)
{
to_threshold(_threshold) -> get_output_at(i) -> add_weight(threshold / _kohlen);
}
for (int i = 0; i < active_koh -> get_output_length(); i++)
{
cur = active_koh->get_output_at(i);
cur->add_weight((results[k][i] - tmp_res[i]) * speedgros);
}
}
koef_of_inp *= koef_multiplier;
koef_of_inp += koef_addition;
}
}