Logo ROOT   6.08/02
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
114  TString fLayoutString;
115  TString fErrorStrategy;
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  void TrainCpu();
157 
158  virtual Double_t GetMvaValue( Double_t* err=0, Double_t* errUpper=0 );
159  virtual const std::vector<Float_t>& GetRegressionValues();
160  virtual const std::vector<Float_t>& GetMulticlassValues();
161 
163 
164  // write weights to stream
165  void AddWeightsXMLTo ( void* parent ) const;
166 
167  // read weights from stream
168  void ReadWeightsFromStream( std::istream & i );
169  void ReadWeightsFromXML ( void* wghtnode );
170 
171  // ranking of input variables
172  const Ranking* CreateRanking();
173 
174 };
175 
176 inline void MethodDNN::WriteMatrixXML(void *parent,
177  const char *name,
178  const TMatrixT<Double_t> &X)
179 {
180  std::stringstream matrixStringStream("");
181  matrixStringStream.precision( 16 );
182 
183  for (size_t i = 0; i < (size_t) X.GetNrows(); i++)
184  {
185  for (size_t j = 0; j < (size_t) X.GetNcols(); j++)
186  {
187  matrixStringStream << std::scientific << X(i,j) << " ";
188  }
189  }
190  std::string s = matrixStringStream.str();
191  void* matxml = gTools().xmlengine().NewChild(parent, 0, name);
192  gTools().xmlengine().NewAttr(matxml, 0, "rows",
193  gTools().StringFromInt((int)X.GetNrows()));
194  gTools().xmlengine().NewAttr(matxml, 0, "cols",
195  gTools().StringFromInt((int)X.GetNcols()));
196  gTools().xmlengine().AddRawLine (matxml, s.c_str());
197 }
198 
199 inline void MethodDNN::ReadMatrixXML(void *xml,
200  const char *name,
202 {
203  void *matrixXML = gTools().GetChild(xml, name);
204  size_t rows, cols;
205  gTools().ReadAttr(matrixXML, "rows", rows);
206  gTools().ReadAttr(matrixXML, "cols", cols);
207 
208  const char * matrixString = gTools().xmlengine().GetNodeContent(matrixXML);
209  std::stringstream matrixStringStream(matrixString);
210 
211  for (size_t i = 0; i < rows; i++)
212  {
213  for (size_t j = 0; j < cols; j++)
214  {
215  matrixStringStream >> X(i,j);
216  }
217  }
218 }
219 } // namespace TMVA
220 
221 #endif
TXMLEngine & xmlengine()
Definition: Tools.h:278
EOutputFunction fOutputFunction
Definition: MethodDNN.h:112
EAnalysisType
Definition: Types.h:129
typename Architecture_t::Matrix_t Matrix_t
Definition: MethodDNN.h:83
bool Bool_t
Definition: RtypesCore.h:59
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:70
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:199
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:176
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
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
virtual void ReadWeightsFromStream(std::istream &)=0
char name[80]
Definition: TGX11.cxx:109