Logo ROOT   6.18/05
Reference Guide
MethodPyKeras.h
Go to the documentation of this file.
1// @(#)root/tmva/pymva $Id$
2// Author: Stefan Wunsch
3
4/**********************************************************************************
5 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6 * Package: TMVA *
7 * Class : MethodPyKeras *
8 * Web : http://tmva.sourceforge.net *
9 * *
10 * Description: *
11 * Interface for Keras python package which is a wrapper for the Theano and *
12 * Tensorflow libraries *
13 * *
14 * Authors (alphabetical): *
15 * Stefan Wunsch <stefan.wunsch@cern.ch> - KIT, Germany *
16 * *
17 * Copyright (c) 2016: *
18 * CERN, Switzerland *
19 * KIT, Germany *
20 * *
21 * Redistribution and use in source and binary forms, with or without *
22 * modification, are permitted according to the terms listed in LICENSE *
23 * (http://tmva.sourceforge.net/LICENSE) *
24 **********************************************************************************/
25
26#ifndef ROOT_TMVA_MethodPyKeras
27#define ROOT_TMVA_MethodPyKeras
28
29#include "TMVA/PyMethodBase.h"
30
31namespace TMVA {
32
33 class MethodPyKeras : public PyMethodBase {
34
35 public :
36
37 // constructors
38 MethodPyKeras(const TString &jobName,
39 const TString &methodTitle,
40 DataSetInfo &dsi,
41 const TString &theOption = "");
43 const TString &theWeightFile);
45
46 void Train();
47 void Init();
48 void DeclareOptions();
49 void ProcessOptions();
50
51 // Check whether the given analysis type (regression, classification, ...)
52 // is supported by this method
54 // Get signal probability of given event
55 Double_t GetMvaValue(Double_t *errLower, Double_t *errUpper);
56 std::vector<Double_t> GetMvaValues(Long64_t firstEvt, Long64_t lastEvt, Bool_t logProgress);
57 // Get regression values of given event
58 std::vector<Float_t>& GetRegressionValues();
59 // Get class probabilities of given event
60 std::vector<Float_t>& GetMulticlassValues();
61
62 const Ranking *CreateRanking() { return 0; }
63 virtual void TestClassification();
64 virtual void AddWeightsXMLTo(void*) const{}
65 virtual void ReadWeightsFromXML(void*){}
66 virtual void ReadWeightsFromStream(std::istream&) {} // backward compatibility
67 virtual void ReadWeightsFromStream(TFile&){} // backward compatibility
68 void ReadModelFromFile();
69
70 void GetHelpMessage() const;
71
72 /// enumeration defining the used Keras backend
73 enum EBackendType { kUndefined = -1, kTensorFlow = 0, kTheano = 1, kCNTK = 2 };
74
75 /// Get the Keras backend (can be: TensorFlow, Theano or CNTK)
78
79 private:
80
81 TString fFilenameModel; // Filename of the previously exported Keras model
82 UInt_t fBatchSize {0}; // Training batch size
83 UInt_t fNumEpochs {0}; // Number of training epochs
84 Int_t fNumThreads {0}; // Number of CPU threads (if 0 uses default values)
85 Int_t fVerbose; // Keras verbosity during training
86 Bool_t fContinueTraining; // Load weights from previous training
87 Bool_t fSaveBestOnly; // Store only weights with smallest validation loss
88 Int_t fTriesEarlyStopping; // Stop training if validation loss is not decreasing for several epochs
89 TString fLearningRateSchedule; // Set new learning rate at specific epochs
90 TString fTensorBoard; // Store log files during training
91 TString fNumValidationString; // option string defining the number of validation events
92 TString fGpuOptions; // GPU options (for Tensorflow to set in session_config.gpu_options)
93
94 bool fModelIsSetup = false; // flag whether model is loaded, neede for getMvaValue during evaluation
95 float* fVals = nullptr; // variables array used for GetMvaValue
96 std::vector<float> fOutput; // probability or regression output array used for GetMvaValue
97 UInt_t fNVars {0}; // number of variables
98 UInt_t fNOutputs {0}; // number of outputs (classes or targets)
99 TString fFilenameTrainedModel; // output filename for trained model
100
101 void SetupKerasModel(Bool_t loadTrainedModel); // setups the needed variables loads the model
102 UInt_t GetNumValidationSamples(); // get numer of validation events according to given option
103
105 };
106
107} // namespace TMVA
108
109#endif // ROOT_TMVA_MethodPyKeras
int Int_t
Definition: RtypesCore.h:41
unsigned int UInt_t
Definition: RtypesCore.h:42
bool Bool_t
Definition: RtypesCore.h:59
double Double_t
Definition: RtypesCore.h:55
long long Long64_t
Definition: RtypesCore.h:69
#define ClassDef(name, id)
Definition: Rtypes.h:326
int type
Definition: TGX11.cxx:120
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:48
Class that contains all the data information.
Definition: DataSetInfo.h:60
void GetHelpMessage() const
std::vector< float > fOutput
Definition: MethodPyKeras.h:96
virtual void AddWeightsXMLTo(void *) const
Definition: MethodPyKeras.h:64
virtual void TestClassification()
initialization
virtual void ReadWeightsFromStream(std::istream &)
Definition: MethodPyKeras.h:66
virtual void ReadWeightsFromXML(void *)
Definition: MethodPyKeras.h:65
EBackendType
enumeration defining the used Keras backend
Definition: MethodPyKeras.h:73
void SetupKerasModel(Bool_t loadTrainedModel)
std::vector< Float_t > & GetMulticlassValues()
UInt_t GetNumValidationSamples()
Validation of the ValidationSize option.
Double_t GetMvaValue(Double_t *errLower, Double_t *errUpper)
std::vector< Float_t > & GetRegressionValues()
const Ranking * CreateRanking()
Definition: MethodPyKeras.h:62
TString fNumValidationString
Definition: MethodPyKeras.h:91
Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t)
TString GetKerasBackendName()
MethodPyKeras(const TString &jobName, const TString &methodTitle, DataSetInfo &dsi, const TString &theOption="")
TString fLearningRateSchedule
Definition: MethodPyKeras.h:89
EBackendType GetKerasBackend()
Get the Keras backend (can be: TensorFlow, Theano or CNTK)
TString fFilenameTrainedModel
Definition: MethodPyKeras.h:99
std::vector< Double_t > GetMvaValues(Long64_t firstEvt, Long64_t lastEvt, Bool_t logProgress)
get all the MVA values for the events of the current Data type
virtual void ReadWeightsFromStream(TFile &)
Definition: MethodPyKeras.h:67
Ranking for variables in method (implementation)
Definition: Ranking.h:48
EAnalysisType
Definition: Types.h:127
Basic string class.
Definition: TString.h:131
create variable transformations