7#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
8#include <numpy/arrayobject.h>
92 DeclareOptionRef(
fTriesEarlyStopping,
"TriesEarlyStopping",
"Number of epochs with no improvement in validation loss after which training will be stopped. The default or a negative number deactivates this option.");
95 "Write a log during training to visualize and monitor the training performance with TensorBoard");
98 "Specify as 0.2 or 20% to use a fifth of the data set as validation set. "
99 "Specify as 100 to use exactly 100 events. (Default: 20%)");
101 "Optional python code provided by the user to be executed before loading the Keras model");
119 if (fNumValidationString.EndsWith(
"%")) {
127 Log() << kFATAL <<
"Cannot parse number \"" << fNumValidationString
128 <<
"\". Expected string like \"20%\" or \"20.0%\"." <<
Endl;
130 }
else if (fNumValidationString.IsFloat()) {
141 Log() << kFATAL <<
"Cannot parse number \"" << fNumValidationString <<
"\". Expected string like \"0.2\" or \"100\"."
148 Log() << kFATAL <<
"Validation size \"" << fNumValidationString <<
"\" is negative." <<
Endl;
152 Log() << kFATAL <<
"Validation size \"" << fNumValidationString <<
"\" is zero." <<
Endl;
156 Log() << kFATAL <<
"Validation size \"" << fNumValidationString
157 <<
"\" is larger than or equal in size to training set (size=\"" <<
trainingSetSize <<
"\")." <<
Endl;
188 Log() << kINFO <<
"Setting up tf.keras" <<
Endl;
190 Log() << kINFO <<
"Setting up keras with " <<
gSystem->
Getenv(
"KERAS_BACKEND") <<
" backend" <<
Endl;
207 PyRunString(
"keras_major_version = int(keras.__version__.split('.')[0])");
208 PyRunString(
"keras_minor_version = int(keras.__version__.split('.')[1])");
223 Log() << kINFO <<
"Keras is not found. Trying using tf.keras" <<
Endl;
232 if (
ret ==
nullptr) {
233 Log() << kFATAL <<
"Importing TensorFlow failed" <<
Endl;
236 PyRunString(
"tf_major_version = int(tf.__version__.split('.')[0])");
243 Log() << kWARNING <<
"Using TensorFlow version 1.x which does not contain tf.keras - use then TensorFlow as Keras backend" <<
Endl;
247 Log() << kFATAL <<
"Keras is not present and not a suitable TensorFlow version is found " <<
Endl;
255 Log() << kWARNING <<
"The Keras version is not compatible with TensorFlow 2. Use instead tf.keras" <<
Endl;
264 Log() << kINFO <<
"Use Keras version from TensorFlow : tf.keras" <<
Endl;
270 Log() << kINFO <<
"Use TensorFlow as Keras backend" <<
Endl;
272 PyRunString(
"from keras.backend import tensorflow_backend as K");
287 TString::Format(
"session_conf = %s(intra_op_parallelism_threads=%d,inter_op_parallelism_threads=%d)",
298 Log() << kINFO <<
"Applying GPU option: gpu_options." <<
optlist->At(
item)->GetName() <<
Endl;
307 PyRunString(
"tf.compat.v1.keras.backend.set_session(sess)");
314 Log() << kWARNING <<
"Cannot set the given " <<
fNumThreads <<
" threads when not using tensorflow as backend"
317 Log() << kWARNING <<
"Cannot set the given GPU option " <<
fGpuOptions
318 <<
" when not using tensorflow as backend" <<
Endl;
329 Log() << kINFO <<
" Loading Keras Model " <<
Endl;
344 PyRunString(
"print('custom objects for loading model : ',load_model_custom_objects)");
357 "', custom_objects=load_model_custom_objects)",
"Failed to load Keras model from file: " +
filenameLoadModel);
370 else Log() << kFATAL <<
"Selected analysis type is not implemented" <<
Endl;
387 PyRunString(
"tf.compat.v1.disable_eager_execution()",
"Failed to disable eager execution");
388 Log() << kINFO <<
"Disabled TF eager execution when evaluating model " <<
Endl;
420 Log() << kFATAL <<
"Python is not initialized" <<
Endl;
425 PyRunString(
"import sys; sys.argv = ['']",
"Set sys.argv failed");
444 Log() << kINFO <<
"Split TMVA training data in " <<
nTrainingEvents <<
" training events and "
470 else Log() << kFATAL <<
"Can not fill target vector because analysis type is not known" <<
Endl;
517 else Log() << kFATAL <<
"Can not fill target vector because analysis type is not known" <<
Endl;
535 Log() << kINFO <<
"Training Model Summary" <<
Endl;
552 PyRunString(
"callbacks.append(" +
fKerasString +
".callbacks.ModelCheckpoint('"+
fFilenameTrainedModel+
"', monitor='val_loss', verbose=verbose, save_best_only=True, mode='auto'))",
"Failed to setup training callback: SaveBestOnly");
553 Log() << kINFO <<
"Option SaveBestOnly: Only model weights with smallest validation loss will be stored" <<
Endl;
560 PyRunString(
"callbacks.append(" +
fKerasString +
".callbacks.EarlyStopping(monitor='val_loss', patience="+
tries+
", verbose=verbose, mode='auto'))",
"Failed to setup training callback: TriesEarlyStopping");
561 Log() << kINFO <<
"Option TriesEarlyStopping: Training will stop after " <<
tries <<
" number of epochs with no improvement of validation loss" <<
Endl;
567 std::vector<std::pair<std::string, std::string>>
scheduleSteps;
572 if (!
x ||
x->GetEntries() != 2) {
573 Log() << kFATAL <<
"Invalid values given in LearningRateSchedule, it should be as \"10,0.1;20,0.01\""
577 std::string((*
x)[1]->
GetName() ) ) );
578 std::cout <<
" add learning rate schedule " <<
scheduleSteps.back().first <<
" : " <<
scheduleSteps.back().second << std::endl;
597 " for e in epochs:\n"
598 " if (epoch < e) :\n"
599 " return lrValues[i]\n"
606 "Failed to setup training callback: LearningRateSchedule");
615 ", histogram_freq=0, batch_size=batchSize, write_graph=True, write_grads=False, write_images=False))",
616 "Failed to setup training callback: TensorBoard");
617 Log() << kINFO <<
"Option TensorBoard: Log files for training monitoring are stored in: " <<
logdir <<
Endl;
621 PyRunString(
"history = model.fit(trainX, trainY, sample_weight=trainWeights, batch_size=batchSize, epochs=numEpochs, verbose=verbose, validation_data=(valX, valY, valWeights), callbacks=callbacks)",
622 "Failed to train model");
625 std::vector<float> fHistory;
633 PyRunString(
"number_of_keys=len(history.history.keys())");
646 Log() << kINFO <<
"Getting training history for item:" <<
iHis <<
" name = " <<
name <<
Endl;
649 for (
size_t i=0; i<fHistory.size(); i++)
698 +
")): output[i]=p\n";
725 <<
" sample (" << nEvents <<
" events)" <<
Endl;
728 for (
UInt_t i=0; i<nEvents; i++) {
736 std::vector<double> mvaValues(nEvents);
743 if (
pModel==0)
Log() << kFATAL <<
"Failed to get model Python object" <<
Endl;
751 for (
UInt_t i=0; i<nEvents; i++) {
757 <<
"Elapsed time for evaluation of " << nEvents <<
" events: "
758 <<
timer.GetElapsedTime() <<
" " <<
Endl;
780 +
")): output[i]=p\n";
810 +
")): output[i]=p\n";
823 Log() <<
"Keras is a high-level API for the Theano and Tensorflow packages." <<
Endl;
824 Log() <<
"This method wraps the training and predictions steps of the Keras" <<
Endl;
825 Log() <<
"Python package for TMVA, so that dataloading, preprocessing and" <<
Endl;
826 Log() <<
"evaluation can be done within the TMVA system. To use this Keras" <<
Endl;
827 Log() <<
"interface, you have to generate a model with Keras first. Then," <<
Endl;
828 Log() <<
"this model can be loaded and trained in TMVA." <<
Endl;
839 PyRunString(
"keras_backend_is_set = keras.backend.backend() == \"tensorflow\"");
844 PyRunString(
"keras_backend_is_set = keras.backend.backend() == \"theano\"");
849 PyRunString(
"keras_backend_is_set = keras.backend.backend() == \"cntk\"");
#define REGISTER_METHOD(CLASS)
for example
long long Long64_t
Portable signed long integer 8 bytes.
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
R__EXTERN TSystem * gSystem
OptionBase * DeclareOptionRef(T &ref, const TString &name, const TString &desc="")
Class that contains all the data information.
UInt_t GetNClasses() const
UInt_t GetNTargets() const
Types::ETreeType GetCurrentType() const
Long64_t GetNEvents(Types::ETreeType type=Types::kMaxTreeType) const
Long64_t GetNTrainingEvents() const
void SetCurrentEvent(Long64_t ievt) const
PyGILState_STATE m_GILState
const char * GetName() const override
Types::EAnalysisType GetAnalysisType() const
const TString & GetWeightFileDir() const
const TString & GetMethodName() const
const Event * GetEvent() const
DataSetInfo & DataInfo() const
virtual void TestClassification()
initialization
UInt_t GetNVariables() const
TransformationHandler & GetTransformationHandler(Bool_t takeReroutedIfAvailable=true)
void NoErrorCalc(Double_t *const err, Double_t *const errUpper)
TrainingHistory fTrainHistory
const Event * GetTrainingEvent(Long64_t ievt) const
void GetHelpMessage() const override
void ProcessOptions() override
Function processing the options This is called only when creating the method before training not when...
std::vector< float > fOutput
std::vector< Float_t > & GetRegressionValues() override
Bool_t UseTFKeras() const
Int_t fTriesEarlyStopping
EBackendType
enumeration defining the used Keras backend
void SetupKerasModel(Bool_t loadTrainedModel)
Double_t GetMvaValue(Double_t *errLower, Double_t *errUpper) override
void DeclareOptions() override
UInt_t GetNumValidationSamples()
Validation of the ValidationSize option.
void TestClassification() override
initialization
void SetupKerasModelForEval()
Setting up model for evaluation Add here some needed optimizations like disabling eager execution.
bool fModelIsSetupForEval
TString fNumValidationString
std::vector< float > fVals
std::vector< Double_t > GetMvaValues(Long64_t firstEvt, Long64_t lastEvt, Bool_t logProgress) override
get all the MVA values for the events of the current Data type
TString GetKerasBackendName()
MethodPyKeras(const TString &jobName, const TString &methodTitle, DataSetInfo &dsi, const TString &theOption="")
TString fLearningRateSchedule
std::vector< Float_t > & GetMulticlassValues() override
Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t) override
void Init() override
Initialization function called from MethodBase::SetupMethod() Note that option string are not yet fil...
void ReadModelFromFile() override
EBackendType GetKerasBackend()
Get the Keras backend (can be: TensorFlow, Theano or CNTK)
TString fFilenameTrainedModel
Virtual base class for all TMVA method based on Python.
static int PyIsInitialized()
Check Python interpreter initialization status.
static PyObject * fGlobalNS
void PyRunString(TString code, TString errorMessage="Failed to run python code", int start=256)
Execute Python code from string.
Timing information for training and evaluation of MVA methods.
void AddValue(TString Property, Int_t stage, Double_t value)
Singleton class for Global types used by TMVA.
@ kSignal
Never change this number - it is elsewhere assumed to be zero !
TObjArray * Tokenize(const TString &delim) const
This function is used to isolate sequential tokens in a TString.
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
virtual const char * Getenv(const char *env)
Get environment variable.
std::string ToString(const T &val)
Utility function for conversion to strings.
create variable transformations
MsgLogger & Endl(MsgLogger &ml)