-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcls_ann_center_neuron.h
executable file
·215 lines (181 loc) · 4.76 KB
/
cls_ann_center_neuron.h
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
#include "ann_import.h"
template<typename FUNC>
class cls_ann_center_neuron : public cls_ann_abstract_neuron<FUNC>
{
protected:
double *center;
double *delta_center;
int dim;
virtual void re_cache_error()
{
this->error_cache_valid = true;
}
virtual void re_cache_output()
{
if (this->output_cache_valid)
return;
if (this->get_input_count() != get_dim())
throw cls_exception_not_identical("cls_ann_center_neuron::re_cache_output() - the input count must match the dim.");
this->activation_cache = 0.0;
#ifdef __USING_GCC__
typename vector<cls_ann_link<FUNC>*>::iterator itor;
#else
vector<cls_ann_link<FUNC>*>::iterator itor;
#endif
int j=0;
for(itor = this->input_links.begin();itor != this->input_links.end();itor++,j++)
{
assert(*itor);
double tmp = (*itor)->get_source()->get_output() - center[j];
this->activation_cache += (tmp*tmp);
}
this->activation_cache = (double)sqrt(1.0*this->activation_cache);
this->output_cache = (this->func)(this->activation_cache);
this->output_cache_valid = true;
}
public:
cls_ann_center_neuron(int label,int dim)
:cls_ann_abstract_neuron<FUNC>(label)
{
this->dim = dim;
center = new double[dim];
for(int cnt=0;cnt<dim;cnt++)
{
center[cnt] = a_unfirom_random(-0.1,0.1);
}
}
void calc_new_weights(double learningRate,double a)
{
#ifdef __USING_GCC__
typename vector<cls_ann_link<FUNC>*>::iterator k1,i;
#else
vector<cls_ann_link<FUNC>*>::iterator k1,i;
#endif
int k;
double act,dact,factor;
#ifdef __USING_GCC__
cls_ann_link<FUNC> *inl,*outl;
#else
cls_ann_link<FUNC> *inl,*outl;
#endif
if (this->get_input_count() != get_dim())
{
throw cls_exception_not_identical("cls_ann_center_neuron::calc_new_center() - centers must match the dims.");
}
for (k1=this->input_links.begin(), k=0; k1!=this->input_links.end(); k1++,k++)
{
assert(*k1);
inl=(cls_ann_link<FUNC>*)(*k1);
delta_center[k] = (double)0.0;
act = this->get_activation();
dact = (double)(exp(-1*act*act)*2);
factor = (inl->get_source()->get_output() - center[k])*dact;///act;
for (i=this->output_links.begin(); i!=this->output_links.end(); i++)
{
assert(*i);
outl=(cls_ann_link<FUNC>*)(*i);
delta_center[k] += outl->get_destination()->get_error() * outl->get_weight();
}
delta_center[k] = delta_center[k] * factor * learningRate;
}
}
void update()
{
int dim = get_dim();
int i;
for (i=0; i<dim; i++)
{
center[i] += delta_center[i];
delta_center[i]=(double)0.0;
}
this->invalidate_output_cache();
this->invalidate_error_cache();
}
cls_ann_center_neuron(int label,vector<double> center)
:cls_ann_neuron<FUNC>(label)
{
this->dim = center.size();
set_center(center);
}
cls_ann_center_neuron(int label,int dim,double center[])
:cls_ann_neuron<FUNC>(label)
{
this->dim = dim;
set_center(center);
}
virtual ~cls_ann_center_neuron()
{
delete [] center;
}
vector<double> get_center() const
{
vector<double> re;
for(int cnt=0;cnt<dim;cnt++)
{
re.push_back(center[cnt]);
}
return re;
}
virtual void set_center(vector<double> center)
{
int dim = get_dim();
for(int cnt=0;cnt<dim;cnt++)
this->center[cnt] = center[cnt];
}
virtual void set_center(double center[])
{
int dim = get_dim();
for(int cnt=0;cnt<dim;cnt++)
this->center[cnt] = center[cnt];
}
virtual void connect(cls_ann_neuron<FUNC> *from)
{
disconnect(from);
cls_ann_link<FUNC> *link = new cls_ann_link<FUNC>(from,this,1.0);
this->input_links.push_back(link);
from->output_links.push_back(link);
this->invalidate_output_cache();
}
virtual unsigned get_dim() const
{
return dim;
}
virtual void set_desired_output( double desired )
{
throw cls_exception_io("The method or operation is not implemented.");
}
virtual void save( ofstream& fout ) const
{
try
{
cls_ann_abstract_neuron<FUNC>::save(fout);
fout.write((char*)&dim,sizeof(dim));
for(int cnt=0;cnt<dim;cnt++)
{
fout.write((char*)¢er[cnt],sizeof(double));
}
}
catch(cls_exception_io exc)
{
throw cls_exception_io(exc.what() + "cls_ann_center_neuron::save() - the file is bad for output.\n");
}
}
virtual void load( ifstream& fin )
{
delete [] center;
try
{
cls_ann_abstract_neuron<FUNC>::load(fin);
fin.read((char*)&dim,sizeof(dim));
center = new double[dim];
for(int cnt=0;cnt<dim;cnt++)
{
fin.read((char*)¢er[cnt],sizeof(double));
}
}
catch(cls_exception_io exc)
{
throw cls_exception_io(exc.what() + "cls_ann_center_neuron::load() - the file is bad for input.\n");
}
}
};