File indexing completed on 2025-01-30 10:22:52
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 #ifndef ROOT_TMVA_MethodANNBase
0035 #define ROOT_TMVA_MethodANNBase
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045 #include "TString.h"
0046 #include <vector>
0047 #include "TTree.h"
0048 #include "TObjArray.h"
0049 #include "TRandom3.h"
0050 #include "TMatrix.h"
0051
0052 #include "TMVA/MethodBase.h"
0053 #include "TMVA/TActivation.h"
0054 #include "TMVA/TNeuron.h"
0055 #include "TMVA/TNeuronInput.h"
0056
0057 class TH1;
0058 class TH1F;
0059
0060 namespace TMVA {
0061
0062 class MethodANNBase : public MethodBase {
0063
0064 public:
0065
0066
0067 MethodANNBase( const TString& jobName,
0068 Types::EMVA methodType,
0069 const TString& methodTitle,
0070 DataSetInfo& theData,
0071 const TString& theOption );
0072
0073 MethodANNBase( Types::EMVA methodType,
0074 DataSetInfo& theData,
0075 const TString& theWeightFile);
0076
0077 virtual ~MethodANNBase();
0078
0079
0080 void InitANNBase();
0081
0082
0083 void SetActivation(TActivation* activation) {
0084 if (fActivation != nullptr) delete fActivation;
0085 fActivation = activation;
0086 }
0087 void SetNeuronInputCalculator(TNeuronInput* inputCalculator) {
0088 if (fInputCalculator != nullptr) delete fInputCalculator;
0089 fInputCalculator = inputCalculator;
0090 }
0091
0092
0093 virtual void Train() = 0;
0094
0095
0096 virtual void PrintNetwork() const;
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106 template <typename WriteIterator>
0107 void GetLayerActivation (size_t layer, WriteIterator writeIterator);
0108
0109 using MethodBase::ReadWeightsFromStream;
0110
0111
0112 void AddWeightsXMLTo( void* parent ) const;
0113 void ReadWeightsFromXML( void* wghtnode );
0114
0115
0116 virtual void ReadWeightsFromStream( std::istream& istr );
0117
0118
0119 virtual Double_t GetMvaValue( Double_t* err = nullptr, Double_t* errUpper = nullptr );
0120
0121 virtual const std::vector<Float_t> &GetRegressionValues();
0122
0123 virtual const std::vector<Float_t> &GetMulticlassValues();
0124
0125
0126 virtual void WriteMonitoringHistosToFile() const;
0127
0128
0129 const Ranking* CreateRanking();
0130
0131
0132 virtual void DeclareOptions();
0133 virtual void ProcessOptions();
0134
0135 Bool_t Debug() const;
0136
0137 enum EEstimator { kMSE=0,kCE};
0138
0139 TObjArray* fNetwork;
0140
0141 protected:
0142
0143 virtual void MakeClassSpecific( std::ostream&, const TString& ) const;
0144
0145 std::vector<Int_t>* ParseLayoutString( TString layerSpec );
0146 virtual void BuildNetwork( std::vector<Int_t>* layout, std::vector<Double_t>* weights = nullptr,
0147 Bool_t fromFile = kFALSE );
0148 void ForceNetworkInputs( const Event* ev, Int_t ignoreIndex = -1 );
0149 Double_t GetNetworkOutput() { return GetOutputNeuron()->GetActivationValue(); }
0150
0151
0152 void PrintMessage( TString message, Bool_t force = kFALSE ) const;
0153 void ForceNetworkCalculations();
0154 void WaitForKeyboard();
0155
0156
0157 Int_t NumCycles() { return fNcycles; }
0158 TNeuron* GetInputNeuron (Int_t index) { return (TNeuron*)fInputLayer->At(index); }
0159 TNeuron* GetOutputNeuron(Int_t index = 0) { return fOutputNeurons.at(index); }
0160
0161
0162 TObjArray* fSynapses;
0163 TActivation* fActivation;
0164 TActivation* fOutput;
0165 TActivation* fIdentity;
0166 TRandom3* frgen;
0167 TNeuronInput* fInputCalculator;
0168
0169 std::vector<Int_t> fRegulatorIdx;
0170 std::vector<Double_t> fRegulators;
0171 EEstimator fEstimator;
0172 TString fEstimatorS;
0173
0174
0175 TH1F* fEstimatorHistTrain;
0176 TH1F* fEstimatorHistTest;
0177
0178
0179 void CreateWeightMonitoringHists( const TString& bulkname, std::vector<TH1*>* hv = nullptr ) const;
0180 std::vector<TH1*> fEpochMonHistS;
0181 std::vector<TH1*> fEpochMonHistB;
0182 std::vector<TH1*> fEpochMonHistW;
0183
0184
0185
0186 TMatrixD fInvHessian;
0187 bool fUseRegulator;
0188
0189 protected:
0190 Int_t fRandomSeed;
0191
0192 Int_t fNcycles;
0193
0194 TString fNeuronType;
0195 TString fNeuronInputType;
0196
0197
0198 private:
0199
0200
0201 void BuildLayers(std::vector<Int_t>* layout, Bool_t from_file = false);
0202 void BuildLayer(Int_t numNeurons, TObjArray* curLayer, TObjArray* prevLayer,
0203 Int_t layerIndex, Int_t numLayers, Bool_t from_file = false);
0204 void AddPreLinks(TNeuron* neuron, TObjArray* prevLayer);
0205
0206
0207 void InitWeights();
0208 void ForceWeights(std::vector<Double_t>* weights);
0209
0210
0211 void DeleteNetwork();
0212 void DeleteNetworkLayer(TObjArray*& layer);
0213
0214
0215 void PrintLayer(TObjArray* layer) const;
0216 void PrintNeuron(TNeuron* neuron) const;
0217
0218
0219 TObjArray* fInputLayer;
0220 std::vector<TNeuron*> fOutputNeurons;
0221 TString fLayerSpec;
0222
0223
0224 static const Bool_t fgDEBUG = kTRUE;
0225
0226 ClassDef(MethodANNBase,0);
0227 };
0228
0229
0230
0231 template <typename WriteIterator>
0232 inline void MethodANNBase::GetLayerActivation (size_t layerNumber, WriteIterator writeIterator)
0233 {
0234
0235
0236
0237
0238
0239 if (layerNumber >= (size_t)fNetwork->GetEntriesFast())
0240 return;
0241
0242 TObjArray* layer = (TObjArray*)fNetwork->At(layerNumber);
0243 UInt_t nNodes = layer->GetEntriesFast();
0244 for (UInt_t iNode = 0; iNode < nNodes; iNode++)
0245 {
0246 (*writeIterator) = ((TNeuron*)layer->At(iNode))->GetActivationValue();
0247 ++writeIterator;
0248 }
0249 }
0250
0251
0252 }
0253
0254 #endif