Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 * *
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 * (see tmva/doc/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 <map>
43#include <string>
44#include <sstream>
45
46#include "TString.h"
47#include "TTree.h"
48#include "TRandom3.h"
49#include "TH1F.h"
50#include "TMVA/MethodBase.h"
51#include "TMVA/NeuralNet.h"
52
53#include "TMVA/Tools.h"
54
55#include "TMVA/DNN/Net.h"
56#include "TMVA/DNN/Minimizers.h"
58
59#ifdef R__HAS_TMVACPU
60#define DNNCPU
61#endif
62#ifdef R__HAS_TMVAGPU
63//#define DNNCUDA
64#endif
65
66#ifdef DNNCPU
68#endif
69
70#ifdef DNNCUDA
72#endif
73
74namespace TMVA {
75
76class R__DEPRECATED(6, 46,
77 "Please train neural networks with a dedicated machine learning library instead, and use SOFIE "
78 "for inference in C++.") MethodDNN : public MethodBase
79{
80 friend struct TestMethodDNNValidationSize;
81
82 using Architecture_t = DNN::TReference<Float_t>;
83 using Net_t = DNN::TNet<Architecture_t>;
84 using Matrix_t = typename Architecture_t::Matrix_t;
85 using Scalar_t = typename Architecture_t::Scalar_t;
86
87private:
88 using LayoutVector_t = std::vector<std::pair<int, DNN::EActivationFunction>>;
89 using KeyValueVector_t = std::vector<std::map<TString, TString>>;
90
91 struct TTrainingSettings
92 {
93 size_t batchSize;
94 size_t testInterval;
95 size_t convergenceSteps;
96 DNN::ERegularization regularization;
97 Double_t learningRate;
98 Double_t momentum;
100 std::vector<Double_t> dropoutProbabilities;
101 bool multithreading;
102 };
103
104 // the option handling methods
105 void DeclareOptions() override;
106 void ProcessOptions() override;
107
108 UInt_t GetNumValidationSamples();
109
110 // general helper functions
111 void Init() override;
112
113 Net_t fNet;
114 DNN::EInitialization fWeightInitialization;
115 DNN::EOutputFunction fOutputFunction;
116
117 TString fLayoutString;
118 TString fErrorStrategy;
119 TString fTrainingStrategyString;
120 TString fWeightInitializationString;
121 TString fArchitectureString;
122 TString fValidationSize;
123 LayoutVector_t fLayout;
124 std::vector<TTrainingSettings> fTrainingSettings;
125 bool fResume;
126
127 KeyValueVector_t fSettings;
128
129 ClassDefOverride(MethodDNN,0); // neural network
130
131 static inline void WriteMatrixXML(void *parent, const char *name,
132 const TMatrixT<Double_t> &X);
133 static inline void ReadMatrixXML(void *xml, const char *name,
135protected:
136
137 void MakeClassSpecific( std::ostream&, const TString& ) const override;
138 void GetHelpMessage() const override;
139
140public:
141
142 // Standard Constructors
143 MethodDNN(const TString& jobName,
144 const TString& methodTitle,
145 DataSetInfo& theData,
146 const TString& theOption);
147 MethodDNN(DataSetInfo& theData,
148 const TString& theWeightFile);
149 virtual ~MethodDNN();
150
151 Bool_t HasAnalysisType(Types::EAnalysisType type,
152 UInt_t numberClasses,
153 UInt_t numberTargets ) override;
154 LayoutVector_t ParseLayoutString(TString layerSpec);
155 KeyValueVector_t ParseKeyValueString(TString parseString,
156 TString blockDelim,
157 TString tokenDelim);
158 void Train() override;
159 void TrainGpu();
160 void TrainCpu();
161
162 Double_t GetMvaValue( Double_t* err = nullptr, Double_t* errUpper = nullptr ) override;
163 const std::vector<Float_t>& GetRegressionValues() override;
164 const std::vector<Float_t>& GetMulticlassValues() override;
165
166 using MethodBase::ReadWeightsFromStream;
167
168 // write weights to stream
169 void AddWeightsXMLTo ( void* parent ) const override;
170
171 // read weights from stream
172 void ReadWeightsFromStream( std::istream & i ) override;
173 void ReadWeightsFromXML ( void* wghtnode ) override;
174
175 // ranking of input variables
176 const Ranking* CreateRanking() override;
177
178};
179
180inline void MethodDNN::WriteMatrixXML(void *parent,
181 const char *name,
182 const TMatrixT<Double_t> &X)
183{
184 std::stringstream matrixStringStream("");
185 matrixStringStream.precision( 16 );
186
187 for (size_t i = 0; i < (size_t) X.GetNrows(); i++)
188 {
189 for (size_t j = 0; j < (size_t) X.GetNcols(); j++)
190 {
191 matrixStringStream << std::scientific << X(i,j) << " ";
192 }
193 }
194 std::string s = matrixStringStream.str();
195 void* matxml = gTools().xmlengine().NewChild(parent, nullptr, name);
196 gTools().xmlengine().NewAttr(matxml, nullptr, "rows",
197 gTools().StringFromInt((int)X.GetNrows()));
198 gTools().xmlengine().NewAttr(matxml, nullptr, "cols",
199 gTools().StringFromInt((int)X.GetNcols()));
200 gTools().xmlengine().AddRawLine (matxml, s.c_str());
201}
202
203inline void MethodDNN::ReadMatrixXML(void *xml,
204 const char *name,
206{
207 void *matrixXML = gTools().GetChild(xml, name);
208 size_t rows, cols;
209 gTools().ReadAttr(matrixXML, "rows", rows);
210 gTools().ReadAttr(matrixXML, "cols", cols);
211
212 const char * matrixString = gTools().xmlengine().GetNodeContent(matrixXML);
213 std::stringstream matrixStringStream(matrixString);
214
215 for (size_t i = 0; i < rows; i++)
216 {
217 for (size_t j = 0; j < cols; j++)
218 {
219 matrixStringStream >> X(i,j);
220 }
221 }
222}
223} // namespace TMVA
224
225#endif
#define R__DEPRECATED(MAJOR, MINOR, REASON)
Definition RConfig.hxx:486
bool Bool_t
Boolean (0=false, 1=true) (bool)
Definition RtypesCore.h:78
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:61
double Double_t
Double 8 bytes.
Definition RtypesCore.h:74
#define ClassDefOverride(name, id)
Definition Rtypes.h:347
#define X(type, name)
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
Definition TGX11.cxx:142
Deep Neural Network Implementation.
TXMLEngine & xmlengine()
Definition Tools.h:262
void ReadAttr(void *node, const char *, T &value)
read attribute from xml
Definition Tools.h:329
void * GetChild(void *parent, const char *childname=nullptr)
get child node
Definition Tools.cxx:1125
TMatrixT.
Definition TMatrixT.h:40
Basic string class.
Definition TString.h:137
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...
XMLNodePointer_t NewChild(XMLNodePointer_t parent, XMLNsPointer_t ns, const char *name, const char *content=nullptr)
create new child element for parent node
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
const char * GetNodeContent(XMLNodePointer_t xmlnode)
get contents (if any) of xmlnode
void Init(TClassEdit::TInterpreterLookupHelper *helper)
double weightDecay(double error, ItWeight itWeight, ItWeight itWeightEnd, double factorWeightDecay, EnumRegularization eRegularization)
compute the weight decay for regularization (L1 or L2)
auto regularization(const typename Architecture_t::Matrix_t &A, ERegularization R) -> decltype(Architecture_t::L1Regularization(A))
Evaluate the regularization functional for a given weight matrix.
Definition Functions.h:238
create variable transformations
Tools & gTools()