ROOT logo
// @(#)root/tmva $Id: TNeuron.h 29195 2009-06-24 10:39:49Z brun $
// Author: Matt Jachowski

/**********************************************************************************
 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
 * Package: TMVA                                                                  *
 * Class  : TMVA::TNeuron                                                         *
 * Web    : http://tmva.sourceforge.net                                           *
 *                                                                                *
 * Description:                                                                   *
 *      Neuron class to be used in MethodANNBase and its derivatives.             *
 *                                                                                *
 * Authors (alphabetical):                                                        *
 *      Matt Jachowski  <jachowski@stanford.edu> - Stanford University, USA       *
 *                                                                                *
 * Copyright (c) 2005:                                                            *
 *      CERN, Switzerland                                                         *
 *                                                                                *
 * Redistribution and use in source and binary forms, with or without             *
 * modification, are permitted according to the terms listed in LICENSE           *
 * (http://tmva.sourceforge.net/LICENSE)                                          *
 **********************************************************************************/ 

#ifndef ROOT_TMVA_TNeuron
#define ROOT_TMVA_TNeuron

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TNeuron                                                              //
//                                                                      //
// Neuron used by derivatives of MethodANNBase                          //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#ifndef ROOT_TString
#include "TString.h"
#endif
#ifndef ROOT_TObjArray
#include "TObjArray.h"
#endif
#ifndef ROOT_TFormula
#include "TFormula.h"
#endif

#ifndef ROOT_TMVA_TSynapse
#include "TMVA/TSynapse.h"
#endif
#ifndef ROOT_TMVA_TActivation
#include "TMVA/TActivation.h"
#endif
#ifndef ROOT_TMVA_Types
#include "TMVA/Types.h"
#endif

namespace TMVA {

   class TNeuronInput;

   class TNeuron : public TObject {

   public:

      TNeuron();
      virtual ~TNeuron();

      // force the input value
      void ForceValue(Double_t value);

      // calculate the input value
      void CalculateValue();

      // calculate the activation value
      void CalculateActivationValue();

      // calculate the error field of the neuron
      void CalculateDelta();

      // set the activation function
      void SetActivationEqn(TActivation* activation);

      // set the input calculator
      void SetInputCalculator(TNeuronInput* calculator);

      // add a synapse as a pre-link
      void AddPreLink(TSynapse* pre);

      // add a synapse as a post-link
      void AddPostLink(TSynapse* post);

      // delete all pre-links
      void DeletePreLinks();

      // set the error
      void SetError(Double_t error);

      // update the error fields of all pre-synapses, batch mode
      // to actually update the weights, call adjust synapse weights
      void UpdateSynapsesBatch();

      // update the error fields and weights of all pre-synapses, sequential mode
      void UpdateSynapsesSequential();

      // update the weights of the all pre-synapses, batch mode 
      //(call UpdateSynapsesBatch first)
      void AdjustSynapseWeights();

      // explicitly initialize error fields of pre-synapses, batch mode
      void InitSynapseDeltas();

      // print activation equation, for debugging
      void PrintActivationEqn();

      // inlined functions
      Double_t  GetValue() const                { return fValue;                          }
      Double_t  GetActivationValue() const      { return fActivationValue;                }
      Double_t  GetDelta() const                { return fDelta;                          }
      Double_t  GetDEDw() const                 { return fDEDw;                           }
      Int_t     NumPreLinks() const             { return NumLinks(fLinksIn);              }
      Int_t     NumPostLinks() const            { return NumLinks(fLinksOut);             }
      TSynapse* PreLinkAt ( Int_t index ) const { return (TSynapse*)fLinksIn->At(index);  }
      TSynapse* PostLinkAt( Int_t index ) const { return (TSynapse*)fLinksOut->At(index); }
      void      SetInputNeuron()                { NullifyLinks(fLinksIn);                 }
      void      SetOutputNeuron()               { NullifyLinks(fLinksOut);                }
      void      SetBiasNeuron()                 { NullifyLinks(fLinksIn);                 }
      void      SetDEDw( Double_t DEDw )        { fDEDw = DEDw;                           }
      Bool_t    IsInputNeuron() const           { return fLinksIn == NULL;                }
      Bool_t    IsOutputNeuron() const          { return fLinksOut == NULL;               }
      void      PrintPreLinks()                 { PrintLinks(fLinksIn); return;           }
      void      PrintPostLinks()                { PrintLinks(fLinksOut); return;          }

   private:

      // prviate helper functions
      void InitNeuron();
      void DeleteLinksArray( TObjArray*& links );
      void PrintLinks      ( TObjArray* links );
      void PrintMessage    ( EMsgType, TString message );

      // inlined helper functions
      Int_t NumLinks(TObjArray* links) const { 
         if (links == NULL) return 0; return links->GetEntriesFast(); 
      }
      void NullifyLinks(TObjArray*& links) { 
         if (links != NULL) delete links; links = NULL; 
      }

      // private member variables
      TObjArray*    fLinksIn;                 // array of input synapses
      TObjArray*    fLinksOut;                // array of output synapses
      Double_t      fValue;                   // input value
      Double_t      fActivationValue;         // activation/output value
      Double_t      fDelta;                   // error field of neuron
      Double_t      fDEDw;                    // sum of all deltas
      Double_t      fError;                   // error, only set for output neurons
      Bool_t        fForcedValue;             // flag for forced input value
      TActivation*  fActivation;              // activation equation
      TNeuronInput* fInputCalculator;         // input calculator

      mutable MsgLogger* fLogger;                     //! message logger
      MsgLogger& Log() const { return *fLogger; }                       

      ClassDef(TNeuron,0) // Neuron class used by MethodANNBase derivative ANNs
   };

} // namespace TMVA

#endif
 TNeuron.h:1
 TNeuron.h:2
 TNeuron.h:3
 TNeuron.h:4
 TNeuron.h:5
 TNeuron.h:6
 TNeuron.h:7
 TNeuron.h:8
 TNeuron.h:9
 TNeuron.h:10
 TNeuron.h:11
 TNeuron.h:12
 TNeuron.h:13
 TNeuron.h:14
 TNeuron.h:15
 TNeuron.h:16
 TNeuron.h:17
 TNeuron.h:18
 TNeuron.h:19
 TNeuron.h:20
 TNeuron.h:21
 TNeuron.h:22
 TNeuron.h:23
 TNeuron.h:24
 TNeuron.h:25
 TNeuron.h:26
 TNeuron.h:27
 TNeuron.h:28
 TNeuron.h:29
 TNeuron.h:30
 TNeuron.h:31
 TNeuron.h:32
 TNeuron.h:33
 TNeuron.h:34
 TNeuron.h:35
 TNeuron.h:36
 TNeuron.h:37
 TNeuron.h:38
 TNeuron.h:39
 TNeuron.h:40
 TNeuron.h:41
 TNeuron.h:42
 TNeuron.h:43
 TNeuron.h:44
 TNeuron.h:45
 TNeuron.h:46
 TNeuron.h:47
 TNeuron.h:48
 TNeuron.h:49
 TNeuron.h:50
 TNeuron.h:51
 TNeuron.h:52
 TNeuron.h:53
 TNeuron.h:54
 TNeuron.h:55
 TNeuron.h:56
 TNeuron.h:57
 TNeuron.h:58
 TNeuron.h:59
 TNeuron.h:60
 TNeuron.h:61
 TNeuron.h:62
 TNeuron.h:63
 TNeuron.h:64
 TNeuron.h:65
 TNeuron.h:66
 TNeuron.h:67
 TNeuron.h:68
 TNeuron.h:69
 TNeuron.h:70
 TNeuron.h:71
 TNeuron.h:72
 TNeuron.h:73
 TNeuron.h:74
 TNeuron.h:75
 TNeuron.h:76
 TNeuron.h:77
 TNeuron.h:78
 TNeuron.h:79
 TNeuron.h:80
 TNeuron.h:81
 TNeuron.h:82
 TNeuron.h:83
 TNeuron.h:84
 TNeuron.h:85
 TNeuron.h:86
 TNeuron.h:87
 TNeuron.h:88
 TNeuron.h:89
 TNeuron.h:90
 TNeuron.h:91
 TNeuron.h:92
 TNeuron.h:93
 TNeuron.h:94
 TNeuron.h:95
 TNeuron.h:96
 TNeuron.h:97
 TNeuron.h:98
 TNeuron.h:99
 TNeuron.h:100
 TNeuron.h:101
 TNeuron.h:102
 TNeuron.h:103
 TNeuron.h:104
 TNeuron.h:105
 TNeuron.h:106
 TNeuron.h:107
 TNeuron.h:108
 TNeuron.h:109
 TNeuron.h:110
 TNeuron.h:111
 TNeuron.h:112
 TNeuron.h:113
 TNeuron.h:114
 TNeuron.h:115
 TNeuron.h:116
 TNeuron.h:117
 TNeuron.h:118
 TNeuron.h:119
 TNeuron.h:120
 TNeuron.h:121
 TNeuron.h:122
 TNeuron.h:123
 TNeuron.h:124
 TNeuron.h:125
 TNeuron.h:126
 TNeuron.h:127
 TNeuron.h:128
 TNeuron.h:129
 TNeuron.h:130
 TNeuron.h:131
 TNeuron.h:132
 TNeuron.h:133
 TNeuron.h:134
 TNeuron.h:135
 TNeuron.h:136
 TNeuron.h:137
 TNeuron.h:138
 TNeuron.h:139
 TNeuron.h:140
 TNeuron.h:141
 TNeuron.h:142
 TNeuron.h:143
 TNeuron.h:144
 TNeuron.h:145
 TNeuron.h:146
 TNeuron.h:147
 TNeuron.h:148
 TNeuron.h:149
 TNeuron.h:150
 TNeuron.h:151
 TNeuron.h:152
 TNeuron.h:153
 TNeuron.h:154
 TNeuron.h:155
 TNeuron.h:156
 TNeuron.h:157
 TNeuron.h:158
 TNeuron.h:159
 TNeuron.h:160
 TNeuron.h:161
 TNeuron.h:162
 TNeuron.h:163
 TNeuron.h:164
 TNeuron.h:165
 TNeuron.h:166
 TNeuron.h:167