7 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION 8 #include <numpy/arrayobject.h> 28 fContinueTraining =
false;
30 fTriesEarlyStopping = -1;
31 fLearningRateSchedule =
"";
32 fFilenameTrainedModel =
"";
67 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.");
88 TString filenameLoadModel;
89 if (loadTrainedModel) {
95 PyRunString(
"model = keras.models.load_model('"+filenameLoadModel+
"')",
96 "Failed to load Keras model from file: "+filenameLoadModel);
97 Log() << kINFO <<
"Load model from file: " << filenameLoadModel <<
Endl;
107 else Log() << kFATAL <<
"Selected analysis type is not implemented" <<
Endl;
111 npy_intp dimsVals[2] = {(npy_intp)1, (npy_intp)
fNVars};
112 PyArrayObject* pVals = (PyArrayObject*)PyArray_SimpleNewFromData(2, dimsVals, NPY_FLOAT, (
void*)
fVals);
116 npy_intp dimsOutput[2] = {(npy_intp)1, (npy_intp)
fNOutputs};
117 PyArrayObject* pOutput = (PyArrayObject*)PyArray_SimpleNewFromData(2, dimsOutput, NPY_FLOAT, (
void*)&
fOutput[0]);
126 Log() << kFATAL <<
"Python is not initialized" <<
Endl;
132 PyRunString(
"import sys; sys.argv = ['']",
"Set sys.argv failed");
133 PyRunString(
"import keras",
"Import Keras failed");
148 float* trainDataX =
new float[nTrainingEvents*
fNVars];
149 float* trainDataY =
new float[nTrainingEvents*
fNOutputs];
150 float* trainDataWeights =
new float[nTrainingEvents];
151 for (
UInt_t i=0; i<nTrainingEvents; i++) {
171 else Log() << kFATAL <<
"Can not fill target vector because analysis type is not known" <<
Endl;
177 npy_intp dimsTrainX[2] = {(npy_intp)nTrainingEvents, (npy_intp)
fNVars};
178 npy_intp dimsTrainY[2] = {(npy_intp)nTrainingEvents, (npy_intp)
fNOutputs};
179 npy_intp dimsTrainWeights[1] = {(npy_intp)nTrainingEvents};
180 PyArrayObject* pTrainDataX = (PyArrayObject*)PyArray_SimpleNewFromData(2, dimsTrainX, NPY_FLOAT, (
void*)trainDataX);
181 PyArrayObject* pTrainDataY = (PyArrayObject*)PyArray_SimpleNewFromData(2, dimsTrainY, NPY_FLOAT, (
void*)trainDataY);
182 PyArrayObject* pTrainDataWeights = (PyArrayObject*)PyArray_SimpleNewFromData(1, dimsTrainWeights, NPY_FLOAT, (
void*)trainDataWeights);
185 PyDict_SetItemString(
fLocalNS,
"trainWeights", (
PyObject*)pTrainDataWeights);
195 float* valDataX =
new float[nValEvents*
fNVars];
196 float* valDataY =
new float[nValEvents*
fNOutputs];
197 float* valDataWeights =
new float[nValEvents];
198 for (
UInt_t i=0; i<nValEvents; i++) {
216 else Log() << kFATAL <<
"Can not fill target vector because analysis type is not known" <<
Endl;
221 npy_intp dimsValX[2] = {(npy_intp)nValEvents, (npy_intp)
fNVars};
222 npy_intp dimsValY[2] = {(npy_intp)nValEvents, (npy_intp)
fNOutputs};
223 npy_intp dimsValWeights[1] = {(npy_intp)nValEvents};
224 PyArrayObject* pValDataX = (PyArrayObject*)PyArray_SimpleNewFromData(2, dimsValX, NPY_FLOAT, (
void*)valDataX);
225 PyArrayObject* pValDataY = (PyArrayObject*)PyArray_SimpleNewFromData(2, dimsValY, NPY_FLOAT, (
void*)valDataY);
226 PyArrayObject* pValDataWeights = (PyArrayObject*)PyArray_SimpleNewFromData(1, dimsValWeights, NPY_FLOAT, (
void*)valDataWeights);
240 PyDict_SetItemString(
fLocalNS,
"batchSize", pBatchSize);
241 PyDict_SetItemString(
fLocalNS,
"numEpochs", pNumEpochs);
242 PyDict_SetItemString(
fLocalNS,
"verbose", pVerbose);
249 PyRunString(
"callbacks.append(keras.callbacks.ModelCheckpoint('"+
fFilenameTrainedModel+
"', monitor='val_loss', verbose=verbose, save_best_only=True, mode='auto'))",
"Failed to setup training callback: SaveBestOnly");
250 Log() << kINFO <<
"Option SaveBestOnly: Only model weights with smallest validation loss will be stored" <<
Endl;
257 PyRunString(
"callbacks.append(keras.callbacks.EarlyStopping(monitor='val_loss', patience="+tries+
", verbose=verbose, mode='auto'))",
"Failed to setup training callback: TriesEarlyStopping");
258 Log() << kINFO <<
"Option TriesEarlyStopping: Training will stop after " << tries <<
" number of epochs with no improvement of validation loss" <<
Endl;
265 "schedulerSteps = {}\n" 266 "for c in strScheduleSteps.split(';'):\n" 267 " x = c.split(',')\n" 268 " schedulerSteps[int(x[0])] = float(x[1])\n",
272 PyRunString(
"def schedule(epoch, model=model, schedulerSteps=schedulerSteps):\n" 273 " if epoch in schedulerSteps: return float(schedulerSteps[epoch])\n" 274 " else: return float(model.optimizer.lr.get_value())\n",
278 PyRunString(
"callbacks.append(keras.callbacks.LearningRateScheduler(schedule))",
279 "Failed to setup training callback: LearningRateSchedule");
284 PyRunString(
"history = model.fit(trainX, trainY, sample_weight=trainWeights, batch_size=batchSize, nb_epoch=numEpochs, verbose=verbose, validation_data=(valX, valY, valWeights), callbacks=callbacks)",
285 "Failed to train model");
304 delete[] trainDataWeights;
307 delete[] valDataWeights;
328 PyRunString(
"for i,p in enumerate(model.predict(vals)): output[i]=p\n",
329 "Failed to get predictions");
344 if (firstEvt > lastEvt || lastEvt > nEvents) lastEvt =
nEvents;
345 if (firstEvt < 0) firstEvt = 0;
346 nEvents = lastEvt-firstEvt;
357 npy_intp dimsData[2] = {(npy_intp)nEvents, (npy_intp)
fNVars};
358 PyArrayObject* pDataMvaValues = (PyArrayObject*)PyArray_SimpleNewFromData(2, dimsData, NPY_FLOAT, (
void*)
data);
359 if (pDataMvaValues==0)
Log() <<
"Failed to load data to Python array" <<
Endl;
363 if (pModel==0)
Log() << kFATAL <<
"Failed to get model Python object" <<
Endl;
364 PyArrayObject* pPredictions = (PyArrayObject*) PyObject_CallMethod(pModel, (
char*)
"predict", (
char*)
"O", pDataMvaValues);
365 if (pPredictions==0)
Log() << kFATAL <<
"Failed to get predictions" <<
Endl;
370 std::vector<double> mvaValues(nEvents);
371 float* predictionsData = (
float*) PyArray_DATA(pPredictions);
390 PyRunString(
"for i,p in enumerate(model.predict(vals)): output[i]=p\n",
391 "Failed to get predictions");
418 PyRunString(
"for i,p in enumerate(model.predict(vals)): output[i]=p\n",
419 "Failed to get predictions");
431 Log() <<
"Keras is a high-level API for the Theano and Tensorflow packages." <<
Endl;
432 Log() <<
"This method wraps the training and predictions steps of the Keras" <<
Endl;
433 Log() <<
"Python package for TMVA, so that dataloading, preprocessing and" <<
Endl;
434 Log() <<
"evaluation can be done within the TMVA system. To use this Keras" <<
Endl;
435 Log() <<
"interface, you have to generate a model with Keras first. Then," <<
Endl;
436 Log() <<
"this model can be loaded and trained in TMVA." <<
Endl;
Double_t GetMvaValue(Double_t *errLower, Double_t *errUpper)
void SetCurrentEvent(Long64_t ievt) const
MsgLogger & Endl(MsgLogger &ml)
Singleton class for Global types used by TMVA.
OptionBase * DeclareOptionRef(T &ref, const TString &name, const TString &desc="")
TransformationHandler & GetTransformationHandler(Bool_t takeReroutedIfAvailable=true)
UInt_t GetNClasses() const
static int PyIsInitialized()
Check Python interpreter initialization status.
const TString & GetWeightFileDir() const
std::vector< Float_t > & GetRegressionValues()
void PyRunString(TString code, TString errorMessage="Failed to run python code", int start=Py_single_input)
Execute Python code from string.
void GetHelpMessage() const
const Event * GetEvent() const
TString fFilenameTrainedModel
DataSetInfo & DataInfo() const
Class that contains all the data information.
Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t)
Double_t GetWeight() const
return the event weight - depending on whether the flag IgnoreNegWeightsInTraining is or not...
Long64_t GetNTrainingEvents() const
const Event * GetTrainingEvent(Long64_t ievt) const
TString fLearningRateSchedule
const Event * GetTestingEvent(Long64_t ievt) const
Float_t GetTarget(UInt_t itgt) const
UInt_t GetNTargets() const
const char * GetName() const
Int_t fTriesEarlyStopping
void SetTarget(UInt_t itgt, Float_t value)
set the target value (dimension itgt) to value
std::vector< Float_t > & GetMulticlassValues()
Long64_t GetNTestEvents() const
UInt_t GetNVariables() const
Float_t GetValue(UInt_t ivar) const
return value of i'th variable
void SetupKerasModel(Bool_t loadTrainedModel)
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 TestClassification()
initialization
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
#define REGISTER_METHOD(CLASS)
for example
Abstract ClassifierFactory template that handles arbitrary types.
Long64_t GetNEvents(Types::ETreeType type=Types::kMaxTreeType) const
Types::EAnalysisType GetAnalysisType() const
MethodPyKeras(const TString &jobName, const TString &methodTitle, DataSetInfo &dsi, const TString &theOption="")
virtual void TestClassification()
initialization
std::vector< float > fOutput
void NoErrorCalc(Double_t *const err, Double_t *const errUpper)