openGPMP
Open Source Mathematics Package
mlp_network.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
14  *
15  * This software is licensed as described in the file LICENSE, which
16  * you should have received as part of this distribution. The terms
17  * among other details are referenced in the official documentation
18  * seen here : https://akielaries.github.io/openGPMP/ along with
19  * important files seen in this project.
20  *
21  * You may opt to use, copy, modify, merge, publish, distribute
22  * and/or sell copies of the Software, and permit persons to whom
23  * the Software is furnished to do so, under the terms of the
24  * LICENSE file. As this is an Open Source effort, all implementations
25  * must be of the same methodology.
26  *
27  *
28  *
29  * This software is distributed on an AS IS basis, WITHOUT
30  * WARRANTY OF ANY KIND, either express or implied.
31  *
32  ************************************************************************/
33 
34 /*
35  * Implementation of a Multi-Layered Perceptron Neural Network
36  */
37 #include <math.h>
38 #include <openGPMP/ml/mlp_net.hpp>
39 #include <stdio.h>
40 #include <string.h>
41 #include <time.h>
42 
43 using namespace gpmp::ml;
44 
45 /*
46  * Initialize randomly generated values for network's method
47  */
49  srand(4711);
50  // srand((uint64_t)time(NULL));
51 }
52 
53 /* verify the random is an integer */
54 int64_t gpmp::ml::PrimaryMLP::rand_int(int64_t hi, int64_t low) {
55  return rand() % (hi - low + 1) + low;
56 }
57 
58 /* verify generated random is a real number */
59 long double gpmp::ml::PrimaryMLP::rand_real(long double low, long double hi) {
60  return ((long double)rand() / RAND_MAX) * (hi - low) + low;
61 }
62 
63 /* PRIMARY MLP CONSTRUCTOR */
64 gpmp::ml::PrimaryMLP::PrimaryMLP(int64_t nl, int64_t npl[])
65  : num_layers(0), layer_ptr(0), _MSE(0.0), _MAE(0.0), _Eta(0.25),
66  _Alpha(0.9), _Gain(1.0), _AvgTestError(0.0) {
67  int64_t _LAYER, _NEURON;
68 
69  // create network layers
70  num_layers = nl;
71  layer_ptr = new layer[nl];
72 
73  // intialize the data of the created network layers
74  for (_LAYER = 0; _LAYER < nl; _LAYER++) {
75  // intialize values to neuron struct information
76  layer_ptr[_LAYER].num_neurons = npl[_LAYER];
77  layer_ptr[_LAYER].neuron_ptr = new neuron[npl[_LAYER]];
78 
79  // intialize date of the neurons of the created network layers
80  for (_NEURON = 0; _NEURON < npl[_LAYER]; _NEURON++) {
81  // initialize exit value
82  layer_ptr[_LAYER].neuron_ptr[_NEURON].sortir = 1.0;
83  // save the error
84  layer_ptr[_LAYER].neuron_ptr[_NEURON].err = 0.0;
85 
86  // check if there is at least 1 layer
87  if (_LAYER > 0) {
88  /* initialize weight, last weight, and saved weight
89  * values to _LAYER - 1
90  */
91  layer_ptr[_LAYER].neuron_ptr[_NEURON].wt =
92  new long double[npl[_LAYER - 1]];
93 
94  layer_ptr[_LAYER].neuron_ptr[_NEURON].wt_last =
95  new long double[npl[_LAYER - 1]];
96 
97  layer_ptr[_LAYER].neuron_ptr[_NEURON].wt_saved =
98  new long double[npl[_LAYER - 1]];
99  }
100  // otherwise
101  else {
102  /*
103  * initialize weight, last weight, and saved weight
104  * to NULL
105  */
106  layer_ptr[_LAYER].neuron_ptr[_NEURON].wt = NULL;
107  layer_ptr[_LAYER].neuron_ptr[_NEURON].wt_last = NULL;
108  layer_ptr[_LAYER].neuron_ptr[_NEURON].wt_saved = NULL;
109  }
110  }
111  }
112 }
113 
114 /* PRIMARY MLP DECONSTRUCTOR */
116  int64_t _LAYER, _NEURON;
117 
118  // TODO : thread the loops dealing with rudimentary computations
119 
120  for (_LAYER = 0; _LAYER < num_layers; _LAYER++) {
121  if (layer_ptr[_LAYER].neuron_ptr) {
122  for (_NEURON = 0; _NEURON < layer_ptr[_LAYER].num_neurons;
123  _NEURON++) {
124  if (layer_ptr[_LAYER].neuron_ptr[_NEURON].wt) {
125  delete[] layer_ptr[_LAYER].neuron_ptr[_NEURON].wt;
126  }
127 
128  if (layer_ptr[_LAYER].neuron_ptr[_NEURON].wt_last) {
129  delete[] layer_ptr[_LAYER].neuron_ptr[_NEURON].wt_last;
130  }
131 
132  if (layer_ptr[_LAYER].neuron_ptr[_NEURON].wt_saved) {
133  delete[] layer_ptr[_LAYER].neuron_ptr[_NEURON].wt_saved;
134  }
135  }
136  }
137  delete[] layer_ptr[_LAYER].neuron_ptr;
138  }
139  delete[] layer_ptr;
140 }
PrimaryMLP(int64_t nl, int64_t npl[])
Definition: mlp_network.cpp:64
long double rand_real(long double low, long double hi)
Definition: mlp_network.cpp:59
int64_t rand_int(int64_t low, int64_t hi)
Definition: mlp_network.cpp:54
Namespace for openGPMP Machine Learning.
Definition: activators.hpp:42
int64_t num_neurons
Definition: mlp_net.hpp:80
neuron * neuron_ptr
Definition: mlp_net.hpp:81
long double err
Definition: mlp_net.hpp:70
long double * wt_saved
Definition: mlp_net.hpp:76
long double * wt
Definition: mlp_net.hpp:72
long double sortir
Definition: mlp_net.hpp:68
long double * wt_last
Definition: mlp_net.hpp:74