Logo ROOT   6.07/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 #ifndef ROOT_TString
43 #include "TString.h"
44 #endif
45 #ifndef ROOT_TTree
46 #include "TTree.h"
47 #endif
48 #ifndef ROOT_TRandom3
49 #include "TRandom3.h"
50 #endif
51 #ifndef ROOT_TH1F
52 #include "TH1F.h"
53 #endif
54 #ifndef ROOT_TMVA_MethodBase
55 #include "TMVA/MethodBase.h"
56 #endif
57 #ifndef TMVA_NEURAL_NET
58 #include "TMVA/NeuralNet.h"
59 #endif
60 
61 #include "TMVA/Tools.h"
62 
63 #include "TMVA/DNN/Net.h"
64 #include "TMVA/DNN/Minimizers.h"
66 
67 #ifdef DNNCPU
69 #endif
70 
71 #ifdef DNNCUDA
73 #endif
74 
75 using namespace TMVA::DNN;
76 
77 namespace TMVA {
78 
79 class MethodDNN : public MethodBase
80 {
84 
85 private:
86 
87  using LayoutVector_t = std::vector<std::pair<int, EActivationFunction>>;
88  using KeyValueVector_t = std::vector<std::map<TString, TString>>;
89 
91  {
92  size_t batchSize;
93  size_t testInterval;
99  std::vector<Double_t> dropoutProbabilities;
101  };
102 
103  // the option handling methods
104  void DeclareOptions();
105  void ProcessOptions();
106 
107  // general helper functions
108  void Init();
109 
113 
120  std::vector<TTrainingSettings> fTrainingSettings;
121  bool fResume;
122 
124 
125  ClassDef(MethodDNN,0); // neural network
126 
127  static inline void WriteMatrixXML(void *parent, const char *name,
128  const TMatrixT<Double_t> &X);
129  static inline void ReadMatrixXML(void *xml, const char *name,
131 protected:
132 
133  void MakeClassSpecific( std::ostream&, const TString& ) const;
134  void GetHelpMessage() const;
135 
136 public:
137 
138  // Standard Constructors
139  MethodDNN(const TString& jobName,
140  const TString& methodTitle,
141  DataSetInfo& theData,
142  const TString& theOption);
143  MethodDNN(DataSetInfo& theData,
144  const TString& theWeightFile);
145  virtual ~MethodDNN();
146 
147  virtual Bool_t HasAnalysisType(Types::EAnalysisType type,
148  UInt_t numberClasses,
149  UInt_t numberTargets );
150  LayoutVector_t ParseLayoutString(TString layerSpec);
151  KeyValueVector_t ParseKeyValueString(TString parseString,
152  TString blockDelim,
153  TString tokenDelim);
154  void Train();
155  void TrainGpu();
156  template <typename AFloat>
157  void TrainCpu();
158 
159  virtual Double_t GetMvaValue( Double_t* err=0, Double_t* errUpper=0 );
160  virtual const std::vector<Float_t>& GetRegressionValues();
161  virtual const std::vector<Float_t>& GetMulticlassValues();
162 
164 
165  // write weights to stream
166  void AddWeightsXMLTo ( void* parent ) const;
167 
168  // read weights from stream
169  void ReadWeightsFromStream( std::istream & i );
170  void ReadWeightsFromXML ( void* wghtnode );
171 
172  // ranking of input variables
173  const Ranking* CreateRanking();
174 
175 };
176 
177 inline void MethodDNN::WriteMatrixXML(void *parent,
178  const char *name,
179  const TMatrixT<Double_t> &X)
180 {
181  std::stringstream matrixStringStream("");
182  matrixStringStream.precision( 16 );
183 
184  for (size_t i = 0; i < (size_t) X.GetNrows(); i++)
185  {
186  for (size_t j = 0; j < (size_t) X.GetNcols(); j++)
187  {
188  matrixStringStream << std::scientific << X(i,j) << " ";
189  }
190  }
191  std::string s = matrixStringStream.str();
192  void* matxml = gTools().xmlengine().NewChild(parent, 0, name);
193  gTools().xmlengine().NewAttr(matxml, 0, "rows",
194  gTools().StringFromInt((int)X.GetNrows()));
195  gTools().xmlengine().NewAttr(matxml, 0, "cols",
196  gTools().StringFromInt((int)X.GetNcols()));
197  gTools().xmlengine().AddRawLine (matxml, s.c_str());
198 }
199 
200 inline void MethodDNN::ReadMatrixXML(void *xml,
201  const char *name,
203 {
204  void *matrixXML = gTools().GetChild(xml, name);
205  size_t rows, cols;
206  gTools().ReadAttr(matrixXML, "rows", rows);
207  gTools().ReadAttr(matrixXML, "cols", cols);
208 
209  const char * matrixString = gTools().xmlengine().GetNodeContent(matrixXML);
210  std::stringstream matrixStringStream(matrixString);
211 
212  for (size_t i = 0; i < rows; i++)
213  {
214  for (size_t j = 0; j < cols; j++)
215  {
216  matrixStringStream >> X(i,j);
217  }
218  }
219 }
220 } // namespace TMVA
221 
222 #endif
TXMLEngine & xmlengine()
Definition: Tools.h:278
EOutputFunction fOutputFunction
Definition: MethodDNN.h:112
EAnalysisType
Definition: Types.h:128
Basic string class.
Definition: TString.h:137
typename Architecture_t::Matrix_t Matrix_t
Definition: MethodDNN.h:83
bool Bool_t
Definition: RtypesCore.h:59
Definition: Blas.h:58
LayoutVector_t fLayout
Definition: MethodDNN.h:119
const char * GetNodeContent(XMLNodePointer_t xmlnode)
get contents (if any) of xml node
Definition: TXMLEngine.cxx:938
TMatrixT.
Definition: TMatrixDfwd.h:24
EInitialization
Definition: Functions.h:68
Tools & gTools()
Definition: Tools.cxx:79
std::vector< std::map< TString, TString >> KeyValueVector_t
Definition: MethodDNN.h:88
#define ClassDef(name, id)
Definition: Rtypes.h:254
The reference architecture class.
Definition: Reference.h:37
std::vector< Double_t > dropoutProbabilities
Definition: MethodDNN.h:99
void Init(TClassEdit::TInterpreterLookupHelper *helper)
Definition: TClassEdit.cxx:119
void * GetChild(void *parent, const char *childname=0)
get child node
Definition: Tools.cxx:1158
TString fErrorStrategy
Definition: MethodDNN.h:115
TString fArchitectureString
Definition: MethodDNN.h:118
static void ReadMatrixXML(void *xml, const char *name, TMatrixT< Double_t > &X)
Definition: MethodDNN.h:200
Int_t GetNrows() const
Definition: TMatrixTBase.h:134
EInitialization fWeightInitialization
Definition: MethodDNN.h:111
KeyValueVector_t fSettings
Definition: MethodDNN.h:123
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:114
unsigned int UInt_t
Definition: RtypesCore.h:42
static void WriteMatrixXML(void *parent, const char *name, const TMatrixT< Double_t > &X)
Definition: MethodDNN.h:177
void ReadAttr(void *node, const char *, T &value)
Definition: Tools.h:296
std::vector< std::pair< int, EActivationFunction >> LayoutVector_t
Definition: MethodDNN.h:87
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
EOutputFunction
Enum that represents output functions.
Definition: Functions.h:43
int type
Definition: TGX11.cxx:120
Int_t GetNcols() const
Definition: TMatrixTBase.h:137
TString fTrainingStrategyString
Definition: MethodDNN.h:116
TString fWeightInitializationString
Definition: MethodDNN.h:117
std::vector< TTrainingSettings > fTrainingSettings
Definition: MethodDNN.h:120
Abstract ClassifierFactory template that handles arbitrary types.
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:60
virtual void ReadWeightsFromStream(std::istream &)=0
char name[80]
Definition: TGX11.cxx:109