Logo ROOT  
Reference Guide
MethodPyTorch.h
Go to the documentation of this file.
1 // @(#)root/tmva/pymva $Id$
2 // Author: Anirudh Dagar, 2020
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : MethodPyTorch *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Interface for PyTorch python based scientific package supporting *
12  * automatic differentiation for machine learning. *
13  * *
14  * Authors (alphabetical): *
15  * Anirudh Dagar <anirudhdagar6@gmail.com> - IIT, Roorkee *
16  * *
17  * Copyright (c) 2020: *
18  * CERN, Switzerland *
19  * IIT, Roorkee *
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_MethodPyTorch
27 #define ROOT_TMVA_MethodPyTorch
28 
29 #include "TMVA/PyMethodBase.h"
30 #include <vector>
31 
32 namespace TMVA {
33 
34  class MethodPyTorch : public PyMethodBase {
35 
36  public :
37 
38  // constructors
39  MethodPyTorch(const TString &jobName,
40  const TString &methodTitle,
41  DataSetInfo &dsi,
42  const TString &theOption = "");
44  const TString &theWeightFile);
46 
47  void Train();
48  void Init();
49  void DeclareOptions();
50  void ProcessOptions();
51 
52  // Check whether the given analysis type (regression, classification, ...)
53  // is supported by this method
55  // Get signal probability of given event
56  Double_t GetMvaValue(Double_t *errLower, Double_t *errUpper);
57  std::vector<Double_t> GetMvaValues(Long64_t firstEvt, Long64_t lastEvt, Bool_t logProgress);
58  // Get regression values of given event
59  std::vector<Float_t>& GetRegressionValues();
60  // Get class probabilities of given event
61  std::vector<Float_t>& GetMulticlassValues();
62 
63  const Ranking *CreateRanking() { return 0; }
64  virtual void TestClassification();
65  virtual void AddWeightsXMLTo(void*) const{}
66  virtual void ReadWeightsFromXML(void*){}
67  virtual void ReadWeightsFromStream(std::istream&) {} // backward compatibility
68  virtual void ReadWeightsFromStream(TFile&){} // backward compatibility
69  void ReadModelFromFile();
70 
71  void GetHelpMessage() const;
72 
73 
74  private:
75 
76  TString fFilenameModel; // Filename of the previously exported PyTorch model
77  UInt_t fBatchSize {0}; // Training batch size
78  UInt_t fNumEpochs {0}; // Number of training epochs
79  Int_t fNumThreads {0}; // Number of CPU threads (if 0 uses default values)
80 
81  Bool_t fContinueTraining; // Load weights from previous training
82  Bool_t fSaveBestOnly; // Store only weights with smallest validation loss
83  TString fLearningRateSchedule; // Set new learning rate at specific epochs
84 
85  TString fNumValidationString; // option string defining the number of validation events
86 
87  TString fUserCodeName; // filename of the user script that will be executed before loading the PyTorch model
88 
89  bool fModelIsSetup = false; // flag whether model is loaded, needed for getMvaValue during evaluation
90  float* fVals = nullptr; // variables array used for GetMvaValue
91  std::vector<float> fOutput; // probability or regression output array used for GetMvaValue
92  UInt_t fNVars {0}; // number of variables
93  UInt_t fNOutputs {0}; // number of outputs (classes or targets)
94  TString fFilenameTrainedModel; // output filename for trained model
95 
96  void SetupPyTorchModel(Bool_t loadTrainedModel); // setups the needed variables, loads the model
97  UInt_t GetNumValidationSamples(); // get number of validation events according to given option
98 
100  };
101 
102 } // namespace TMVA
103 
104 #endif // ROOT_TMVA_MethodPyTorch
TMVA::MethodPyTorch::Init
void Init()
Definition: MethodPyTorch.cxx:275
TMVA::PyMethodBase
Definition: PyMethodBase.h:56
TMVA::MethodPyTorch::MethodPyTorch
MethodPyTorch(const TString &jobName, const TString &methodTitle, DataSetInfo &dsi, const TString &theOption="")
Definition: MethodPyTorch.cxx:38
TMVA::MethodPyTorch::TestClassification
virtual void TestClassification()
initialization
Definition: MethodPyTorch.cxx:503
TMVA::MethodPyTorch::fNumEpochs
UInt_t fNumEpochs
Definition: MethodPyTorch.h:78
TMVA::MethodPyTorch::DeclareOptions
void DeclareOptions()
Definition: MethodPyTorch.cxx:74
TMVA::MethodPyTorch::fNVars
UInt_t fNVars
Definition: MethodPyTorch.h:92
TMVA::Ranking
Ranking for variables in method (implementation)
Definition: Ranking.h:48
TMVA::MethodPyTorch::fUserCodeName
TString fUserCodeName
Definition: MethodPyTorch.h:87
Long64_t
long long Long64_t
Definition: RtypesCore.h:80
TMVA::MethodPyTorch::GetNumValidationSamples
UInt_t GetNumValidationSamples()
Validation of the ValidationSize option.
Definition: MethodPyTorch.cxx:100
TMVA::MethodPyTorch::fContinueTraining
Bool_t fContinueTraining
Definition: MethodPyTorch.h:81
TMVA::MethodPyTorch::ReadWeightsFromStream
virtual void ReadWeightsFromStream(std::istream &)
Definition: MethodPyTorch.h:67
TMVA::MethodPyTorch::fVals
float * fVals
Definition: MethodPyTorch.h:90
TMVA::MethodPyTorch::fOutput
std::vector< float > fOutput
Definition: MethodPyTorch.h:91
TString
Basic string class.
Definition: TString.h:136
TMVA::MethodPyTorch::GetHelpMessage
void GetHelpMessage() const
Definition: MethodPyTorch.cxx:648
TMVA::MethodPyTorch::fFilenameModel
TString fFilenameModel
Definition: MethodPyTorch.h:76
TMVA::MethodPyTorch
Definition: MethodPyTorch.h:34
bool
TMVA::MethodPyTorch::GetMvaValue
Double_t GetMvaValue(Double_t *errLower, Double_t *errUpper)
Definition: MethodPyTorch.cxx:507
TMVA::MethodPyTorch::fLearningRateSchedule
TString fLearningRateSchedule
Definition: MethodPyTorch.h:83
TMVA::DataSetInfo
Class that contains all the data information.
Definition: DataSetInfo.h:62
TMVA::Types::EAnalysisType
EAnalysisType
Definition: Types.h:128
TMVA::MethodPyTorch::fModelIsSetup
bool fModelIsSetup
Definition: MethodPyTorch.h:89
TMVA::MethodPyTorch::fNumValidationString
TString fNumValidationString
Definition: MethodPyTorch.h:85
TMVA::MethodPyTorch::fBatchSize
UInt_t fBatchSize
Definition: MethodPyTorch.h:77
TFile
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
unsigned int
TMVA::MethodPyTorch::GetRegressionValues
std::vector< Float_t > & GetRegressionValues()
Definition: MethodPyTorch.cxx:596
TMVA::MethodPyTorch::Train
void Train()
Definition: MethodPyTorch.cxx:297
TMVA::MethodPyTorch::GetMvaValues
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
Definition: MethodPyTorch.cxx:529
Double_t
double Double_t
Definition: RtypesCore.h:59
TMVA::MethodPyTorch::ReadModelFromFile
void ReadModelFromFile()
Definition: MethodPyTorch.cxx:644
TMVA::MethodPyTorch::fFilenameTrainedModel
TString fFilenameTrainedModel
Definition: MethodPyTorch.h:94
TMVA::MethodPyTorch::ReadWeightsFromStream
virtual void ReadWeightsFromStream(TFile &)
Definition: MethodPyTorch.h:68
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TMVA::MethodPyTorch::~MethodPyTorch
~MethodPyTorch()
Definition: MethodPyTorch.cxx:62
TMVA::MethodPyTorch::CreateRanking
const Ranking * CreateRanking()
Definition: MethodPyTorch.h:63
TMVA::MethodPyTorch::fSaveBestOnly
Bool_t fSaveBestOnly
Definition: MethodPyTorch.h:82
TMVA::MethodPyTorch::HasAnalysisType
Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t)
Definition: MethodPyTorch.cxx:66
type
int type
Definition: TGX11.cxx:121
TMVA::MethodPyTorch::AddWeightsXMLTo
virtual void AddWeightsXMLTo(void *) const
Definition: MethodPyTorch.h:65
PyMethodBase.h
TMVA::MethodPyTorch::ProcessOptions
void ProcessOptions()
Definition: MethodPyTorch.cxx:152
TMVA::MethodPyTorch::ReadWeightsFromXML
virtual void ReadWeightsFromXML(void *)
Definition: MethodPyTorch.h:66
TMVA::MethodPyTorch::fNOutputs
UInt_t fNOutputs
Definition: MethodPyTorch.h:93
TMVA::MethodPyTorch::SetupPyTorchModel
void SetupPyTorchModel(Bool_t loadTrainedModel)
Definition: MethodPyTorch.cxx:190
TMVA::MethodPyTorch::GetMulticlassValues
std::vector< Float_t > & GetMulticlassValues()
Definition: MethodPyTorch.cxx:626
TMVA::MethodPyTorch::fNumThreads
Int_t fNumThreads
Definition: MethodPyTorch.h:79
TMVA
create variable transformations
Definition: GeneticMinimizer.h:22
int