// @(#)root/mlp:$Id$
// Author: Christophe.Delaere@cern.ch   20/07/03

/*************************************************************************
 * Copyright (C) 1995-2003, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TNeuron
#define ROOT_TNeuron

#ifndef ROOT_TNamed
#include "TNamed.h"
#endif
#ifndef ROOT_TObjArray
#include "TObjArray.h"
#endif

class TTreeFormula;
class TSynapse;
class TBranch;
class TTree;
class TFormula;

//____________________________________________________________________
//
// TNeuron
//
// This class decribes an elementary neuron, which is the basic
// element for a Neural Network.
// A network is build connecting neurons by synapses.
// There are different types of neurons: linear (a+bx),
// sigmoid (1/(1+exp(-x)), tanh or gaussian.
// In a Multi Layer Perceptron, the input layer is made of
// inactive neurons (returning the normalized input), hidden layers
// are made of sigmoids and output neurons are linear.
//
// This implementation contains several methods to compute the value,
// the derivative, the DeDw, ...
// Values are stored in local buffers. The SetNewEvent() method is
// there to inform buffered values are outdated.
//
//____________________________________________________________________

class TNeuron : public TNamed {
   friend class TSynapse;

 public:
   enum ENeuronType { kOff, kLinear, kSigmoid, kTanh, kGauss, kSoftmax, kExternal };

   TNeuron(ENeuronType type = kSigmoid,
           const char* name = "", const char* title = "",
           const char* extF = "", const char* extD  = "" );
   virtual ~TNeuron() {}
   inline TSynapse* GetPre(Int_t n) const { return (TSynapse*) fpre.At(n); }
   inline TSynapse* GetPost(Int_t n) const { return (TSynapse*) fpost.At(n); }
   inline TNeuron* GetInLayer(Int_t n) const { return (TNeuron*) flayer.At(n); }
   TTreeFormula* UseBranch(TTree*, const char*);
   Double_t GetInput() const;
   Double_t GetValue() const;
   Double_t GetDerivative() const;
   Double_t GetError() const;
   Double_t GetTarget() const;
   Double_t GetDeDw() const;
   Double_t GetBranch() const;
   ENeuronType GetType() const;
   void SetWeight(Double_t w);
   inline Double_t GetWeight() const { return fWeight; }
   void SetNormalisation(Double_t mean, Double_t RMS);
   inline const Double_t* GetNormalisation() const { return fNorm; }
   void SetNewEvent() const;
   void SetDEDw(Double_t in);
   inline Double_t GetDEDw() const { return fDEDw; }
   void ForceExternalValue(Double_t value);
   void AddInLayer(TNeuron*);

 protected:
   Double_t Sigmoid(Double_t x) const;
   Double_t DSigmoid(Double_t x) const;
   void AddPre(TSynapse*);
   void AddPost(TSynapse*);

 private:
   TNeuron(const TNeuron&); // Not implemented
   TNeuron& operator=(const TNeuron&); // Not implemented

   TObjArray fpre;        // pointers to the previous level in a network
   TObjArray fpost;       // pointers to the next level in a network
   TObjArray flayer;      // pointers to the current level in a network (neurons, not synapses)
   Double_t fWeight;      // weight used for computation
   Double_t fNorm[2];     // normalisation to mean=0, RMS=1.
   ENeuronType fType;     // neuron type
   TFormula* fExtF;       // function   (external mode)
   TFormula* fExtD;       // derivative (external mode)
   //buffers
   //should be mutable when supported by all compilers
   TTreeFormula* fFormula;//! formula to be used for inputs and outputs
   Int_t fIndex;          //! index in the formula
   Bool_t fNewInput;      //! do we need to compute fInput again ?
   Double_t fInput;       //! buffer containing the last neuron input
   Bool_t fNewValue;      //! do we need to compute fValue again ?
   Double_t fValue;       //! buffer containing the last neuron output
   Bool_t fNewDeriv;      //! do we need to compute fDerivative again ?
   Double_t fDerivative;  //! buffer containing the last neuron derivative
   Bool_t fNewDeDw;       //! do we need to compute fDeDw again ?
   Double_t fDeDw;        //! buffer containing the last derivative of the error
   Double_t fDEDw;        //! buffer containing the sum over all examples of DeDw

   ClassDef(TNeuron, 4)   // Neuron for MultiLayerPerceptrons
};

#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