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 "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
70namespace TMVA {
71
72class MethodDNN : public MethodBase
73{
75
80
81private:
82 using LayoutVector_t = std::vector<std::pair<int, DNN::EActivationFunction>>;
83 using KeyValueVector_t = std::vector<std::map<TString, TString>>;
84
86 {
87 size_t batchSize;
94 std::vector<Double_t> dropoutProbabilities;
96 };
97
98 // the option handling methods
100 void ProcessOptions();
101
103
104 // general helper functions
105 void Init();
106
110
118 std::vector<TTrainingSettings> fTrainingSettings;
120
122
123 ClassDef(MethodDNN,0); // neural network
124
125 static inline void WriteMatrixXML(void *parent, const char *name,
126 const TMatrixT<Double_t> &X);
127 static inline void ReadMatrixXML(void *xml, const char *name,
129protected:
130
131 void MakeClassSpecific( std::ostream&, const TString& ) const;
132 void GetHelpMessage() const;
133
134public:
135
136 // Standard Constructors
137 MethodDNN(const TString& jobName,
138 const TString& methodTitle,
139 DataSetInfo& theData,
140 const TString& theOption);
142 const TString& theWeightFile);
143 virtual ~MethodDNN();
144
146 UInt_t numberClasses,
147 UInt_t numberTargets );
150 TString blockDelim,
151 TString tokenDelim);
152 void Train();
153 void TrainGpu();
154 void TrainCpu();
155
156 virtual Double_t GetMvaValue( Double_t* err=0, Double_t* errUpper=0 );
157 virtual const std::vector<Float_t>& GetRegressionValues();
158 virtual const std::vector<Float_t>& GetMulticlassValues();
159
161
162 // write weights to stream
163 void AddWeightsXMLTo ( void* parent ) const;
164
165 // read weights from stream
166 void ReadWeightsFromStream( std::istream & i );
167 void ReadWeightsFromXML ( void* wghtnode );
168
169 // ranking of input variables
170 const Ranking* CreateRanking();
171
172};
173
174inline void MethodDNN::WriteMatrixXML(void *parent,
175 const char *name,
176 const TMatrixT<Double_t> &X)
177{
178 std::stringstream matrixStringStream("");
179 matrixStringStream.precision( 16 );
180
181 for (size_t i = 0; i < (size_t) X.GetNrows(); i++)
182 {
183 for (size_t j = 0; j < (size_t) X.GetNcols(); j++)
184 {
185 matrixStringStream << std::scientific << X(i,j) << " ";
186 }
187 }
188 std::string s = matrixStringStream.str();
189 void* matxml = gTools().xmlengine().NewChild(parent, 0, name);
190 gTools().xmlengine().NewAttr(matxml, 0, "rows",
191 gTools().StringFromInt((int)X.GetNrows()));
192 gTools().xmlengine().NewAttr(matxml, 0, "cols",
193 gTools().StringFromInt((int)X.GetNcols()));
194 gTools().xmlengine().AddRawLine (matxml, s.c_str());
195}
196
197inline void MethodDNN::ReadMatrixXML(void *xml,
198 const char *name,
200{
201 void *matrixXML = gTools().GetChild(xml, name);
202 size_t rows, cols;
203 gTools().ReadAttr(matrixXML, "rows", rows);
204 gTools().ReadAttr(matrixXML, "cols", cols);
205
206 const char * matrixString = gTools().xmlengine().GetNodeContent(matrixXML);
207 std::stringstream matrixStringStream(matrixString);
208
209 for (size_t i = 0; i < rows; i++)
210 {
211 for (size_t j = 0; j < cols; j++)
212 {
213 matrixStringStream >> X(i,j);
214 }
215 }
216}
217} // namespace TMVA
218
219#endif
unsigned int UInt_t
Definition: RtypesCore.h:42
bool Bool_t
Definition: RtypesCore.h:59
double Double_t
Definition: RtypesCore.h:55
#define ClassDef(name, id)
Definition: Rtypes.h:326
char name[80]
Definition: TGX11.cxx:109
int type
Definition: TGX11.cxx:120
Generic neural network class.
Definition: Net.h:49
The reference architecture class.
Definition: Reference.h:50
TMatrixT< AReal > Matrix_t
Definition: Reference.h:56
Class that contains all the data information.
Definition: DataSetInfo.h:60
Virtual base Class for all MVA method.
Definition: MethodBase.h:111
virtual void ReadWeightsFromStream(std::istream &)=0
Deep Neural Network Implementation.
Definition: MethodDNN.h:73
TString fLayoutString
Definition: MethodDNN.h:111
virtual Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets)
typename Architecture_t::Scalar_t Scalar_t
Definition: MethodDNN.h:79
virtual const std::vector< Float_t > & GetMulticlassValues()
Definition: MethodDNN.cxx:1339
UInt_t GetNumValidationSamples()
void ReadWeightsFromXML(void *wghtnode)
Definition: MethodDNN.cxx:1388
std::vector< std::map< TString, TString > > KeyValueVector_t
Definition: MethodDNN.h:83
typename Architecture_t::Matrix_t Matrix_t
Definition: MethodDNN.h:78
TString fTrainingStrategyString
Definition: MethodDNN.h:113
KeyValueVector_t fSettings
Definition: MethodDNN.h:121
void ReadWeightsFromStream(std::istream &i)
Definition: MethodDNN.cxx:1440
LayoutVector_t ParseLayoutString(TString layerSpec)
static void WriteMatrixXML(void *parent, const char *name, const TMatrixT< Double_t > &X)
Definition: MethodDNN.h:174
MethodDNN(DataSetInfo &theData, const TString &theWeightFile)
void MakeClassSpecific(std::ostream &, const TString &) const
Definition: MethodDNN.cxx:1457
MethodDNN(const TString &jobName, const TString &methodTitle, DataSetInfo &theData, const TString &theOption)
void ProcessOptions()
Definition: MethodDNN.cxx:412
virtual ~MethodDNN()
LayoutVector_t fLayout
Definition: MethodDNN.h:117
TString fValidationSize
Definition: MethodDNN.h:116
TString fWeightInitializationString
Definition: MethodDNN.h:114
std::vector< std::pair< int, DNN::EActivationFunction > > LayoutVector_t
Definition: MethodDNN.h:82
DNN::EInitialization fWeightInitialization
Definition: MethodDNN.h:108
friend struct TestMethodDNNValidationSize
Definition: MethodDNN.h:74
TString fErrorStrategy
Definition: MethodDNN.h:112
std::vector< TTrainingSettings > fTrainingSettings
Definition: MethodDNN.h:118
void DeclareOptions()
TString fArchitectureString
Definition: MethodDNN.h:115
virtual Double_t GetMvaValue(Double_t *err=0, Double_t *errUpper=0)
Definition: MethodDNN.cxx:1284
const Ranking * CreateRanking()
Definition: MethodDNN.cxx:1446
KeyValueVector_t ParseKeyValueString(TString parseString, TString blockDelim, TString tokenDelim)
DNN::EOutputFunction fOutputFunction
Definition: MethodDNN.h:109
void AddWeightsXMLTo(void *parent) const
Definition: MethodDNN.cxx:1362
void GetHelpMessage() const
Definition: MethodDNN.cxx:1464
static void ReadMatrixXML(void *xml, const char *name, TMatrixT< Double_t > &X)
Definition: MethodDNN.h:197
virtual const std::vector< Float_t > & GetRegressionValues()
Definition: MethodDNN.cxx:1301
Ranking for variables in method (implementation)
Definition: Ranking.h:48
void * GetChild(void *parent, const char *childname=0)
get child node
Definition: Tools.cxx:1162
TXMLEngine & xmlengine()
Definition: Tools.h:270
void ReadAttr(void *node, const char *, T &value)
read attribute from xml
Definition: Tools.h:337
EAnalysisType
Definition: Types.h:127
Int_t GetNrows() const
Definition: TMatrixTBase.h:124
Int_t GetNcols() const
Definition: TMatrixTBase.h:127
Basic string class.
Definition: TString.h:131
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:909
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:709
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:580
const char * GetNodeContent(XMLNodePointer_t xmlnode)
get contents (if any) of xmlnode
static constexpr double s
EInitialization
Definition: Functions.h:70
EOutputFunction
Enum that represents output functions.
Definition: Functions.h:44
ERegularization
Enum representing the regularization type applied for a given layer.
Definition: Functions.h:63
create variable transformations
Tools & gTools()
DNN::ERegularization regularization
Definition: MethodDNN.h:90
std::vector< Double_t > dropoutProbabilities
Definition: MethodDNN.h:94