 |
Belle II Software
release-05-02-19
|
12 #include <mva/interface/Dataset.h>
13 #include <mva/interface/Expert.h>
14 #include <mva/interface/Options.h>
15 #include <mva/interface/Teacher.h>
16 #include <mva/interface/Weightfile.h>
18 #include <framework/logging/Logger.h>
20 #include <boost/property_tree/ptree_fwd.hpp>
21 #include <boost/program_options.hpp>
35 template<
class BaseClassifierOptions>
36 class RegressionOptions :
public SpecificOptions {
39 void load(
const boost::property_tree::ptree& pt)
override;
42 void save(boost::property_tree::ptree& pt)
const override;
79 class RegressionDataSet :
public Dataset {
94 void loadEvent(
unsigned int iEvent)
override;
97 std::vector<float>
getFeature(
unsigned int iFeature)
override;
100 std::vector<float>
getSpectator(
unsigned int iSpectator)
override;
140 template<
class BaseClassifierTeacher,
class RegressionClassifierOptions>
141 class RegressionTeacher :
public Teacher {
145 const RegressionClassifierOptions& specific_options);
157 Weightfile
train(Dataset& training_data)
const override;
180 template<
class BaseClassifierExpert,
class RegressionClassifierOptions>
181 class RegressionExpert :
public Expert {
184 void load(Weightfile& weightfile)
override;
187 std::vector<float>
apply(Dataset& test_data)
const override;
198 template<
class BaseClassifierOptions>
201 m_numberOfBins = pt.get<
unsigned int>(getMethod() +
"_numberOfBins");
204 template<
class BaseClassifierOptions>
207 return m_numberOfBins;
210 template<
class BaseClassifierOptions>
213 m_numberOfBins = maximalBinNumber;
216 template<
class BaseClassifierOptions>
219 return m_baseClassifierOptions;
222 template<
class BaseClassifierOptions>
225 return m_baseClassifierOptions;
228 template<
class BaseClassifierOptions>
231 pt.put(getMethod() +
"_numberOfBins", m_numberOfBins);
234 template<
class BaseClassifierOptions>
237 auto description = getBaseClassifierOptions().getDescription();
239 description.add_options()
240 (
"numberOfBins", po::value<unsigned int>(&m_numberOfBins),
241 "Number of bins to split the target variable into");
246 template<
class BaseClassifierTeacher,
class RegressionClassifierOptions>
254 for (
unsigned int binNumber = 1; binNumber < m_specific_options.getMaximalBinNumber(); binNumber++) {
256 1.0 / m_specific_options.getMaximalBinNumber() * binNumber);
257 Weightfile specificWeightFile = m_baseClassifierTeacher.train(specificDataSet);
260 Weightfile::saveToXMLFile(specificWeightFile, file);
261 weightfile.
addFile(
"BaseClassifier_WeightFile_" + std::to_string(binNumber), file);
266 template<
class BaseClassifierTeacher,
class RegressionClassifierOptions>
269 const RegressionClassifierOptions& specific_options) :
270 Teacher(general_options), m_specific_options(specific_options),
271 m_baseClassifierTeacher(general_options, specific_options.getBaseClassifierOptions())
275 template<
class BaseClassifierExpert,
class RegressionClassifierOptions>
278 RegressionClassifierOptions specific_options;
281 m_baseClassifierExperts.resize(specific_options.getMaximalBinNumber());
282 for (
unsigned int binNumber = 1; binNumber < specific_options.getMaximalBinNumber(); binNumber++) {
284 weightfile.
getFile(
"BaseClassifier_WeightFile_" + std::to_string(binNumber), file);
287 m_baseClassifierExperts[binNumber].load(specificWeightFile);
291 template<
class BaseClassifierExpert,
class RegressionClassifierOptions>
295 std::vector<float> sum;
296 for (
const auto& expert : m_baseClassifierExperts) {
299 sum = expert.apply(test_data);
303 const auto& expertResult = expert.apply(test_data);
304 if (sum.size() != expertResult.size()) {
305 B2FATAL(
"There is one expert in the list that returned not the same number of results than the others!");
308 for (
unsigned int index = 0; index < sum.size(); index++) {
309 sum[index] += expertResult[index];
313 for (
unsigned int index = 0; index < sum.size(); index++) {
314 sum[index] /= m_baseClassifierExperts.size();
void setMaximalBinNumber(unsigned int maximalBinNumber)
Set the number of bins to use.
Weightfile train(Dataset &training_data) const override
Call the train function.
std::vector< float > getSpectator(unsigned int iSpectator) override
Return a specific spectator from the real dataset.
Dataset needed during the training of a regression method.
Abstract base class of all Datasets given to the MVA interface The current event can always be access...
void getOptions(Options &options) const
Fills an Option object from the xml tree.
RegressionDataSet(const GeneralOptions &general_options, Dataset *dataSet, double cutValue)
Create a new regression data set out of the general options, a pointer to the real dataset and the cu...
std::vector< BaseClassifierExpert > m_baseClassifierExperts
The list of single experts.
void addFile(const std::string &identifier, const std::string &custom_weightfile)
Add a file (mostly a weightfile from a MVA library) to our Weightfile.
The Weightfile class serializes all information about a training into an xml tree.
BaseClassifierOptions m_baseClassifierOptions
Options of the base classifier.
BaseClassifierTeacher m_baseClassifierTeacher
The teacher to use for training.
std::vector< float > getWeights() override
Return the weights from the real dataset.
unsigned int getNumberOfFeatures() const override
Return the number of features from the real dataset.
RegressionTeacher(const GeneralOptions &general_options, const RegressionClassifierOptions &specific_options)
Create a new teacher out of the general and the regression method specific options.
std::vector< float > getFeature(unsigned int iFeature) override
Return a specific feature from the real dataset.
void load(const boost::property_tree::ptree &pt) override
Load the options from a boost property tree. Only loads the maximal bin number.
std::vector< float > getTargets() override
Return the targets from the real dataset.
Dataset * m_childDataSet
The real data set (our child)
Abstract base class for different kinds of events.
std::vector< float > apply(Dataset &test_data) const override
Apply the loaded experts by averaging over the single expert decisions.
void load(Weightfile &weightfile) override
Load the expert from the weightfile by loading each stored single classifier one ofter the other.
Abstract base class of all Teachers Each MVA library has its own implementation of this class,...
unsigned int getNumberOfEvents() const override
Return the number of events from the real dataset.
void addOptions(const Options &options)
Add an Option object to the xml tree.
unsigned int m_numberOfBins
How many bins to use.
static Weightfile loadFromXMLFile(const std::string &filename)
Static function which loads a Weightfile from a XML file.
General options which are shared by all MVA trainings.
RegressionClassifierOptions m_specific_options
The method specific options.
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)
unsigned int getMaximalBinNumber() const
Return the number of bins to use.
double m_cutValue
The cut value.
unsigned int getNumberOfSpectators() const override
Return the number of spectators from the real dataset.
po::options_description getDescription() override
Get the descriptions for these options.
std::string generateFileName(const std::string &suffix="")
Returns a temporary filename with the given suffix.
Dataset(const GeneralOptions &general_options)
Constructs a new dataset given the general options.
void save(boost::property_tree::ptree &pt) const override
Save the options from a boost property tree. Only saves the maximal bin number.
void loadEvent(unsigned int iEvent) override
Load an event. Sets all internal variables and sets the isSignal variable dependent on the cut value.
const BaseClassifierOptions & getBaseClassifierOptions() const
Return options of the base classifier (const version)