Logo ROOT   6.10/09
Reference Guide
TActivationSigmoid.cxx
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 : TActivationSigmoid *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Sigmoid activation function for TNeuron *
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 /*! \class TMVA::TActivationSigmoid
25 \ingroup TMVA
26 Sigmoid activation function for TNeuron. This really simple implementation
27 uses TFormula and should probably be replaced with something more
28 efficient later.
29 */
30 
32 
33 #include "TMVA/TActivation.h"
34 
35 #include "TFormula.h"
36 #include "TMath.h"
37 #include "TString.h"
38 
39 #include <iostream>
40 
41 static const Int_t UNINITIALIZED = -1;
42 
44 
45 ////////////////////////////////////////////////////////////////////////////////
46 /// constructor for sigmoid normalized in [0,1]
47 
49 {
50  fEqn = new TFormula("sigmoid", "1.0/(1.0+TMath::Exp(-x))");
51  fEqnDerivative =
52  new TFormula("derivative", "TMath::Exp(-x)/(1.0+TMath::Exp(-x))^2");
53 }
54 
55 ////////////////////////////////////////////////////////////////////////////////
56 /// destructor
57 
59 {
60  if (fEqn != NULL) delete fEqn;
61  if (fEqnDerivative != NULL) delete fEqnDerivative;
62 }
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 /// evaluate the sigmoid
66 
68 {
69  if (fEqn == NULL) return UNINITIALIZED;
70  return fEqn->Eval(arg);
71 
72  //return EvalFast(arg);
73 }
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 /// evaluate the derivative of the sigmoid
77 
79 {
80  if (fEqnDerivative == NULL) return UNINITIALIZED;
81  return fEqnDerivative->Eval(arg);
82 
83  //return EvalDerivativeFast(arg);
84 }
85 
86 ////////////////////////////////////////////////////////////////////////////////
87 /// get expressions for the sigmoid and its derivatives
88 
90 {
91  TString expr = "";
92 
93  if (fEqn == NULL) expr += "<null>";
94  else expr += fEqn->GetExpFormula();
95 
96  expr += "\t\t";
97 
98  if (fEqnDerivative == NULL) expr += "<null>";
99  else expr += fEqnDerivative->GetExpFormula();
100 
101  return expr;
102 }
103 
104 ////////////////////////////////////////////////////////////////////////////////
105 /// writes the sigmoid activation function source code
106 
107 void TMVA::TActivationSigmoid::MakeFunction( std::ostream& fout, const TString& fncName )
108 {
109  fout << "double " << fncName << "(double x) const {" << std::endl;
110  fout << " // sigmoid" << std::endl;
111  fout << " return 1.0/(1.0+exp(-x));" << std::endl;
112  fout << "}" << std::endl;
113 }
Double_t Eval(Double_t x) const
Sets first variable (e.g. x) and evaluate formula.
Definition: TFormula.cxx:2642
Basic string class.
Definition: TString.h:129
int Int_t
Definition: RtypesCore.h:41
#define NULL
Definition: RtypesCore.h:88
Sigmoid activation function for TNeuron.
Double_t EvalDerivative(Double_t arg)
evaluate the derivative of the sigmoid
virtual void MakeFunction(std::ostream &fout, const TString &fncName)
writes the sigmoid activation function source code
The Formula class.
Definition: TFormula.h:83
Double_t Eval(Double_t arg)
evaluate the sigmoid
TString GetExpression()
get expressions for the sigmoid and its derivatives
#define ClassImp(name)
Definition: Rtypes.h:336
double Double_t
Definition: RtypesCore.h:55
Abstract ClassifierFactory template that handles arbitrary types.
static const Int_t UNINITIALIZED
TString GetExpFormula(Option_t *option="") const
Return the expression formula.
Definition: TFormula.cxx:2712