Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TNeuron.h
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Matt Jachowski
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : TMVA::TNeuron *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Neuron class to be used in MethodANNBase and its derivatives. *
12  * *
13  * Authors (alphabetical): *
14  * Matt Jachowski <jachowski@stanford.edu> - Stanford University, USA *
15  * *
16  * Copyright (c) 2005: *
17  * CERN, Switzerland *
18  * *
19  * Redistribution and use in source and binary forms, with or without *
20  * modification, are permitted according to the terms listed in LICENSE *
21  * (http://tmva.sourceforge.net/LICENSE) *
22  **********************************************************************************/
23 
24 #ifndef ROOT_TMVA_TNeuron
25 #define ROOT_TMVA_TNeuron
26 
27 //////////////////////////////////////////////////////////////////////////
28 // //
29 // TNeuron //
30 // //
31 // Neuron used by derivatives of MethodANNBase //
32 // //
33 //////////////////////////////////////////////////////////////////////////
34 
35 #include <iostream>
36 
37 #include "TString.h"
38 #include "TObjArray.h"
39 #include "TFormula.h"
40 
41 #include "TMVA/TSynapse.h"
42 #include "TMVA/TActivation.h"
43 #include "TMVA/Types.h"
44 
45 namespace TMVA {
46 
47  class TNeuronInput;
48 
49  class TNeuron : public TObject {
50 
51  public:
52 
53  TNeuron();
54  virtual ~TNeuron();
55 
56  // force the input value
57  void ForceValue(Double_t value);
58 
59  // calculate the input value
60  void CalculateValue();
61 
62  // calculate the activation value
63  void CalculateActivationValue();
64 
65  // calculate the error field of the neuron
66  void CalculateDelta();
67 
68  // set the activation function
69  void SetActivationEqn(TActivation* activation);
70 
71  // set the input calculator
72  void SetInputCalculator(TNeuronInput* calculator);
73 
74  // add a synapse as a pre-link
75  void AddPreLink(TSynapse* pre);
76 
77  // add a synapse as a post-link
78  void AddPostLink(TSynapse* post);
79 
80  // delete all pre-links
81  void DeletePreLinks();
82 
83  // set the error
84  void SetError(Double_t error);
85 
86  // update the error fields of all pre-synapses, batch mode
87  // to actually update the weights, call adjust synapse weights
88  void UpdateSynapsesBatch();
89 
90  // update the error fields and weights of all pre-synapses, sequential mode
91  void UpdateSynapsesSequential();
92 
93  // update the weights of the all pre-synapses, batch mode
94  //(call UpdateSynapsesBatch first)
95  void AdjustSynapseWeights();
96 
97  // explicitly initialize error fields of pre-synapses, batch mode
98  void InitSynapseDeltas();
99 
100  // print activation equation, for debugging
101  void PrintActivationEqn();
102 
103  // inlined functions
104  Double_t GetValue() const { return fValue; }
105  Double_t GetActivationValue() const { return fActivationValue; }
106  Double_t GetDelta() const { return fDelta; }
107  Double_t GetDEDw() const { return fDEDw; }
108  Int_t NumPreLinks() const { return NumLinks(fLinksIn); }
109  Int_t NumPostLinks() const { return NumLinks(fLinksOut); }
110  TSynapse* PreLinkAt ( Int_t index ) const { return (TSynapse*)fLinksIn->At(index); }
111  TSynapse* PostLinkAt( Int_t index ) const { return (TSynapse*)fLinksOut->At(index); }
112  void SetInputNeuron() { NullifyLinks(fLinksIn); }
113  void SetOutputNeuron() { NullifyLinks(fLinksOut); }
114  void SetBiasNeuron() { NullifyLinks(fLinksIn); }
115  void SetDEDw( Double_t DEDw ) { fDEDw = DEDw; }
116  Bool_t IsInputNeuron() const { return fLinksIn == NULL; }
117  Bool_t IsOutputNeuron() const { return fLinksOut == NULL; }
118  void PrintPreLinks() const { PrintLinks(fLinksIn); return; }
119  void PrintPostLinks() const { PrintLinks(fLinksOut); return; }
120 
121  virtual void Print(Option_t* = "") const {
122  std::cout << fValue << std::endl;
123  //PrintPreLinks(); PrintPostLinks();
124  }
125 
126  private:
127 
128  // private helper functions
129  void InitNeuron();
130  void DeleteLinksArray( TObjArray*& links );
131  void PrintLinks ( TObjArray* links ) const;
132  void PrintMessage ( EMsgType, TString message );
133 
134  // inlined helper functions
135  Int_t NumLinks(TObjArray* links) const {
136  if (links == nullptr) return 0;
137  else return links->GetEntriesFast();
138  }
139  void NullifyLinks(TObjArray*& links) {
140  if (links != nullptr) { delete links; links = nullptr; }
141  }
142 
143  // private member variables
144  TObjArray* fLinksIn; // array of input synapses
145  TObjArray* fLinksOut; // array of output synapses
146  Double_t fValue; // input value
147  Double_t fActivationValue; // activation/output value
148  Double_t fDelta; // error field of neuron
149  Double_t fDEDw; // sum of all deltas
150  Double_t fError; // error, only set for output neurons
151  Bool_t fForcedValue; // flag for forced input value
152  TActivation* fActivation; // activation equation
153  TNeuronInput* fInputCalculator; // input calculator
154 
155  MsgLogger& Log() const;
156 
157  ClassDef(TNeuron,0); // Neuron class used by MethodANNBase derivative ANNs
158  };
159 
160 } // namespace TMVA
161 
162 #endif