Logo ROOT  
Reference Guide
MethodDNN.h
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Peter Speckmayer
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : MethodDNN *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * NeuralNetwork *
12  * *
13  * Authors (alphabetical): *
14  * Peter Speckmayer <peter.speckmayer@gmx.at> - CERN, Switzerland *
15  * Simon Pfreundschuh <s.pfreundschuh@gmail.com> - CERN, Switzerland *
16  * *
17  * Copyright (c) 2005-2015: *
18  * CERN, Switzerland *
19  * U. of Victoria, Canada *
20  * MPI-K Heidelberg, Germany *
21  * U. of Bonn, Germany *
22  * *
23  * Redistribution and use in source and binary forms, with or without *
24  * modification, are permitted according to the terms listed in LICENSE *
25  * (http://tmva.sourceforge.net/LICENSE) *
26  **********************************************************************************/
27 
28 //#pragma once
29 
30 #ifndef ROOT_TMVA_MethodDNN
31 #define ROOT_TMVA_MethodDNN
32 
33 //////////////////////////////////////////////////////////////////////////
34 // //
35 // MethodDNN //
36 // //
37 // Neural Network implementation //
38 // //
39 //////////////////////////////////////////////////////////////////////////
40 
41 #include <vector>
42 #include <map>
43 #include <string>
44 #include <sstream>
45 
46 #include "TString.h"
47 #include "TTree.h"
48 #include "TRandom3.h"
49 #include "TH1F.h"
50 #include "TMVA/MethodBase.h"
51 #include "TMVA/NeuralNet.h"
52 
53 #include "TMVA/Tools.h"
54 
55 #include "TMVA/DNN/Net.h"
56 #include "TMVA/DNN/Minimizers.h"
58 
59 #ifdef R__HAS_TMVACPU
60 #define DNNCPU
61 #endif
62 #ifdef R__HAS_TMVAGPU
63 //#define DNNCUDA
64 #endif
65 
66 #ifdef DNNCPU
68 #endif
69 
70 #ifdef DNNCUDA
72 #endif
73 
74 namespace TMVA {
75 
76 class MethodDNN : public MethodBase
77 {
79 
84 
85 private:
86  using LayoutVector_t = std::vector<std::pair<int, DNN::EActivationFunction>>;
87  using KeyValueVector_t = std::vector<std::map<TString, TString>>;
88 
90  {
91  size_t batchSize;
92  size_t testInterval;
98  std::vector<Double_t> dropoutProbabilities;
100  };
101 
102  // the option handling methods
104  void ProcessOptions();
105 
107 
108  // general helper functions
109  void Init();
110 
114 
122  std::vector<TTrainingSettings> fTrainingSettings;
123  bool fResume;
124 
126 
127  ClassDef(MethodDNN,0); // neural network
128 
129  static inline void WriteMatrixXML(void *parent, const char *name,
130  const TMatrixT<Double_t> &X);
131  static inline void ReadMatrixXML(void *xml, const char *name,
132  TMatrixT<Double_t> &X);
133 protected:
134 
135  void MakeClassSpecific( std::ostream&, const TString& ) const;
136  void GetHelpMessage() const;
137 
138 public:
139 
140  // Standard Constructors
141  MethodDNN(const TString& jobName,
142  const TString& methodTitle,
143  DataSetInfo& theData,
144  const TString& theOption);
146  const TString& theWeightFile);
147  virtual ~MethodDNN();
148 
150  UInt_t numberClasses,
151  UInt_t numberTargets );
154  TString blockDelim,
155  TString tokenDelim);
156  void Train();
157  void TrainGpu();
158  void TrainCpu();
159 
160  virtual Double_t GetMvaValue( Double_t* err=0, Double_t* errUpper=0 );
161  virtual const std::vector<Float_t>& GetRegressionValues();
162  virtual const std::vector<Float_t>& GetMulticlassValues();
163 
165 
166  // write weights to stream
167  void AddWeightsXMLTo ( void* parent ) const;
168 
169  // read weights from stream
170  void ReadWeightsFromStream( std::istream & i );
171  void ReadWeightsFromXML ( void* wghtnode );
172 
173  // ranking of input variables
174  const Ranking* CreateRanking();
175 
176 };
177 
178 inline void MethodDNN::WriteMatrixXML(void *parent,
179  const char *name,
180  const TMatrixT<Double_t> &X)
181 {
182  std::stringstream matrixStringStream("");
183  matrixStringStream.precision( 16 );
184 
185  for (size_t i = 0; i < (size_t) X.GetNrows(); i++)
186  {
187  for (size_t j = 0; j < (size_t) X.GetNcols(); j++)
188  {
189  matrixStringStream << std::scientific << X(i,j) << " ";
190  }
191  }
192  std::string s = matrixStringStream.str();
193  void* matxml = gTools().xmlengine().NewChild(parent, 0, name);
194  gTools().xmlengine().NewAttr(matxml, 0, "rows",
195  gTools().StringFromInt((int)X.GetNrows()));
196  gTools().xmlengine().NewAttr(matxml, 0, "cols",
197  gTools().StringFromInt((int)X.GetNcols()));
198  gTools().xmlengine().AddRawLine (matxml, s.c_str());
199 }
200 
201 inline void MethodDNN::ReadMatrixXML(void *xml,
202  const char *name,
204 {
205  void *matrixXML = gTools().GetChild(xml, name);
206  size_t rows, cols;
207  gTools().ReadAttr(matrixXML, "rows", rows);
208  gTools().ReadAttr(matrixXML, "cols", cols);
209 
210  const char * matrixString = gTools().xmlengine().GetNodeContent(matrixXML);
211  std::stringstream matrixStringStream(matrixString);
212 
213  for (size_t i = 0; i < rows; i++)
214  {
215  for (size_t j = 0; j < cols; j++)
216  {
217  matrixStringStream >> X(i,j);
218  }
219  }
220 }
221 } // namespace TMVA
222 
223 #endif
Cpu.h
TXMLEngine::NewAttr
XMLAttrPointer_t NewAttr(XMLNodePointer_t xmlnode, XMLNsPointer_t, const char *name, const char *value)
creates new attribute for xmlnode, namespaces are not supported for attributes
Definition: TXMLEngine.cxx:586
TMVA::MethodDNN::TTrainingSettings::testInterval
size_t testInterval
Definition: MethodDNN.h:92
TMVA::MethodDNN::fOutputFunction
DNN::EOutputFunction fOutputFunction
Definition: MethodDNN.h:113
TMVA::MethodDNN::GetRegressionValues
virtual const std::vector< Float_t > & GetRegressionValues()
Definition: MethodDNN.cxx:1314
Minimizers.h
TH1F.h
TMVA::Tools::GetChild
void * GetChild(void *parent, const char *childname=0)
get child node
Definition: Tools.cxx:1162
TMVA::MethodDNN::ReadWeightsFromStream
virtual void ReadWeightsFromStream(std::istream &)=0
TMVA::DNN::EOutputFunction
EOutputFunction
Enum that represents output functions.
Definition: Functions.h:46
TXMLEngine::NewChild
XMLNodePointer_t NewChild(XMLNodePointer_t parent, XMLNsPointer_t ns, const char *name, const char *content=nullptr)
create new child element for parent node
Definition: TXMLEngine.cxx:715
TMVA::MethodDNN::TestMethodDNNValidationSize
friend struct TestMethodDNNValidationSize
Definition: MethodDNN.h:78
TMVA::DNN::TReference
The reference architecture class.
Definition: Reference.h:53
TMVA::MethodBase::ReadWeightsFromStream
virtual void ReadWeightsFromStream(std::istream &)=0
TMVA::DNN::ERegularization
ERegularization
Enum representing the regularization type applied for a given layer.
Definition: Functions.h:65
TMVA::MethodDNN::HasAnalysisType
virtual Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets)
TMVA::Ranking
Ranking for variables in method (implementation)
Definition: Ranking.h:48
TMVA::MethodDNN::GetHelpMessage
void GetHelpMessage() const
Definition: MethodDNN.cxx:1477
TMVA::MethodDNN::MethodDNN
MethodDNN(DataSetInfo &theData, const TString &theWeightFile)
TMVA::MethodDNN::TrainGpu
void TrainGpu()
Definition: MethodDNN.cxx:908
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:162
TMVA::MethodDNN::ProcessOptions
void ProcessOptions()
Definition: MethodDNN.cxx:424
TMVA::MethodDNN::TTrainingSettings::regularization
DNN::ERegularization regularization
Definition: MethodDNN.h:94
TMVA::MethodDNN::TTrainingSettings::convergenceSteps
size_t convergenceSteps
Definition: MethodDNN.h:93
TMVA::MethodDNN::TTrainingSettings::dropoutProbabilities
std::vector< Double_t > dropoutProbabilities
Definition: MethodDNN.h:98
TMVA::MethodDNN::Matrix_t
typename Architecture_t::Matrix_t Matrix_t
Definition: MethodDNN.h:82
TMVA::MethodDNN
Deep Neural Network Implementation.
Definition: MethodDNN.h:77
MethodBase.h
TMVA::MethodDNN::KeyValueVector_t
std::vector< std::map< TString, TString > > KeyValueVector_t
Definition: MethodDNN.h:87
TTree.h
TString
Basic string class.
Definition: TString.h:136
TMatrixT< Double_t >
TMVA::MethodDNN::ReadWeightsFromXML
void ReadWeightsFromXML(void *wghtnode)
Definition: MethodDNN.cxx:1401
TString.h
TMVA::MethodDNN::fArchitectureString
TString fArchitectureString
Definition: MethodDNN.h:119
bool
TMVA::Tools::xmlengine
TXMLEngine & xmlengine()
Definition: Tools.h:268
Cuda.h
TMVA::MethodDNN::LayoutVector_t
std::vector< std::pair< int, DNN::EActivationFunction > > LayoutVector_t
Definition: MethodDNN.h:86
TMVA::MethodDNN::Init
void Init()
TMVA::MethodDNN::TTrainingSettings::multithreading
bool multithreading
Definition: MethodDNN.h:99
NeuralNet.h
TMVA::DataSetInfo
Class that contains all the data information.
Definition: DataSetInfo.h:62
TMVA::MethodDNN::ParseKeyValueString
KeyValueVector_t ParseKeyValueString(TString parseString, TString blockDelim, TString tokenDelim)
TMVA::MethodDNN::fLayout
LayoutVector_t fLayout
Definition: MethodDNN.h:121
TMVA::MethodDNN::fErrorStrategy
TString fErrorStrategy
Definition: MethodDNN.h:116
TMVA::Types::EAnalysisType
EAnalysisType
Definition: Types.h:128
TMVA::DNN::TNet< Architecture_t >
TMVA::Tools::ReadAttr
void ReadAttr(void *node, const char *, T &value)
read attribute from xml
Definition: Tools.h:335
TMVA::MethodDNN::GetNumValidationSamples
UInt_t GetNumValidationSamples()
TRandom3.h
Net.h
TMVA::MethodDNN::ReadMatrixXML
static void ReadMatrixXML(void *xml, const char *name, TMatrixT< Double_t > &X)
Definition: MethodDNN.h:201
TMVA::MethodDNN::fSettings
KeyValueVector_t fSettings
Definition: MethodDNN.h:125
TMVA::MethodBase
Virtual base Class for all MVA method.
Definition: MethodBase.h:111
TMVA::MethodDNN::WriteMatrixXML
static void WriteMatrixXML(void *parent, const char *name, const TMatrixT< Double_t > &X)
Definition: MethodDNN.h:178
TMVA::MethodDNN::TTrainingSettings::momentum
Double_t momentum
Definition: MethodDNN.h:96
TMVA::MethodDNN::fTrainingStrategyString
TString fTrainingStrategyString
Definition: MethodDNN.h:117
TMVA::MethodDNN::fWeightInitializationString
TString fWeightInitializationString
Definition: MethodDNN.h:118
unsigned int
TMVA::MethodDNN::CreateRanking
const Ranking * CreateRanking()
Definition: MethodDNN.cxx:1459
TMVA::MethodDNN::fWeightInitialization
DNN::EInitialization fWeightInitialization
Definition: MethodDNN.h:112
TMVA::MethodDNN::fValidationSize
TString fValidationSize
Definition: MethodDNN.h:120
TMVA::DNN::TReference::Scalar_t
AReal Scalar_t
Definition: Reference.h:57
TMVA::MethodDNN::fNet
Net_t fNet
Definition: MethodDNN.h:111
TMVA::DNN::EInitialization
EInitialization
Definition: Functions.h:72
TMVA::MethodDNN::TrainCpu
void TrainCpu()
Definition: MethodDNN.cxx:1098
TMVA::MethodDNN::fResume
bool fResume
Definition: MethodDNN.h:123
TMVA::MethodDNN::fLayoutString
TString fLayoutString
Definition: MethodDNN.h:115
TMVA::MethodDNN::~MethodDNN
virtual ~MethodDNN()
Double_t
double Double_t
Definition: RtypesCore.h:59
TMVA::MethodDNN::TTrainingSettings::learningRate
Double_t learningRate
Definition: MethodDNN.h:95
TMVA::MethodDNN::TTrainingSettings::weightDecay
Double_t weightDecay
Definition: MethodDNN.h:97
Reference.h
TMVA::MethodDNN::DeclareOptions
void DeclareOptions()
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TMVA::MethodDNN::TTrainingSettings
Definition: MethodDNN.h:90
name
char name[80]
Definition: TGX11.cxx:110
TMVA::MethodDNN::GetMulticlassValues
virtual const std::vector< Float_t > & GetMulticlassValues()
Definition: MethodDNN.cxx:1352
TMVA::MethodDNN::Train
void Train()
Definition: MethodDNN.cxx:665
TMVA::MethodDNN::AddWeightsXMLTo
void AddWeightsXMLTo(void *parent) const
Definition: MethodDNN.cxx:1375
TMVA::MethodDNN::GetMvaValue
virtual Double_t GetMvaValue(Double_t *err=0, Double_t *errUpper=0)
Definition: MethodDNN.cxx:1297
TMVA::MethodDNN::ParseLayoutString
LayoutVector_t ParseLayoutString(TString layerSpec)
TMVA::MethodDNN::Scalar_t
typename Architecture_t::Scalar_t Scalar_t
Definition: MethodDNN.h:83
Tools.h
type
int type
Definition: TGX11.cxx:121
TMVA::gTools
Tools & gTools()
TMVA::MethodDNN::fTrainingSettings
std::vector< TTrainingSettings > fTrainingSettings
Definition: MethodDNN.h:122
TMVA::MethodDNN::MethodDNN
MethodDNN(const TString &jobName, const TString &methodTitle, DataSetInfo &theData, const TString &theOption)
TXMLEngine::GetNodeContent
const char * GetNodeContent(XMLNodePointer_t xmlnode)
get contents (if any) of xmlnode
Definition: TXMLEngine.cxx:1083
TMVA::DNN::TReference::Matrix_t
TMatrixT< AReal > Matrix_t
Definition: Reference.h:58
TMVA::MethodDNN::TTrainingSettings::batchSize
size_t batchSize
Definition: MethodDNN.h:91
TMVA::MethodDNN::MakeClassSpecific
void MakeClassSpecific(std::ostream &, const TString &) const
Definition: MethodDNN.cxx:1470
TMVA
create variable transformations
Definition: GeneticMinimizer.h:22
TXMLEngine::AddRawLine
Bool_t AddRawLine(XMLNodePointer_t parent, const char *line)
Add just line into xml file Line should has correct xml syntax that later it can be decoded by xml pa...
Definition: TXMLEngine.cxx:915