Belle II Software  release-05-01-25
FANNOptions.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2016 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Thomas Keck and Fernando Abudinen *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <mva/methods/FANN.h>
12 
13 #include <framework/logging/Logger.h>
14 #include <TFormula.h>
15 
16 namespace Belle2 {
21  namespace MVA {
22 
23  void FANNOptions::load(const boost::property_tree::ptree& pt)
24  {
25 
26  int version = pt.get<int>("FANN_version");
27  if (version != 1) {
28  B2ERROR("Unkown weightfile version " << std::to_string(version));
29  throw std::runtime_error("Unkown weightfile version " + std::to_string(version));
30  }
31  m_max_epochs = pt.get<unsigned int>("FANN_max_epochs");
32  m_verbose_mode = pt.get<bool>("FANN_verbose_mode");
33 
34  m_hidden_layers_architecture = pt.get<std::string>("FANN_hidden_layers_architecture");
35 
36  m_hidden_activiation_function = pt.get<std::string>("FANN_hidden_activation_function");
37  m_output_activiation_function = pt.get<std::string>("FANN_output_activation_function");
38  m_error_function = pt.get<std::string>("FANN_error_function");
39  m_training_method = pt.get<std::string>("FANN_training_method");
40  m_validation_fraction = pt.get<double>("FANN_validation_fraction");
41  m_random_seeds = pt.get<unsigned int>("FANN_random_seeds");
42  m_test_rate = pt.get<unsigned int>("FANN_test_rate");
43  m_number_of_threads = pt.get<unsigned int>("FANN_number_of_threads");
44 
45  m_scale_features = pt.get<bool>("FANN_scale_features");
46  m_scale_target = pt.get<bool>("FANN_scale_target");
47 
48  }
49 
50  void FANNOptions::save(boost::property_tree::ptree& pt) const
51  {
52  pt.put("FANN_version", 1);
53  pt.put("FANN_max_epochs", m_max_epochs);
54  pt.put("FANN_verbose_mode", m_verbose_mode);
55  pt.put("FANN_hidden_layers_architecture", m_hidden_layers_architecture);
56  pt.put("FANN_hidden_activation_function", m_hidden_activiation_function);
57  pt.put("FANN_output_activation_function", m_output_activiation_function);
58  pt.put("FANN_error_function", m_error_function);
59  pt.put("FANN_training_method", m_training_method);
60  pt.put("FANN_validation_fraction", m_validation_fraction);
61  pt.put("FANN_random_seeds", m_random_seeds);
62  pt.put("FANN_test_rate", m_test_rate);
63  pt.put("FANN_number_of_threads", m_number_of_threads);
64 
65  pt.put("FANN_scale_features", m_scale_features);
66  pt.put("FANN_scale_target", m_scale_target);
67 
68 
69  }
70 
71  po::options_description FANNOptions::getDescription()
72  {
73  po::options_description description("FANN options");
74  description.add_options()
75  ("max_epochs", po::value<unsigned int>(&m_max_epochs), "Number of iEpochs")
76  ("verbose_mode", po::value<bool>(&m_verbose_mode), "Prints out the training status or not")
77  ("hidden_layers_architecture", po::value<std::string>(&m_hidden_layers_architecture),
78  "Architecture with number of neurons in each hidden layer")
79  ("hidden_activiation_function", po::value<std::string>(&m_hidden_activiation_function),
80  "Name of acitvation function used for hidden layers")
81  ("output_activiation_function", po::value<std::string>(&m_output_activiation_function),
82  "Name of acitvation function used for output layer")
83  ("error_function", po::value<std::string>(&m_error_function), "Name of error function")
84  ("training_method", po::value<std::string>(&m_training_method), "Method used for backpropagation")
85  ("validation_fraction", po::value<double>(&m_validation_fraction), "Fraction of training sample used for validation.")
86  ("random_seeds", po::value<unsigned int>(&m_random_seeds),
87  "Number of times the training is repeated with a new weight random seed.")
88  ("test_rate", po::value<unsigned int>(&m_test_rate), "Rate of iEpochs to check the validation error")
89  ("number_of_threads", po::value<unsigned int>(&m_number_of_threads), "Number of threads for parallel training")
90  ("scale_features", po::value<bool>(&m_scale_features), "Boolean indicating if features should be scaled or not")
91  ("scale_target", po::value<bool>(&m_scale_target), "Boolean indicating if target should be scaled or not");
92  return description;
93  }
94 
95  std::vector<unsigned int> FANNOptions::getHiddenLayerNeurons(unsigned int nf) const
96  {
97  std::vector<unsigned int> hiddenLayers;
98  std::stringstream iLayers(m_hidden_layers_architecture);
99  std::string layer;
100  while (std::getline(iLayers, layer, ',')) {
101  for (auto& character : layer) {
102  if (character == 'N') character = 'x';
103  }
104  auto* iLayerSize = new TFormula("iLayerSize", layer.c_str());
105  hiddenLayers.push_back(iLayerSize->Eval(nf));
106  }
107  return hiddenLayers;
108  }
109  }
111 }
Belle2::MVA::FANNOptions::m_random_seeds
unsigned int m_random_seeds
Number of times the training is repeated with a new weight random seed.
Definition: FANN.h:72
Belle2::MVA::FANNOptions::m_training_method
std::string m_training_method
Training method for back propagation.
Definition: FANN.h:69
Belle2::MVA::FANNOptions::save
virtual void save(boost::property_tree::ptree &pt) const override
Save mechanism to store Options in a xml tree.
Definition: FANNOptions.cc:58
Belle2::MVA::FANNOptions::m_hidden_activiation_function
std::string m_hidden_activiation_function
Activation function in hidden layer.
Definition: FANN.h:66
Belle2::MVA::FANNOptions::m_validation_fraction
double m_validation_fraction
Fraction of training sample used for validation in order to avoid overtraining.
Definition: FANN.h:71
Belle2::MVA::FANNOptions::load
virtual void load(const boost::property_tree::ptree &pt) override
Load mechanism to load Options from a xml tree.
Definition: FANNOptions.cc:31
Belle2::MVA::FANNOptions::m_test_rate
unsigned int m_test_rate
Error on validation is compared with the one before.
Definition: FANN.h:74
Belle2::MVA::FANNOptions::m_verbose_mode
bool m_verbose_mode
Sets to report training status or not.
Definition: FANN.h:63
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::MVA::FANNOptions::m_number_of_threads
unsigned int m_number_of_threads
Number of threads for parallel training.
Definition: FANN.h:76
Belle2::MVA::FANNOptions::m_output_activiation_function
std::string m_output_activiation_function
Activation function in output layer.
Definition: FANN.h:67
Belle2::MVA::FANNOptions::m_hidden_layers_architecture
std::string m_hidden_layers_architecture
String containing the architecture of hidden neurons.
Definition: FANN.h:64
Belle2::MVA::FANNOptions::m_scale_features
bool m_scale_features
Scale features before training.
Definition: FANN.h:79
Belle2::MVA::FANNOptions::m_scale_target
bool m_scale_target
Scale target before training.
Definition: FANN.h:80
Belle2::MVA::FANNOptions::m_max_epochs
unsigned int m_max_epochs
Maximum number of epochs.
Definition: FANN.h:62
Belle2::MVA::FANNOptions::m_error_function
std::string m_error_function
Loss function.
Definition: FANN.h:68
Belle2::MVA::FANNOptions::getDescription
virtual po::options_description getDescription() override
Returns a program options description for all available options.
Definition: FANNOptions.cc:79
Belle2::MVA::FANNOptions::getHiddenLayerNeurons
std::vector< unsigned int > getHiddenLayerNeurons(unsigned int nf) const
Returns the internal vector parameter with the number of hidden neurons per layer.
Definition: FANNOptions.cc:103