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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
|
/////////////////////////////////////////////////////////////////////////////
//
// class NNet
//
// this is a template for all the nets
// (see NeuralNet documentations for more information)
//
// header file
//
// Copyright (c) 2005 Georg Holzmann <grh@gmx.at>
//
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
/////////////////////////////////////////////////////////////////////////////
#ifndef _INCLUDE_NEURAL_TEMPLATE_NET__
#define _INCLUDE_NEURAL_TEMPLATE_NET__
#include "NNActivation.h"
#include "NNException.h"
namespace TheBrain
{
template <class HiddNeuronType,class OutNeuronType>
class NNet
{
protected:
/* the number of output values
* this is automatically also the
* number of output neurons !
*/
int output_val_;
/* the number of hidden neurons
* per one output neuron
* (this net has one hidden layer,
* so this is the number of hidden
* neurons is hidden_val_*output_val_)
*/
int hidden_val_;
/* nr of input values per one output neuron
* (so the number of input values are
* input_val_*output_val_)
*/
int input_val_;
/* the memory of the output layer
* if you use a recurrent neuron, this
* determines how much output values the
* recurrent neurons can remeber
* these values are fed back as new input
*/
int memory_out_;
/* the memory of the hidden layer
* if you use a recurrent neuron, this
* determines how much output values the
* recurrent neurons can remeber
* these values are fed back as new input
*/
int memory_hidden_;
/* these are the output neurons
*/
OutNeuronType *out_neurons_;
/* these are the hidden neurons
*/
HiddNeuronType *hidden_neurons_;
/* function pointer to the activation
* function of the output neurons
*/
float (*output_act_f)(float value);
/* function pointer to the activation
* function of the hidden neurons
*/
float (*hidden_act_f)(float value);
/* function pointer to the derivation of the
* activation function of the hidden neurons
*/
float (*hidden_act_f_d)(float value);
public:
/* Constructor
*/
NNet(int input_val=1, int hidden_val=1, int output_val=1, int memory_out=0,
int memory_hidden=1, int HIDDEN_ACT_FUNC=0, int OUT_ACT_FUNC=0);
/* Destructor
*/
virtual ~NNet();
//-----------------------------------------------------
/* Set/Get learning rate
*/
virtual void setLearningRate(float learn_rate);
virtual float getLearningRate() const;
/* Set/Get range
* (see Neuron.h)
*/
virtual void setRange(float range);
virtual float getRange() const;
/* some more get/set methods
*/
virtual void setOutputVal(int output_val)
throw();
virtual int getOutputVal() const;
virtual void setHiddenVal(int hidden_val)
throw();
virtual int getHiddenVal() const;
virtual void setInputVal(int input_val)
throw();
virtual int getInputVal() const;
virtual void setMemoryOut(int memory)
throw();
virtual int getMemoryOut() const;
virtual void setMemoryHidden(int memory)
throw();
virtual int getMemoryHidden() const;
//-----------------------------------------------------
/* creates the network
*/
virtual void create()
throw(NNExcept);
/* inits the weight matrix and the bias vector of
* the network with random values between [min|max]
*/
virtual void initRand(const int &min, const int &max)
throw(NNExcept);
/* calculates the output with the current Net and writes
* it in the array output_data
* ATTENTION: array input_data must be a matrix in the form:
* float[output_val_][input_val_]
* array output_data must be in size output_val_
* (there is no checking !!!)
*/
virtual void calculate(float **input_data, float *output_data);
/* this method trains the network:
* input_data is, as above, the input data, output_data is the
* output of the current net with input_data, target_output is
* the desired output data
* (this is the a truncated backpropagation through time
* algorithm to train the network)
* ATTENTION: array input_data must be a matrix in the form:
* float[output_val_][input_val_]
* array output_data must be in size output_val_
* array target_output must be in size output_val_
* (there is no checking !!!)
*/
virtual void trainBTT(float **input_data, float *output_data,
float *target_output);
//-----------------------------------------------------
/* saves the contents of the current net to file
*/
virtual void save(string filename)
throw(NNExcept);
/* loads the parameters of the net from file
*/
virtual void load(string filename)
throw(NNExcept);
//-----------------------------------------------------
private:
/* output of the hidden layer with activation function
*/
float *hidden_a_;
/* output of the hidden layer without activation function
*/
float *hidden_s_;
/* error signal of the neurons in the hidden layer
*/
float *hidden_error_;
/* out signal without activation function
*/
float out_s_;
/* error signal of the output layer
*/
float out_error_;
/* Copy Construction is not allowed
*/
NNet(const NNet<HiddNeuronType,OutNeuronType> &src)
{ }
/* assignement operator is not allowed
*/
const NNet<HiddNeuronType,OutNeuronType>& operator=
(const NNet<HiddNeuronType,OutNeuronType>& src)
{ return *this; }
};
//--------------------------------------------------
/* Constructor
*/
template <class HiddNeuronType, class OutNeuronType>
NNet<HiddNeuronType,OutNeuronType>
::NNet(int input_val, int hidden_val, int output_val, int memory_out,
int memory_hidden, int HIDDEN_ACT_FUNC, int OUT_ACT_FUNC)
: out_neurons_(NULL), hidden_neurons_(NULL), hidden_a_(NULL),
hidden_s_(NULL), hidden_error_(NULL)
{
output_val_ = (output_val<1) ? 1 : output_val;
hidden_val_ = (hidden_val<0) ? 0 : hidden_val;
input_val_ = (input_val<1) ? 1 : input_val;
memory_out_ = (memory_out<0) ? 0 : memory_out;
memory_hidden_ = (memory_hidden<0) ? 0 : memory_hidden;
// choose hidden activation function:
switch(HIDDEN_ACT_FUNC)
{
case SIGMOID:
hidden_act_f = act_sigmoid;
hidden_act_f_d = act_sigmoid_derive;
break;
case TANH:
hidden_act_f = act_tanh;
hidden_act_f_d = act_tanh_derive;
break;
default:
case LINEAR:
hidden_act_f = act_linear;
hidden_act_f_d = act_linear_derive;
break;
}
// choose out function:
switch(OUT_ACT_FUNC)
{
case SIGMOID:
output_act_f = act_sigmoid;
break;
case TANH:
output_act_f = act_tanh;
break;
default:
case LINEAR:
output_act_f = act_linear;
break;
}
}
//--------------------------------------------------
/* Destructor
*/
template <class HiddNeuronType, class OutNeuronType>
NNet<HiddNeuronType, OutNeuronType>::~NNet()
{
if(hidden_neurons_)
delete[] hidden_neurons_;
if(out_neurons_)
delete[] out_neurons_;
if(hidden_a_)
delete[] hidden_a_;
if(hidden_s_)
delete[] hidden_s_;
if(hidden_error_)
delete[] hidden_error_;
}
//--------------------------------------------------
/* creates the network
*/
template <class HiddNeuronType, class OutNeuronType>
void NNet<HiddNeuronType,OutNeuronType>::create()
throw(NNExcept)
{
// delete if they exist
if(out_neurons_)
delete[] out_neurons_;
if(hidden_neurons_)
delete[] hidden_neurons_;
if(hidden_a_)
delete[] hidden_a_;
if(hidden_s_)
delete[] hidden_s_;
if(hidden_error_)
delete[] hidden_error_;
out_neurons_ = new OutNeuronType[output_val_](input_val_,memory_out_);
hidden_neurons_ = new HiddNeuronType[hidden_val_*output_val_](input_val_,memory_hidden_);
if(!out_neurons_ || !hidden_neurons_)
throw NNExcept("No memory for Neurons!");
// create the temporary storage
hidden_a_ = new float[hidden_val_];
hidden_s_ = new float[hidden_val_];
hidden_error_ = new float[hidden_val_];
if(!hidden_a_ || !hidden_s_ || !hidden_error_)
throw NNExcept("No memory for Neurons!");
// create all the neurons
for(int i=0; i<output_val_; i++)
out_neurons_[i].create();
for(int i=0; i<hidden_val_*output_val_; i++)
hidden_neurons_[i].create();
}
//--------------------------------------------------
/* inits the weight matrix and the bias vector of
* the network with random values between [min|max]
*/
template <class HiddNeuronType, class OutNeuronType>
void NNet<HiddNeuronType,OutNeuronType>::initRand(const int &min, const int &max)
throw(NNExcept)
{
if(!out_neurons_)
throw NNExcept("You must first create the Net!");
// init all the neurons
for(int i=0; i<output_val_; i++)
out_neurons_[i].initRand(min,max);
for(int i=0; i<hidden_val_*output_val_; i++)
hidden_neurons_[i].initRand(min,max);
}
//--------------------------------------------------
/* calculates the output with the current Net and writes
* it in the array output_data
* ATTENTION: array input_data must be a matrix in the form:
* float[output_val_][input_val_]
* array output_data must be in size output_val_
* (there is no checking !!!)
*/
template <class HiddNeuronType, class OutNeuronType>
void NNet<HiddNeuronType,OutNeuronType>::calculate(float **input_data, float *output_data)
{
for(int i=0; i<output_val_; i++)
{
// 1.: calculation of the hidden layer
for(int j=0; j<hidden_val_; j++)
{
hidden_a_[j] = hidden_act_f(
hidden_neurons_[i*hidden_val_+j].calculate(input_data[i]) );
}
// 2.: calculation of the output layer
*output_data++ = output_act_f( out_neurons_[i].calculate(hidden_a_) );
}
}
//--------------------------------------------------
/* this method trains the network:
* input_data is, as above, the input data, output_data is the
* output of the current net with input_data, target_output is
* the desired output data
* (this is the a truncated backpropagation through time
* algorithm to train the network)
* ATTENTION: array input_data must be a matrix in the form:
* float[output_val_][input_val_]
* array output_data must be in size output_val_
* array target_output must be in size output_val_
* (there is no checking !!!)
*/
template <class HiddNeuronType, class OutNeuronType>
void NNet<HiddNeuronType,OutNeuronType>::trainBTT(float **input_data, float *output_data,
float *target_output)
{
post("train");
for(int i=0; i<output_val_; i++)
{
//---------------------------------------------------------
// 1. Forward - Pass:
//
// the output of the hidden and the output-layer
// are calculated and saved (before and after
// the activation function)
// calculation of the hidden layer
for(int j=0; j<hidden_val_; j++)
{
hidden_s_[j] = hidden_neurons_[i*hidden_val_+j].calculate(input_data[i]);
hidden_a_[j] = hidden_act_f(hidden_s_[j]);
}
// calculation of the output layer
out_s_ = out_neurons_[i].calculate(hidden_a_);
output_data[i] = output_act_f(out_s_);
//---------------------------------------------------------
// 2. Backward - Pass:
//
// calculation of the error signals
// (they are also stored)
// output layer
out_error_ = output_data[i] - target_output[i];
// hidden layer:
for(int j=0; j<hidden_val_; j++)
{
hidden_error_[j] = hidden_act_f_d( hidden_s_[j]+0.1 ) *
( out_error_ * out_neurons_[i].getIW(j) );
}
//---------------------------------------------------------
// 3. Modification of the weights:
for(int j=0; j<hidden_val_; j++)
{
// output layer:
out_neurons_[i].setIW(j,
out_neurons_[i].getIW(j) -
getLearningRate() * out_error_
* hidden_a_[j] );
// hidden layer:
for(int k=0; k<input_val_; k++)
{
hidden_neurons_[i*hidden_val_+j].setIW(k,
hidden_neurons_[i*hidden_val_+j].getIW(k) -
getLearningRate() * hidden_error_[j]
* input_data[i][k]/hidden_neurons_[0].getRange() );
}
// recurrent part of the hidden layer:
float delta = getLearningRate() * hidden_error_[j] * hidden_a_[j];
for(int k=0; k<memory_hidden_; k++)
{
hidden_neurons_[i*hidden_val_+j].setLW(k,
hidden_neurons_[i*hidden_val_+j].getLW(k) - delta);
}
}
// recurrent part of the output layer:
float delta = getLearningRate() * out_error_ * output_data[i];
for(int j=0; j<memory_out_; j++)
{
out_neurons_[i].setLW(j,
out_neurons_[i].getLW(j) - delta);
}
}
}
//--------------------------------------------------
/* saves the contents of the current net to file
*/
template <class HiddNeuronType, class OutNeuronType>
void NNet<HiddNeuronType,OutNeuronType>::save(string filename)
throw(NNExcept)
{
}
//--------------------------------------------------
/* loads the parameters of the net from file
*/
template <class HiddNeuronType, class OutNeuronType>
void NNet<HiddNeuronType,OutNeuronType>::load(string filename)
throw(NNExcept)
{
}
//-----------------------------------------------------
/* Set/Get learning rate
* (see Neuron.h)
*/
template <class HiddNeuronType, class OutNeuronType>
void NNet<HiddNeuronType,OutNeuronType>::setLearningRate(float learn_rate)
{
learn_rate = (learn_rate<0) ? 0 : learn_rate;
for(int i=0; i<output_val_; i++)
out_neurons_[i].setLearningRate(learn_rate);
for(int i=0; i<hidden_val_*output_val_; i++)
hidden_neurons_[i].setLearningRate(learn_rate);
}
template <class HiddNeuronType, class OutNeuronType>
float NNet<HiddNeuronType, OutNeuronType>::getLearningRate() const
{
return out_neurons_[0].getLearningRate();
}
//-----------------------------------------------------
/* Set/Get range
* (see Neuron.h)
*/
template <class HiddNeuronType, class OutNeuronType>
void NNet<HiddNeuronType,OutNeuronType>::setRange(float range)
{
for(int i=0; i<output_val_; i++)
out_neurons_[i].setRange(1);
for(int i=0; i<hidden_val_*output_val_; i++)
hidden_neurons_[i].setRange(range);
}
template <class HiddNeuronType, class OutNeuronType>
float NNet<HiddNeuronType, OutNeuronType>::getRange() const
{
return hidden_neurons_[0].getRange();
}
//-----------------------------------------------------
/* get/set output_val_
*/
template <class HiddNeuronType, class OutNeuronType>
void NNet<HiddNeuronType,OutNeuronType>::setOutputVal(int output_val)
throw()
{
output_val_ = (output_val<1) ? 1 : output_val;
create();
}
template <class HiddNeuronType, class OutNeuronType>
int NNet<HiddNeuronType,OutNeuronType>::getOutputVal() const
{
return output_val_;
}
//-----------------------------------------------------
/* get/set hidden_val_
*/
template <class HiddNeuronType, class OutNeuronType>
void NNet<HiddNeuronType,OutNeuronType>::setHiddenVal(int hidden_val)
throw()
{
hidden_val_ = (hidden_val<1) ? 1 : hidden_val;
create();
}
template <class HiddNeuronType, class OutNeuronType>
int NNet<HiddNeuronType,OutNeuronType>::getHiddenVal() const
{
return hidden_val_;
}
//-----------------------------------------------------
/* get/set input_val_
*/
template <class HiddNeuronType, class OutNeuronType>
void NNet<HiddNeuronType,OutNeuronType>::setInputVal(int input_val)
throw()
{
input_val_ = (input_val<1) ? 1 : input_val;
create();
}
template <class HiddNeuronType, class OutNeuronType>
int NNet<HiddNeuronType,OutNeuronType>::getInputVal() const
{
return input_val_;
}
//-----------------------------------------------------
/* get/set memory of the output layer
*/
template <class HiddNeuronType, class OutNeuronType>
void NNet<HiddNeuronType,OutNeuronType>::setMemoryOut(int memory)
throw()
{
memory_out_ = (memory<0) ? 0 : memory;
create();
}
template <class HiddNeuronType, class OutNeuronType>
int NNet<HiddNeuronType,OutNeuronType>::getMemoryOut() const
{
return memory_out_;
}
//-----------------------------------------------------
/* get/set memory of the hidden layer
*/
template <class HiddNeuronType, class OutNeuronType>
void NNet<HiddNeuronType,OutNeuronType>::setMemoryHidden(int memory)
throw()
{
memory_hidden_ = (memory<0) ? 0 : memory;
create();
}
template <class HiddNeuronType, class OutNeuronType>
int NNet<HiddNeuronType,OutNeuronType>::getMemoryHidden() const
{
return memory_hidden_;
}
} // end of namespace
#endif //_INCLUDE_LIN_NEURAL_NET__
|