Logo ROOT   6.14/05
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 R__HAS_TMVACPU
56 #define DNNCPU
57 #endif
58 #ifdef R__HAS_TMVAGPU
59 #define DNNCUDA
60 #endif
61 
62 #ifdef DNNCPU
64 #endif
65 
66 #ifdef DNNCUDA
68 #endif
69 
70 namespace TMVA {
71 
72 class MethodDNN : public MethodBase
73 {
75 
79 
80 private:
81  using LayoutVector_t = std::vector<std::pair<int, DNN::EActivationFunction>>;
82  using KeyValueVector_t = std::vector<std::map<TString, TString>>;
83 
85  {
86  size_t batchSize;
87  size_t testInterval;
93  std::vector<Double_t> dropoutProbabilities;
95  };
96 
97  // the option handling methods
98  void DeclareOptions();
99  void ProcessOptions();
100 
102 
103  // general helper functions
104  void Init();
105 
109 
117  std::vector<TTrainingSettings> fTrainingSettings;
118  bool fResume;
119 
121 
122  ClassDef(MethodDNN,0); // neural network
123 
124  static inline void WriteMatrixXML(void *parent, const char *name,
125  const TMatrixT<Double_t> &X);
126  static inline void ReadMatrixXML(void *xml, const char *name,
127  TMatrixT<Double_t> &X);
128 protected:
129 
130  void MakeClassSpecific( std::ostream&, const TString& ) const;
131  void GetHelpMessage() const;
132 
133 public:
134 
135  // Standard Constructors
136  MethodDNN(const TString& jobName,
137  const TString& methodTitle,
138  DataSetInfo& theData,
139  const TString& theOption);
140  MethodDNN(DataSetInfo& theData,
141  const TString& theWeightFile);
142  virtual ~MethodDNN();
143 
145  UInt_t numberClasses,
146  UInt_t numberTargets );
149  TString blockDelim,
150  TString tokenDelim);
151  void Train();
152  void TrainGpu();
153  void TrainCpu();
154 
155  virtual Double_t GetMvaValue( Double_t* err=0, Double_t* errUpper=0 );
156  virtual const std::vector<Float_t>& GetRegressionValues();
157  virtual const std::vector<Float_t>& GetMulticlassValues();
158 
160 
161  // write weights to stream
162  void AddWeightsXMLTo ( void* parent ) const;
163 
164  // read weights from stream
165  void ReadWeightsFromStream( std::istream & i );
166  void ReadWeightsFromXML ( void* wghtnode );
167 
168  // ranking of input variables
169  const Ranking* CreateRanking();
170 
171 };
172 
173 inline void MethodDNN::WriteMatrixXML(void *parent,
174  const char *name,
175  const TMatrixT<Double_t> &X)
176 {
177  std::stringstream matrixStringStream("");
178  matrixStringStream.precision( 16 );
179 
180  for (size_t i = 0; i < (size_t) X.GetNrows(); i++)
181  {
182  for (size_t j = 0; j < (size_t) X.GetNcols(); j++)
183  {
184  matrixStringStream << std::scientific << X(i,j) << " ";
185  }
186  }
187  std::string s = matrixStringStream.str();
188  void* matxml = gTools().xmlengine().NewChild(parent, 0, name);
189  gTools().xmlengine().NewAttr(matxml, 0, "rows",
190  gTools().StringFromInt((int)X.GetNrows()));
191  gTools().xmlengine().NewAttr(matxml, 0, "cols",
192  gTools().StringFromInt((int)X.GetNcols()));
193  gTools().xmlengine().AddRawLine (matxml, s.c_str());
194 }
195 
196 inline void MethodDNN::ReadMatrixXML(void *xml,
197  const char *name,
199 {
200  void *matrixXML = gTools().GetChild(xml, name);
201  size_t rows, cols;
202  gTools().ReadAttr(matrixXML, "rows", rows);
203  gTools().ReadAttr(matrixXML, "cols", cols);
204 
205  const char * matrixString = gTools().xmlengine().GetNodeContent(matrixXML);
206  std::stringstream matrixStringStream(matrixString);
207 
208  for (size_t i = 0; i < rows; i++)
209  {
210  for (size_t j = 0; j < cols; j++)
211  {
212  matrixStringStream >> X(i,j);
213  }
214  }
215 }
216 } // namespace TMVA
217 
218 #endif
void GetHelpMessage() const
Definition: MethodDNN.cxx:1436
TXMLEngine & xmlengine()
Definition: Tools.h:270
LayoutVector_t ParseLayoutString(TString layerSpec)
DNN::EOutputFunction fOutputFunction
Definition: MethodDNN.h:108
void ProcessOptions()
Definition: MethodDNN.cxx:412
std::vector< std::map< TString, TString > > KeyValueVector_t
Definition: MethodDNN.h:82
Int_t GetNcols() const
Definition: TMatrixTBase.h:125
void MakeClassSpecific(std::ostream &, const TString &) const
Definition: MethodDNN.cxx:1429
EAnalysisType
Definition: Types.h:127
Virtual base Class for all MVA method.
Definition: MethodBase.h:109
Basic string class.
Definition: TString.h:131
typename Architecture_t::Matrix_t Matrix_t
Definition: MethodDNN.h:78
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)
friend struct TestMethodDNNValidationSize
Definition: MethodDNN.h:74
LayoutVector_t fLayout
Definition: MethodDNN.h:116
const char * GetNodeContent(XMLNodePointer_t xmlnode)
get contents (if any) of xmlnode
EInitialization
Definition: Functions.h:70
void ReadWeightsFromStream(std::istream &i)
Definition: MethodDNN.cxx:1412
#define ClassDef(name, id)
Definition: Rtypes.h:320
The reference architecture class.
Definition: DataLoader.h:30
std::vector< std::pair< int, DNN::EActivationFunction > > LayoutVector_t
Definition: MethodDNN.h:81
std::vector< Double_t > dropoutProbabilities
Definition: MethodDNN.h:93
void ReadWeightsFromXML(void *wghtnode)
Definition: MethodDNN.cxx:1360
void * GetChild(void *parent, const char *childname=0)
get child node
Definition: Tools.cxx:1162
TString fErrorStrategy
Definition: MethodDNN.h:111
TString fArchitectureString
Definition: MethodDNN.h:114
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:196
KeyValueVector_t fSettings
Definition: MethodDNN.h:120
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:907
TString fLayoutString
Definition: MethodDNN.h:110
const Ranking * CreateRanking()
Definition: MethodDNN.cxx:1418
unsigned int UInt_t
Definition: RtypesCore.h:42
static void WriteMatrixXML(void *parent, const char *name, const TMatrixT< Double_t > &X)
Definition: MethodDNN.h:173
void ReadAttr(void *node, const char *, T &value)
read attribute from xml
Definition: Tools.h:335
Tools & gTools()
DNN::EInitialization fWeightInitialization
Definition: MethodDNN.h:107
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:578
UInt_t GetNumValidationSamples()
double Double_t
Definition: RtypesCore.h:55
Deep Neural Network Implementation.
Definition: MethodDNN.h:72
EOutputFunction
Enum that represents output functions.
Definition: Functions.h:43
int type
Definition: TGX11.cxx:120
static constexpr double s
DNN::ERegularization regularization
Definition: MethodDNN.h:89
TString fTrainingStrategyString
Definition: MethodDNN.h:112
TString fWeightInitializationString
Definition: MethodDNN.h:113
void DeclareOptions()
std::vector< TTrainingSettings > fTrainingSettings
Definition: MethodDNN.h:117
virtual ~MethodDNN()
Abstract ClassifierFactory template that handles arbitrary types.
virtual Double_t GetMvaValue(Double_t *err=0, Double_t *errUpper=0)
Definition: MethodDNN.cxx:1256
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:707
TString fValidationSize
Definition: MethodDNN.h:115
ERegularization
Enum representing the regularization type applied for a given layer.
Definition: Functions.h:62
void AddWeightsXMLTo(void *parent) const
Definition: MethodDNN.cxx:1334
virtual void ReadWeightsFromStream(std::istream &)=0
virtual const std::vector< Float_t > & GetMulticlassValues()
Definition: MethodDNN.cxx:1311
virtual const std::vector< Float_t > & GetRegressionValues()
Definition: MethodDNN.cxx:1273
char name[80]
Definition: TGX11.cxx:109