9#include <mva/methods/FANN.h>
11#include <framework/logging/Logger.h>
14#include <parallel_fann.hpp>
27 m_specific_options(specific_options) { }
33 unsigned int numberOfFeatures = training_data.getNumberOfFeatures();
34 unsigned int numberOfEvents = training_data.getNumberOfEvents();
37 unsigned int number_of_layers = 2;
38 for (
unsigned int hiddenLayer : hiddenLayers) {
39 if (hiddenLayer > 0) {
44 auto layers = std::unique_ptr<unsigned int[]>(
new unsigned int[number_of_layers]);
45 layers[0] = numberOfFeatures;
46 for (
unsigned int i = 0; i < hiddenLayers.size(); ++i) {
47 if (hiddenLayers[i] > 0) {
48 layers[i + 1] = hiddenLayers[i];
51 layers[number_of_layers - 1] = 1;
53 struct fann* ann = fann_create_standard_array(number_of_layers, layers.get());
55 std::map<std::string, enum fann_activationfunc_enum> activationFunctions;
57 for (
auto& name : FANN_ACTIVATIONFUNC_NAMES) {
58 activationFunctions[name] = fann_activationfunc_enum(i);
63 typedef float (*FnPtr)(
struct fann * ann,
struct fann_train_data * data,
const unsigned int threadnumb);
64 std::map<std::string, FnPtr> trainingMethods;
65 trainingMethods[
"FANN_TRAIN_RPROP"] = parallel_fann::train_epoch_irpropm_parallel;
66 trainingMethods[
"FANN_TRAIN_BATCH"] = parallel_fann::train_epoch_batch_parallel;
67 trainingMethods[
"FANN_TRAIN_QUICKPROP"] = parallel_fann::train_epoch_quickprop_parallel;
68 trainingMethods[
"FANN_TRAIN_SARPROP"] = parallel_fann::train_epoch_sarprop_parallel;
69 trainingMethods[
"FANN_TRAIN_INCREMENTAL"] =
nullptr;
71 std::map<std::string, enum fann_train_enum> trainingMethods;
73 for (
auto& name : FANN_TRAIN_NAMES) {
74 trainingMethods[name] = fann_train_enum(i);
79 std::map<std::string, enum fann_errorfunc_enum> errorFunctions;
81 for (
auto& name : FANN_ERRORFUNC_NAMES) {
82 errorFunctions[name] = fann_errorfunc_enum(i);
108 throw std::runtime_error(
"m_max_epochs should be larger than 0. The given value is " + std::to_string(
114 throw std::runtime_error(
"m_random_seeds should be larger than 0. The given value is " + std::to_string(
120 throw std::runtime_error(
"m_test_rate should be larger than 0. The given value is " + std::to_string(
126 throw std::runtime_error(
"m_number_of_threads should be larger than 0. The given value is " +
137 unsigned int nTestingEvents = int(nTestingAndValidationEvents * 0.5);
138 unsigned int nValidationEvents = int(nTestingAndValidationEvents * 0.5);
139 unsigned int nTrainingEvents = numberOfEvents - nValidationEvents - nTestingEvents;
141 if (nTestingAndValidationEvents < 1) {
142 B2ERROR(
"m_validation_fraction should be a number between 0 and 1 (0 < x < 1). The given value is " <<
144 ". The total number of events is " << numberOfEvents <<
". numberOfEvents * m_validation_fraction has to be larger than one");
145 throw std::runtime_error(
"m_validation_fraction should be a number between 0 and 1 (0 < x < 1). numberOfEvents * m_validation_fraction has to be larger than one");
148 if (nTrainingEvents < 1) {
149 B2ERROR(
"m_validation_fraction should be a number between 0 and 1 (0 < x < 1). The given value is " <<
151 ". The total number of events is " << numberOfEvents <<
". numberOfEvents * (1 - m_validation_fraction) has to be larger than one");
152 throw std::runtime_error(
"m_validation_fraction should be a number between 0 and 1 (0 < x < 1). numberOfEvents * (1 - m_validation_fraction) has to be larger than one");
156 struct fann_train_data* train_data =
157 fann_create_train(nTrainingEvents, numberOfFeatures, 1);
158 for (
unsigned iEvent = 0; iEvent < nTrainingEvents; ++iEvent) {
159 training_data.loadEvent(iEvent);
160 for (
unsigned iFeature = 0; iFeature < numberOfFeatures; ++iFeature) {
161 train_data->input[iEvent][iFeature] = training_data.m_input[iFeature];
163 train_data->output[iEvent][0] = training_data.m_target;
166 struct fann_train_data* valid_data =
167 fann_create_train(nValidationEvents, numberOfFeatures, 1);
168 for (
unsigned iEvent = nTrainingEvents; iEvent < nTrainingEvents + nValidationEvents; ++iEvent) {
169 training_data.loadEvent(iEvent);
170 for (
unsigned iFeature = 0; iFeature < numberOfFeatures; ++iFeature) {
171 valid_data->input[iEvent - nTrainingEvents][iFeature] = training_data.m_input[iFeature];
173 valid_data->output[iEvent - nTrainingEvents][0] = training_data.m_target;
177 struct fann_train_data* test_data =
178 fann_create_train(nTestingEvents, numberOfFeatures, 1);
179 for (
unsigned iEvent = nTrainingEvents + nValidationEvents; iEvent < numberOfEvents; ++iEvent) {
180 training_data.loadEvent(iEvent);
181 for (
unsigned iFeature = 0; iFeature < numberOfFeatures; ++iFeature) {
182 test_data->input[iEvent - nTrainingEvents - nValidationEvents][iFeature] = training_data.m_input[iFeature];
184 test_data->output[iEvent - nTrainingEvents - nValidationEvents][0] = training_data.m_target;
187 struct fann_train_data* data = fann_create_train(numberOfEvents, numberOfFeatures, 1);
188 for (
unsigned int iEvent = 0; iEvent < numberOfEvents; ++iEvent) {
189 training_data.loadEvent(iEvent);
190 for (
unsigned int iFeature = 0; iFeature < numberOfFeatures; ++iFeature) {
191 data->input[iEvent][iFeature] = training_data.m_input[iFeature];
193 data->output[iEvent][0] = training_data.m_target;
197 fann_set_input_scaling_params(ann, data, -1.0, 1.0);
201 fann_set_output_scaling_params(ann, data, -1.0, 1.0);
205 fann_scale_train(ann, data);
206 fann_scale_train(ann, train_data);
207 fann_scale_train(ann, valid_data);
208 fann_scale_train(ann, test_data);
211 struct fann* bestANN =
nullptr;
212 double bestRMS = 999.;
213 std::vector<double> bestTrainLog = {};
214 std::vector<double> bestValidLog = {};
218 double bestValid = 999.;
219 std::vector<double> trainLog = {};
220 std::vector<double> validLog = {};
224 struct fann* iRunANN =
nullptr;
225 fann_randomize_weights(ann, -0.1, 0.1);
231 }
else {mse = parallel_fann::train_epoch_incremental_mod(ann, train_data);}
234 mse = fann_train_epoch(ann, train_data);
236 trainLog[iEpoch - 1] = mse;
243 double valid_mse = fann_test_data(ann, valid_data);
246 validLog[iEpoch - 1] = valid_mse;
248 if (valid_mse < bestValid) {
249 bestValid = valid_mse;
250 iRunANN = fann_copy(ann);
255 B2INFO(
"Training stopped in iEpoch " << iEpoch);
256 B2INFO(
"Train error: " << mse <<
", valid error: " << valid_mse <<
257 ", best valid: " << bestValid);
263 if (iEpoch == 1 || (iEpoch < 100 && iEpoch % 10 == 0) || iEpoch % 100 == 0) {
265 ", valid error = " << valid_mse <<
", best valid = " << bestValid);
274 double test_mse = fann_test_data(iRunANN, test_data);
277 double RMS = sqrt(test_mse);
281 bestANN = fann_copy(iRunANN);
282 fann_destroy(iRunANN);
283 bestTrainLog.assign(trainLog.begin(), trainLog.begin() + breakEpoch);
284 bestValidLog.assign(validLog.begin(), validLog.begin() + breakEpoch);
289 fann_destroy_train(data);
290 fann_destroy_train(train_data);
291 fann_destroy_train(valid_data);
292 fann_destroy_train(test_data);
298 fann_save(bestANN, custom_weightfile.c_str());
299 fann_destroy(bestANN);
303 weightfile.
addFile(
"FANN_Weightfile", custom_weightfile);
304 weightfile.
addVector(
"FANN_bestTrainLog", bestTrainLog);
305 weightfile.
addVector(
"FANN_bestValidLog", bestValidLog);
323 weightfile.
getFile(
"FANN_Weightfile", custom_weightfile);
328 m_ann = fann_create_from_file(custom_weightfile.c_str());
336 std::vector<fann_type> input(test_data.getNumberOfFeatures());
337 std::vector<float> probabilities(test_data.getNumberOfEvents());
338 for (
unsigned int iEvent = 0; iEvent < test_data.getNumberOfEvents(); ++iEvent) {
339 test_data.loadEvent(iEvent);
340 for (
unsigned int iFeature = 0; iFeature < test_data.getNumberOfFeatures(); ++iFeature) {
341 input[iFeature] = test_data.m_input[iFeature];
344 probabilities[iEvent] = fann_run(
m_ann, input.data())[0];
347 return probabilities;
Abstract base class of all Datasets given to the MVA interface The current event can always be access...
virtual ~FANNExpert()
Destructor of FANN Expert.
struct fann * m_ann
Pointer to FANN expert.
virtual std::vector< float > apply(Dataset &test_data) const override
Apply this expert onto a dataset.
virtual void load(Weightfile &weightfile) override
Load the expert from a Weightfile.
FANNOptions m_specific_options
Method specific options.
Options for the FANN MVA method.
double m_validation_fraction
Fraction of training sample used for validation in order to avoid overtraining.
bool m_scale_features
Scale features before training.
bool m_verbose_mode
Sets to report training status or not.
unsigned int m_random_seeds
Number of times the training is repeated with a new weight random seed.
std::string m_error_function
Loss function.
unsigned int m_number_of_threads
Number of threads for parallel training.
unsigned int m_test_rate
Error on validation is compared with the one before.
std::string m_hidden_activiation_function
Activation function in hidden layer.
bool m_scale_target
Scale target before training.
std::vector< unsigned int > getHiddenLayerNeurons(unsigned int nf) const
Returns the internal vector parameter with the number of hidden neurons per layer.
std::string m_training_method
Training method for back propagation.
std::string m_output_activiation_function
Activation function in output layer.
unsigned int m_max_epochs
Maximum number of epochs.
FANNTeacher(const GeneralOptions &general_options, const FANNOptions &specific_options)
Constructs a new teacher using the GeneralOptions and specific options of this training.
FANNOptions m_specific_options
Method specific options.
virtual Weightfile train(Dataset &training_data) const override
Train a mva method using the given dataset returning a Weightfile.
General options which are shared by all MVA trainings.
Abstract base class of all Teachers Each MVA library has its own implementation of this class,...
GeneralOptions m_general_options
GeneralOptions containing all shared options.
The Weightfile class serializes all information about a training into an xml tree.
void addFile(const std::string &identifier, const std::string &custom_weightfile)
Add a file (mostly a weightfile from a MVA library) to our Weightfile.
void addOptions(const Options &options)
Add an Option object to the xml tree.
void getOptions(Options &options) const
Fills an Option object from the xml tree.
void addSignalFraction(float signal_fraction)
Saves the signal fraction in the xml tree.
void addVector(const std::string &identifier, const std::vector< T > &vector)
Add a vector to the xml tree.
std::string generateFileName(const std::string &suffix="")
Returns a temporary filename with the given suffix.
void getFile(const std::string &identifier, const std::string &custom_weightfile)
Creates a file from our weightfile (mostly this will be a weightfile of an MVA library)
Abstract base class for different kinds of events.