aboutsummaryrefslogtreecommitdiff
path: root/pix_recNN/RecurrentNeuron.cpp
blob: 1b322c1420eb5434529aa410b30eb6ee08e87fab (plain)
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
/////////////////////////////////////////////////////////////////////////////
//
// class RecurrentNeuron
//
//   source 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. 
//
/////////////////////////////////////////////////////////////////////////////

#include "RecurrentNeuron.h"

namespace TheBrain
{

//--------------------------------------------------
/* Constructor
 */
RecurrentNeuron::RecurrentNeuron(int inputs, int memory)
  : Neuron(inputs), LW_(NULL), mem_data_(NULL)
{
  memory_ = (memory<0) ? 1 : memory+1;
}

//--------------------------------------------------
/* Destructor
 */
RecurrentNeuron::~RecurrentNeuron()
{
   if(LW_)
     delete[] LW_;

   if(mem_data_)
     delete[] mem_data_;
}

//--------------------------------------------------
/* creates a new IW-matrix (size: inputs_) and 
 * b1-vector
 * ATTENTION: if they exist they'll be deleted
 */
void RecurrentNeuron::create()
  throw(NNExcept)
{
  // delete if they exist
  if(IW_)
    delete[] IW_;
  if(LW_)
    delete[] LW_;
  if(mem_data_)
    delete[] mem_data_;

  IW_ = new float[inputs_];
  LW_ = new float[memory_];
  mem_data_ = new float[memory_];

  if(!IW_ || !LW_ || !mem_data_)
    throw NNExcept("No memory for Neurons!");

  index_=0;
}

//--------------------------------------------------
/* inits the weight matrix and the bias vector of
 * the network with random values between [min|max]
 */
void RecurrentNeuron::initRand(const int &min, const int &max)
  throw(NNExcept)
{
  if(!IW_ || !LW_)
    throw NNExcept("You must first create the Net!");

  // make randomvalue between 0 and 1
  // then map it to the bounds
  b1_ = ((float)rand()/(float)RAND_MAX)*(max-min) + min;

  for(int i=0; i<inputs_; i++)
    {
      IW_[i] = ((float)rand()/(float)RAND_MAX)*(max-min) + min;
    }
  for(int i=0; i<memory_; i++)
    {
      //LW_[i] = ((float)rand()/(float)RAND_MAX)*(max-min) + min;
      LW_[i] = ((float)rand()/(float)RAND_MAX)*(min);
    }
}

//--------------------------------------------------
/* inits the net with given weight matrix and bias
 * (makes a deep copy)
 * ATTENTION: the dimension of IW-pointer must be the same
 *            as the inputs (also for LW) !!!
 */
void RecurrentNeuron::init(const float *IW, const float *LW, float b1)
  throw(NNExcept)
{
  if(!IW_ || !LW_)
    throw NNExcept("You must first create the Net!");

  b1_ = b1;

  for(int i=0; i<inputs_; i++)
      IW_[i] = IW[i];
  for(int i=0; i<memory_; i++)
      LW_[i] = LW[i];
}

//--------------------------------------------------
/* calculates the output with the current IW, b1 values
 * ATTENTION: the array input_data must be in the same
 *            size as inputs_
 */
float RecurrentNeuron::calculate(float *input_data)
{
  float output = 0;

  // multiply the inputs with the weight matrix IW
  for(int i=0; i<inputs_; i++)
    {
      output += input_data[i] * IW_[i];
    }

  // map input values to the range
  output /= range_; 

  // multiply memory with weight matrix LW
  // the index is used to make something
  // like a simple list or ringbuffer
  for(int i=0; i<memory_; i++)
    {
      output += mem_data_[index_] * LW_[i];
      index_ = (index_+1) % memory_;
    }

  // now add bias
  output += b1_;

  // finally save the new output in memory
  mem_data_[index_] = output;
  index_ = (index_+1) % memory_;

  //post("input: %f %f %f, IW: %f %f %f, b: %f",
  //      input_data[0], input_data[1], input_data[2],
  //      IW_[0], IW_[1], IW_[2], b1_);
  //post("output: %f",output);

  return (output);
}

//--------------------------------------------------
/* 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 (output_data is not
 * calculated in that method !), target_output is the desired
 * output data
 * (this is the LMS-algorithm to train linear neural networks)
 * ATTENTION: the array input_data must be in the same
 *            size as inputs_
 * returns the calculated output
 */
// float RecurrentNeuron::trainLMS(const float *input_data, 
// 				const float &target_output)
// {
//   // calculate output value:

//   float output = 0;

//   // multiply the inputs with the weight matrix IW
//   for(int i=0; i<inputs_; i++)
//     {
//       output += input_data[i] * IW_[i];
//     }

//   // map input values to the range
//   output /= range_; 

//   // multiply memory with weight matrix LW
//   // the index is used to make something
//   // like a simple list or ringbuffer
//   for(int i=0; i<memory_; i++)
//     {
//       output += mem_data_[index_] * LW_[i];
//       index_ = (index_+1) % memory_;
//     }

//   // now add bias
//   output += b1_;

//   //----------------

//   // this is the LMS-algorithm to train linear
//   // neural networks

//   // calculate the error signal:
//   float error = (target_output - output);

//   // now change IW
//   for(int i=0; i<inputs_; i++)
//     IW_[i] += 2 * learn_rate_ * error * (input_data[i]/range_);

//   // change LW
//   for(int i=0; i<memory_; i++)
//     {
//       LW_[i] += 2 * learn_rate_ * error * mem_data_[index_];
//       index_ = (index_+1) % memory_;     
//     }

//   // and the bias
//   b1_ += 2 * learn_rate_ * error; 

//   //-----------------

//   // finally save the new output in memory
//   mem_data_[index_] = output;
//   index_ = (index_+1) % memory_;

//   return (output);
// }


} // end of namespace