32#ifndef RCSC_ANN_BPN1_H
33#define RCSC_ANN_BPN1_H
57 return 1.0 / ( 1.0 + std::exp( - x ) );
67 return - std::log( 1.0 / y - 1.0 );
78 return s * ( 1.0 - s );
89 return y * ( 1.0 - y );
149template < std::size_t INPUT,
152 typename FuncH = SigmoidFunc,
153 typename FuncO = SigmoidFunc >
171 std::array< value_type, INPUT + 1 > M_weight_i_to_h[HIDDEN];
173 std::array< value_type, INPUT + 1 > M_delta_weight_i_to_h[HIDDEN];
176 std::array< value_type, HIDDEN + 1 > M_weight_h_to_o[OUTPUT];
178 std::array< value_type, HIDDEN + 1 > M_delta_weight_h_to_o[OUTPUT];
184 mutable std::array< value_type, HIDDEN + 1 > M_hidden_layer;
218 template <
typename RNG >
234 M_hidden_layer.assign( 0 );
235 M_hidden_layer.back() = 1;
236 for ( std::size_t i = 0; i < HIDDEN; ++i )
238 M_weight_i_to_h[i].assign( 0 );
239 M_delta_weight_i_to_h[i].assign( 0 );
241 for ( std::size_t i = 0; i < OUTPUT; ++i )
243 M_weight_h_to_o[i].assign( 0 );
244 M_delta_weight_h_to_o[i].assign( 0 );
252 template <
typename RNG >
255 for ( std::size_t i = 0; i < HIDDEN; ++i )
257 std::generate( M_weight_i_to_h[i].begin(),
258 M_weight_i_to_h[i].end(),
261 for ( std::size_t i = 0; i < OUTPUT; ++i )
263 std::generate( M_weight_h_to_o[i].begin(),
264 M_weight_h_to_o[i].end(),
279 for ( std::size_t i = 0; i < HIDDEN; ++i )
281 value_type sum = std::inner_product( input.begin(),
283 M_weight_i_to_h[i].begin(),
286 sum += M_weight_i_to_h[i].back();
287 M_hidden_layer[i] = func_h( sum );
291 for ( std::size_t i = 0; i < OUTPUT; ++i )
293 value_type sum = std::inner_product( M_hidden_layer.begin(),
294 M_hidden_layer.end(),
295 M_weight_h_to_o[i].begin(),
298 output[i] = func_o( sum );
318 for ( std::size_t i = 0; i < OUTPUT; ++i )
321 output_back[i] = err * func_o.diffAtY( output[i] );
325 std::array< value_type, HIDDEN > hidden_back;
327 for ( std::size_t i = 0; i < HIDDEN; ++i )
330 for ( std::size_t j = 0; j < OUTPUT; ++j )
332 sum += output_back[j] * M_weight_h_to_o[j][i];
334 hidden_back[i] = sum * func_h.diffAtY( M_hidden_layer[i] );
338 for ( std::size_t i = 0; i < OUTPUT; ++i )
340 for ( std::size_t j = 0; j < HIDDEN + 1; ++j )
342 M_delta_weight_h_to_o[i][j]
343 = M_eta * M_hidden_layer[j] * output_back[i]
344 + M_alpha * M_delta_weight_h_to_o[i][j];
345 M_weight_h_to_o[i][j]
346 += M_delta_weight_h_to_o[i][j];
352 for ( std::size_t i = 0; i < HIDDEN; ++i )
354 for ( std::size_t j = 0; j < INPUT; ++j )
356 M_delta_weight_i_to_h[i][j]
357 = M_eta * input[j] * hidden_back[i]
358 + M_alpha * M_delta_weight_i_to_h[i][j];
359 M_weight_i_to_h[i][j]
360 += M_delta_weight_i_to_h[i][j];
365 for ( std::size_t i = 0; i < HIDDEN; ++i )
367 M_delta_weight_i_to_h[i][INPUT]
368 = M_eta * 1.0 * hidden_back[i]
369 + M_alpha * M_delta_weight_i_to_h[i][INPUT];
370 M_weight_i_to_h[i][INPUT]
371 += M_delta_weight_i_to_h[i][INPUT];
378 for ( std::size_t i = 0; i < OUTPUT; ++i )
380 total_error += std::pow( teacher[i] - output[i], 2 );
397 for ( std::size_t i = 0; i < HIDDEN; ++i )
399 for ( std::size_t j = 0; j < INPUT + 1; ++j )
401 if ( ! is.good() )
return false;
402 is >> M_weight_i_to_h[i][j];
405 for ( std::size_t i = 0; i < OUTPUT; ++i )
407 for ( std::size_t j = 0; j < HIDDEN + 1; ++ j )
409 if ( ! is.good() )
return false;
410 is >> M_weight_h_to_o[i][j];
421 std::ostream &
print( std::ostream & os )
const
423 for ( std::size_t i = 0; i < HIDDEN; ++i )
425 std::copy( M_weight_i_to_h[i].begin(),
426 M_weight_i_to_h[i].end(),
427 std::ostream_iterator< value_type >( os,
" " ) );
429 for ( std::size_t i = 0; i < OUTPUT; ++i )
431 std::copy( M_weight_h_to_o[i].begin(),
432 M_weight_h_to_o[i].end(),
433 std::ostream_iterator< value_type >( os,
" " ) );
Back Propagetion Neural Network.
Definition: bpn1.h:154
BPNetwork1(const value_type &eta, const value_type &alpha)
create with learning parameter
Definition: bpn1.h:204
std::ostream & print(std::ostream &os) const
put network structure to stream by "one" line
Definition: bpn1.h:421
std::array< value_type, INPUT > input_array
typedef of the input array type that uses template parameter.
Definition: bpn1.h:159
BPNetwork1()
default constructor
Definition: bpn1.h:192
BPNetwork1(const value_type &eta, const value_type &alpha, RNG &rng)
create with random number generator
Definition: bpn1.h:219
void propagate(const input_array &input, output_array &output) const
simulate network.
Definition: bpn1.h:274
void init()
init member variables
Definition: bpn1.h:232
double value_type
typedef of the value type
Definition: bpn1.h:156
bool read(std::istream &is)
read network structure from input stream.
Definition: bpn1.h:395
std::array< value_type, OUTPUT > output_array
typedef of the output array type that uses template parameter.
Definition: bpn1.h:161
void randomize(RNG &rng)
create unit connection randomly
Definition: bpn1.h:253
value_type train(const input_array &input, const output_array &teacher)
update unit connection weights using teacher signal
Definition: bpn1.h:307
linear function and differenceial function objcet
Definition: bpn1.h:99
double operator()(const double &x) const
functional operator
Definition: bpn1.h:105
double diffAtY(const double &) const
differencial function for y
Definition: bpn1.h:133
double diffAtX(const double &) const
differencial function for x
Definition: bpn1.h:124
double inverse(const double &y) const
inverse function
Definition: bpn1.h:115
sigmoid function and differencial function object
Definition: bpn1.h:49
double diffAtY(const double &y) const
differencial function for y
Definition: bpn1.h:86
double diffAtX(const double &x) const
Definition: bpn1.h:75
double operator()(const double &x) const
functional operator
Definition: bpn1.h:55
double inverse(const double &y) const
inverse function
Definition: bpn1.h:65