Change Build Framework & add application package
[platform/core/ml/nntrainer.git] / nntrainer / include / neuralnet.h
1 /**
2  * Copyright (C) 2019 Samsung Electronics Co., Ltd. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *   http://www.apache.org/licenses/LICENSE-2.0
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  * See the License for the specific language governing permissions and
12  * limitations under the License.
13  *
14  *
15  * @file        neuralnet.h
16  * @date        04 December 2019
17  * @brief       This is Neural Network Class
18  * @see         https://github.com/nnstreamer/nntrainer
19  * @author      Jijoong Moon <jijoong.moon@samsung.com>
20  * @bug         No known bugs except for NYI items
21  *
22  */
23 #ifndef __NEURALNET_H__
24 #define __NEURALNET_H__
25
26 #include <fstream>
27 #include <iostream>
28 #include <vector>
29 #include "layers.h"
30 #include "tensor.h"
31
32 /**
33  * @Namespace   Namespace of Network
34  * @brief       Namespace for Network
35  */
36 namespace Network {
37
38 /**
39  * @brief     Enumeration of Network Type
40  *            0. KNN ( k Nearest Neighbor )
41  *            1. REG ( Logistic Regression )
42  *            2. NEU ( Neural Network )
43  *            3. Unknown
44  */
45 typedef enum { NET_KNN, NET_REG, NET_NEU, NET_UNKNOWN } net_type;
46
47 /**
48  * @brief     Enumeration for input configuration file parsing
49  *            0. OPT     ( Optimizer Token )
50  *            1. COST    ( Cost Function Token )
51  *            2. NET     ( Network Token )
52  *            3. ACTI    ( Activation Token )
53  *            4. LAYER   ( Layer Token )
54  *            5. WEIGHTINI  ( Weight Initialization Token )
55  *            7. WEIGHT_DECAY  ( Weight Decay Token )
56  *            8. UNKNOWN
57  */
58 typedef enum {
59   TOKEN_OPT,
60   TOKEN_COST,
61   TOKEN_NET,
62   TOKEN_ACTI,
63   TOKEN_LAYER,
64   TOKEN_WEIGHTINI,
65   TOKEN_WEIGHT_DECAY,
66   TOKEN_UNKNOWN
67 } input_type;
68
69 /**
70  * @class   NeuralNetwork Class
71  * @brief   NeuralNetwork Class which has Network Configuration & Layers
72  */
73 class NeuralNetwork {
74  public:
75   /**
76    * @brief     Constructor of NeuralNetwork Class
77    */
78   NeuralNetwork(){};
79
80   /**
81    * @brief     Constructor of NeuralNetwork Class with Configuration file path
82    */
83   NeuralNetwork(std::string config_path);
84
85   /**
86    * @brief     Destructor of NeuralNetwork Class
87    */
88   ~NeuralNetwork(){};
89
90   /**
91     * @brief     Get Loss
92     * @retval    loss value
93     */
94   float getLoss();
95
96   /**
97     * @brief     Get Optimizer
98     * @retval    Optimizer
99     */
100   Layers::Optimizer getOptimizer() { return opt; };
101
102   /**
103     * @brief     Get Learing rate
104     * @retval    Learning rate
105     */
106   float getLearningRate() { return learning_rate; };
107
108   /**
109    * @brief     Set Loss
110    * @param[in] l loss value
111    */
112   void setLoss(float l);
113
114   /**
115    * @brief     Initialize Network
116    */
117   void init();
118
119   /**
120    * @brief     forward propagation
121    * @param[in] input Input Tensor X
122    * @retval    Output Tensor Y
123    */
124   Tensors::Tensor forwarding(Tensors::Tensor input);
125
126   /**
127    * @brief     forward propagation
128    * @param[in] input Input Tensor X
129    * @param[in] label Input Tensor Y2
130    * @retval    Output Tensor Y
131    */
132   Tensors::Tensor forwarding(Tensors::Tensor input, Tensors::Tensor output);
133
134   /**
135    * @brief     back propagation to update W & B
136    * @param[in] input Input Tensor X
137    * @param[in] expectedOutput Lable Tensor Y
138    * @param[in] iteration Epoch Number for ADAM
139    */
140   void backwarding(Tensors::Tensor input, Tensors::Tensor expectedOutput, int iteration);
141
142   /**
143    * @brief     save W & B into file
144    */
145   void saveModel();
146
147   /**
148    * @brief     read W & B from file
149    */
150   void readModel();
151
152   /**
153    * @brief     set configuration file
154    * @param[in] config_path configuration file path
155    */
156   void setConfig(std::string config_path);
157
158   /**
159    * @brief     get Epoch
160    * @retval    epoch
161    */
162   unsigned int getEpoch() { return epoch; };
163
164   /**
165    * @brief     Copy Neural Network
166    * @param[in] from NeuralNetwork Object to copy
167    * @retval    NeuralNewtork Object copyed
168    */
169   NeuralNetwork &copy(NeuralNetwork &from);
170
171   /**
172    * @brief     finalize NeuralNetwork Object
173    */
174   void finalize();
175
176  private:
177   /**
178    * @brief     batch size
179    */
180   int batchsize;
181
182   /**
183    * @brief     function pointer for activation
184    */
185   float (*activation)(float);
186
187   /**
188    * @brief     function pointer for derivative of activation
189    */
190   float (*activationPrime)(float);
191
192   /**
193    * @brief     learning rate
194    */
195   float learning_rate;
196
197   /**
198    * @brief     decay_rate for decayed learning rate
199    */
200   float decay_rate;
201
202   /**
203    * @brief     decay_step for decayed learning rate
204    */
205   float decay_steps;
206
207   /**
208    * @brief     Maximum Epoch
209    */
210   unsigned int epoch;
211
212   /**
213    * @brief     loss
214    */
215   float loss;
216
217   /**
218    * @brief     boolean to set the Bias zero
219    */
220   bool init_zero;
221
222   /**
223    * @brief     Cost Function type
224    */
225   Layers::cost_type cost;
226
227   /**
228    * @brief     Weight Initialization type
229    */
230   Layers::weightIni_type weightini;
231
232   /**
233    * @brief     Model path to save or read
234    */
235   std::string model;
236
237   /**
238    * @brief     Configuration file path
239    */
240   std::string config;
241
242   /**
243    * @brief     Optimizer
244    */
245   Layers::Optimizer opt;
246
247   /**
248    * @brief     Network Type
249    */
250   net_type nettype;
251
252   /**
253    * @brief     vector for store layer pointers.
254    */
255   std::vector<Layers::Layer *> layers;
256 };
257 }
258
259 #endif