ROOT  6.06/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 //_______________________________________________________________________
25 //
26 // Sigmoid activation function for TNeuron. This really simple implementation
27 // uses TFormulas and should probably be replaced with something more
28 // efficient later.
29 //
30 //_______________________________________________________________________
31 
32 #include <iostream>
33 
34 #include "TFormula.h"
35 #include "TString.h"
36 #include "TMath.h"
37 
38 #ifndef ROOT_TMVA_TActivationSigmoid
40 #endif
41 
42 static const Int_t UNINITIALIZED = -1;
43 
45 
46 ////////////////////////////////////////////////////////////////////////////////
47 /// constructor for sigmoid normalized in [0,1]
48 
49 TMVA::TActivationSigmoid::TActivationSigmoid()
50 {
51  fEqn = new TFormula("sigmoid", "1.0/(1.0+TMath::Exp(-x))");
52  fEqnDerivative =
53  new TFormula("derivative", "TMath::Exp(-x)/(1.0+TMath::Exp(-x))^2");
54 }
55 
56 ////////////////////////////////////////////////////////////////////////////////
57 /// destructor
58 
60 {
61  if (fEqn != NULL) delete fEqn;
62  if (fEqnDerivative != NULL) delete fEqnDerivative;
63 }
64 
65 ////////////////////////////////////////////////////////////////////////////////
66 /// evaluate the sigmoid
67 
69 {
70  if (fEqn == NULL) return UNINITIALIZED;
71  return fEqn->Eval(arg);
72 
73  //return EvalFast(arg);
74 }
75 
76 ////////////////////////////////////////////////////////////////////////////////
77 /// evaluate the derivative of the sigmoid
78 
80 {
81  if (fEqnDerivative == NULL) return UNINITIALIZED;
82  return fEqnDerivative->Eval(arg);
83 
84  //return EvalDerivativeFast(arg);
85 }
86 
87 ////////////////////////////////////////////////////////////////////////////////
88 /// get expressions for the sigmoid and its derivatives
89 
91 {
92  TString expr = "";
93 
94  if (fEqn == NULL) expr += "<null>";
95  else expr += fEqn->GetExpFormula();
96 
97  expr += "\t\t";
98 
99  if (fEqnDerivative == NULL) expr += "<null>";
100  else expr += fEqnDerivative->GetExpFormula();
101 
102  return expr;
103 }
104 
105 ////////////////////////////////////////////////////////////////////////////////
106 /// writes the sigmoid activation function source code
107 
108 void TMVA::TActivationSigmoid::MakeFunction( std::ostream& fout, const TString& fncName )
109 {
110  fout << "double " << fncName << "(double x) const {" << std::endl;
111  fout << " // sigmoid" << std::endl;
112  fout << " return 1.0/(1.0+exp(-x));" << std::endl;
113  fout << "}" << std::endl;
114 }
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
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 F O R M U L A class.
Definition: TFormula.h:89
Double_t Eval(Double_t arg)
evaluate the sigmoid
TString GetExpression()
get expressions for the sigmoid and its derivatives
double Double_t
Definition: RtypesCore.h:55
ClassImp(TMVA::TActivationSigmoid) TMVA
constructor for sigmoid normalized in [0,1]
Abstract ClassifierFactory template that handles arbitrary types.
static const Int_t UNINITIALIZED
#define NULL
Definition: Rtypes.h:82