Newer
Older
//
// Created by martin on 7/15/18.
//
#ifndef INC_4NEURO_ERRORFUNCTION_H
#define INC_4NEURO_ERRORFUNCTION_H
kra568
committed
#include "../settings.h"
#include "../Network/NeuralNetwork.h"
#include "../DataSet/DataSet.h"
Martin Beseda
committed
Martin Beseda
committed
//TODO HEAVY refactoring needed!
Martin Beseda
committed
namespace lib4neuro {
enum ErrorFunctionType {
ErrorFuncMSE
};
class ErrorFunction {
public:
/**
*
* @param weights
* @return
*/
Martin Beseda
committed
virtual double eval(std::vector<double>* weights = nullptr) = 0;
Martin Beseda
committed
/**
*
* @return
*/
LIB4NEURO_API virtual size_t get_dimension();
/**
*
* @param params
* @param grad
Martin Beseda
committed
* @param alpha
* @param batch
Martin Beseda
committed
calculate_error_gradient(std::vector<double>& params,
std::vector<double>& grad,
Martin Beseda
committed
double alpha = 1.0,
size_t batch = 0) = 0;
Martin Beseda
committed
virtual std::vector<double>* get_parameters();
Martin Beseda
committed
virtual DataSet* get_dataset();
/**
*
* @return
*/
virtual DataSet* get_test_dataset();
/**
*
* @return
*/
NeuralNetwork* get_network_instance();
/**
*
* @param percent_train
* @return
*/
void divide_data_train_test(double percent_test);
/**
*
*/
void return_full_data_set_for_training();
/**
*
*/
virtual double eval_on_test_data(std::vector<double>* weights = nullptr) = 0;
Martin Beseda
committed
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
/**
*
* @param results_file_path
* @param weights
* @return
*/
virtual double eval_on_test_data(std::string results_file_path, std::vector<double>* weights = nullptr) = 0;
/**
*
* @param results_file_path
* @param weights
* @return
*/
virtual double eval_on_test_data(std::ofstream* results_file_path, std::vector<double>* weights = nullptr) = 0;
/**
*
* @param data_set
* @param weights
* @return
*/
virtual double eval_on_data_set(DataSet* data_set, std::vector<double>* weights = nullptr) = 0;
/**
*
* @param data_set
* @param weights
* @param results_file_path
* @return
*/
virtual double
eval_on_data_set(DataSet* data_set, std::string results_file_path, std::vector<double>* weights = nullptr) = 0;
/**
*
* @param data_set
* @param results_file_path
* @param weights
* @return
*/
virtual double eval_on_data_set(DataSet* data_set, std::ofstream* results_file_path,
std::vector<double>* weights = nullptr) = 0;
Martin Beseda
committed
protected:
/**
*
*/
size_t dimension = 0;
NeuralNetwork* net = nullptr;
/**
*
*/
DataSet* ds = nullptr;
/**
*
*/
DataSet* ds_full = nullptr;
/**
*
*/
DataSet* ds_test = nullptr;
Martin Beseda
committed
};
Martin Beseda
committed
Martin Beseda
committed
class MSE : public ErrorFunction {
public:
/**
* Constructor for single neural network
* @param net
* @param ds
*/
Martin Beseda
committed
LIB4NEURO_API MSE(NeuralNetwork* net, DataSet* ds);
Martin Beseda
committed
/**
*
* @param weights
* @return
*/
Martin Beseda
committed
LIB4NEURO_API double eval(std::vector<double>* weights = nullptr) override;
/**
*
* @param params
* @param grad
Martin Beseda
committed
* @param alpha
* @param batch
Martin Beseda
committed
calculate_error_gradient(std::vector<double>& params,
std::vector<double>& grad,
Martin Beseda
committed
double alpha = 1.0,
size_t batch = 0) override;
Martin Beseda
committed
* @param weights
Martin Beseda
committed
LIB4NEURO_API double eval_on_test_data(std::vector<double>* weights = nullptr) override;
Martin Beseda
committed
/**
*
* @param results_file_path
* @param weights
* @return
*/
LIB4NEURO_API double eval_on_test_data(std::string results_file_path, std::vector<double>* weights = nullptr);
Martin Beseda
committed
Martin Beseda
committed
/**
*
* @param results_file_path
* @param weights
* @return
*/
LIB4NEURO_API double eval_on_test_data(std::ofstream* results_file_path, std::vector<double>* weights = nullptr) override;
Martin Beseda
committed
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
/**
*
* @param data_set
* @param results_file_path
* @param weights
* @return
*/
LIB4NEURO_API double eval_on_data_set(DataSet* data_set,
std::ofstream* results_file_path,
std::vector<double>* weights = nullptr) override;
/**
*
* @param data_set
* @param weights
* @return
*/
LIB4NEURO_API double eval_on_data_set(DataSet* data_set, std::vector<double>* weights = nullptr) override;
/**
*
* @param data_set
* @param results_file_path
* @param weights
* @return
*/
LIB4NEURO_API double eval_on_data_set(DataSet* data_set, std::string results_file_path,
std::vector<double>* weights = nullptr) override;
Martin Beseda
committed
};
class ErrorSum : public ErrorFunction {
public:
/**
*
*/
LIB4NEURO_API ErrorSum();
/**
*
*/
LIB4NEURO_API ~ErrorSum();
/**
*
* @param weights
* @return
*/
Martin Beseda
committed
LIB4NEURO_API double eval(std::vector<double>* weights = nullptr) override;
/**
*
* @param weights
* @return
*/
LIB4NEURO_API double eval_on_test_data(std::vector<double>* weights = nullptr) override;
/**
*
* @param results_file_path
* @param weights
* @return
*/
LIB4NEURO_API double eval_on_test_data(std::string results_file_path, std::vector<double>* weights = nullptr);
Martin Beseda
committed
Martin Beseda
committed
* @param results_file_path
* @param weights
* @return
*/
Martin Beseda
committed
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
LIB4NEURO_API double eval_on_test_data(std::ofstream* results_file_path, std::vector<double>* weights = nullptr) override;
/**
*
* @param data_set
* @param weights
* @return
*/
LIB4NEURO_API double eval_on_data_set(DataSet* data_set, std::vector<double>* weights = nullptr) override;
/**
*
* @param data_set
* @param results_file_path
* @param weights
* @return
*/
LIB4NEURO_API double eval_on_data_set(DataSet* data_set, std::string results_file_path,
std::vector<double>* weights = nullptr) override;
/**
*
* @param data_set
* @param results_file_path
* @param weights
* @return
*/
LIB4NEURO_API double eval_on_data_set(DataSet* data_set,
std::ofstream* results_file_path,
std::vector<double>* weights = nullptr) override;
Martin Beseda
committed
/**
*
* @param F
*/
Martin Beseda
committed
LIB4NEURO_API void add_error_function(ErrorFunction* F, double alpha = 1.0);
Martin Beseda
committed
/**
*
* @return
*/
LIB4NEURO_API size_t get_dimension() override;
/**
*
* @param params
* @param grad
Martin Beseda
committed
* @param alpha
* @param batch
Martin Beseda
committed
calculate_error_gradient(std::vector<double>& params,
std::vector<double>& grad,
Martin Beseda
committed
double alpha = 1.0,
size_t batch = 0) override;
Martin Beseda
committed
LIB4NEURO_API std::vector<double>* get_parameters() override;
Martin Beseda
committed
/**
*
* @return
*/
LIB4NEURO_API DataSet* get_dataset() override;
Martin Beseda
committed
private:
Martin Beseda
committed
std::vector<ErrorFunction*>* summand;
std::vector<double>* summand_coefficient;
Martin Beseda
committed
};
}
#endif //INC_4NEURO_ERRORFUNCTION_H