Logo ROOT   6.10/09
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 "TString.h"
43 #include "TTree.h"
44 #include "TRandom3.h"
45 #include "TH1F.h"
46 #include "TMVA/MethodBase.h"
47 #include "TMVA/NeuralNet.h"
48 
49 #include "TMVA/Tools.h"
50 
51 #include "TMVA/DNN/Net.h"
52 #include "TMVA/DNN/Minimizers.h"
54 
55 #ifdef DNNCPU
57 #endif
58 
59 #ifdef DNNCUDA
61 #endif
62 
63 namespace TMVA {
64 
65 class MethodDNN : public MethodBase
66 {
70 
71 private:
72  using LayoutVector_t = std::vector<std::pair<int, DNN::EActivationFunction>>;
73  using KeyValueVector_t = std::vector<std::map<TString, TString>>;
74 
76  {
77  size_t batchSize;
78  size_t testInterval;
84  std::vector<Double_t> dropoutProbabilities;
86  };
87 
88  // the option handling methods
89  void DeclareOptions();
90  void ProcessOptions();
91 
92  // general helper functions
93  void Init();
94 
98 
105  std::vector<TTrainingSettings> fTrainingSettings;
106  bool fResume;
107 
109 
110  ClassDef(MethodDNN,0); // neural network
111 
112  static inline void WriteMatrixXML(void *parent, const char *name,
113  const TMatrixT<Double_t> &X);
114  static inline void ReadMatrixXML(void *xml, const char *name,
116 protected:
117 
118  void MakeClassSpecific( std::ostream&, const TString& ) const;
119  void GetHelpMessage() const;
120 
121 public:
122 
123  // Standard Constructors
124  MethodDNN(const TString& jobName,
125  const TString& methodTitle,
126  DataSetInfo& theData,
127  const TString& theOption);
128  MethodDNN(DataSetInfo& theData,
129  const TString& theWeightFile);
130  virtual ~MethodDNN();
131 
133  UInt_t numberClasses,
134  UInt_t numberTargets );
137  TString blockDelim,
138  TString tokenDelim);
139  void Train();
140  void TrainGpu();
141  void TrainCpu();
142 
143  virtual Double_t GetMvaValue( Double_t* err=0, Double_t* errUpper=0 );
144  virtual const std::vector<Float_t>& GetRegressionValues();
145  virtual const std::vector<Float_t>& GetMulticlassValues();
146 
148 
149  // write weights to stream
150  void AddWeightsXMLTo ( void* parent ) const;
151 
152  // read weights from stream
153  void ReadWeightsFromStream( std::istream & i );
154  void ReadWeightsFromXML ( void* wghtnode );
155 
156  // ranking of input variables
157  const Ranking* CreateRanking();
158 
159 };
160 
161 inline void MethodDNN::WriteMatrixXML(void *parent,
162  const char *name,
163  const TMatrixT<Double_t> &X)
164 {
165  std::stringstream matrixStringStream("");
166  matrixStringStream.precision( 16 );
167 
168  for (size_t i = 0; i < (size_t) X.GetNrows(); i++)
169  {
170  for (size_t j = 0; j < (size_t) X.GetNcols(); j++)
171  {
172  matrixStringStream << std::scientific << X(i,j) << " ";
173  }
174  }
175  std::string s = matrixStringStream.str();
176  void* matxml = gTools().xmlengine().NewChild(parent, 0, name);
177  gTools().xmlengine().NewAttr(matxml, 0, "rows",
178  gTools().StringFromInt((int)X.GetNrows()));
179  gTools().xmlengine().NewAttr(matxml, 0, "cols",
180  gTools().StringFromInt((int)X.GetNcols()));
181  gTools().xmlengine().AddRawLine (matxml, s.c_str());
182 }
183 
184 inline void MethodDNN::ReadMatrixXML(void *xml,
185  const char *name,
187 {
188  void *matrixXML = gTools().GetChild(xml, name);
189  size_t rows, cols;
190  gTools().ReadAttr(matrixXML, "rows", rows);
191  gTools().ReadAttr(matrixXML, "cols", cols);
192 
193  const char * matrixString = gTools().xmlengine().GetNodeContent(matrixXML);
194  std::stringstream matrixStringStream(matrixString);
195 
196  for (size_t i = 0; i < rows; i++)
197  {
198  for (size_t j = 0; j < cols; j++)
199  {
200  matrixStringStream >> X(i,j);
201  }
202  }
203 }
204 } // namespace TMVA
205 
206 #endif
void GetHelpMessage() const
Definition: MethodDNN.cxx:1331
TXMLEngine & xmlengine()
Definition: Tools.h:270
LayoutVector_t ParseLayoutString(TString layerSpec)
DNN::EOutputFunction fOutputFunction
Definition: MethodDNN.h:97
void ProcessOptions()
Definition: MethodDNN.cxx:411
std::vector< std::map< TString, TString > > KeyValueVector_t
Definition: MethodDNN.h:73
Int_t GetNcols() const
Definition: TMatrixTBase.h:125
void MakeClassSpecific(std::ostream &, const TString &) const
Definition: MethodDNN.cxx:1324
EAnalysisType
Definition: Types.h:125
Virtual base Class for all MVA method.
Definition: MethodBase.h:106
Basic string class.
Definition: TString.h:129
typename Architecture_t::Matrix_t Matrix_t
Definition: MethodDNN.h:69
Ranking for variables in method (implementation)
Definition: Ranking.h:48
bool Bool_t
Definition: RtypesCore.h:59
virtual Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets)
LayoutVector_t fLayout
Definition: MethodDNN.h:104
const char * GetNodeContent(XMLNodePointer_t xmlnode)
get contents (if any) of xml node
Definition: TXMLEngine.cxx:938
TMatrixT.
Definition: TMatrixDfwd.h:22
EInitialization
Definition: Functions.h:70
void ReadWeightsFromStream(std::istream &i)
Definition: MethodDNN.cxx:1307
#define ClassDef(name, id)
Definition: Rtypes.h:297
The reference architecture class.
Definition: Reference.h:37
std::vector< std::pair< int, DNN::EActivationFunction > > LayoutVector_t
Definition: MethodDNN.h:72
std::vector< Double_t > dropoutProbabilities
Definition: MethodDNN.h:84
void ReadWeightsFromXML(void *wghtnode)
Definition: MethodDNN.cxx:1255
void * GetChild(void *parent, const char *childname=0)
get child node
Definition: Tools.cxx:1161
TString fErrorStrategy
Definition: MethodDNN.h:100
TString fArchitectureString
Definition: MethodDNN.h:103
Class that contains all the data information.
Definition: DataSetInfo.h:60
static void ReadMatrixXML(void *xml, const char *name, TMatrixT< Double_t > &X)
Definition: MethodDNN.h:184
KeyValueVector_t fSettings
Definition: MethodDNN.h:108
KeyValueVector_t ParseKeyValueString(TString parseString, TString blockDelim, TString tokenDelim)
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:769
TString fLayoutString
Definition: MethodDNN.h:99
const Ranking * CreateRanking()
Definition: MethodDNN.cxx:1313
unsigned int UInt_t
Definition: RtypesCore.h:42
static void WriteMatrixXML(void *parent, const char *name, const TMatrixT< Double_t > &X)
Definition: MethodDNN.h:161
void ReadAttr(void *node, const char *, T &value)
read attribute from xml
Definition: Tools.h:290
Tools & gTools()
DNN::EInitialization fWeightInitialization
Definition: MethodDNN.h:96
Int_t GetNrows() const
Definition: TMatrixTBase.h:122
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:488
double Double_t
Definition: RtypesCore.h:55
Deep Neural Network Implementation.
Definition: MethodDNN.h:65
EOutputFunction
Enum that represents output functions.
Definition: Functions.h:43
int type
Definition: TGX11.cxx:120
DNN::ERegularization regularization
Definition: MethodDNN.h:80
TString fTrainingStrategyString
Definition: MethodDNN.h:101
TString fWeightInitializationString
Definition: MethodDNN.h:102
void DeclareOptions()
std::vector< TTrainingSettings > fTrainingSettings
Definition: MethodDNN.h:105
virtual ~MethodDNN()
Abstract ClassifierFactory template that handles arbitrary types.
virtual Double_t GetMvaValue(Double_t *err=0, Double_t *errUpper=0)
Definition: MethodDNN.cxx:1151
MethodDNN(const TString &jobName, const TString &methodTitle, DataSetInfo &theData, const TString &theOption)
XMLNodePointer_t NewChild(XMLNodePointer_t parent, XMLNsPointer_t ns, const char *name, const char *content=0)
create new child element for parent node
Definition: TXMLEngine.cxx:614
ERegularization
Enum representing the regularization type applied for a given layer.
Definition: Functions.h:62
void AddWeightsXMLTo(void *parent) const
Definition: MethodDNN.cxx:1229
virtual void ReadWeightsFromStream(std::istream &)=0
virtual const std::vector< Float_t > & GetMulticlassValues()
Definition: MethodDNN.cxx:1206
virtual const std::vector< Float_t > & GetRegressionValues()
Definition: MethodDNN.cxx:1168