TActivationSigmoid.cxx

Go to the documentation of this file.
00001 // @(#)root/tmva $Id: TActivationSigmoid.cxx 29122 2009-06-22 06:51:30Z brun $
00002 // Author: Matt Jachowski
00003 
00004 /**********************************************************************************
00005  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
00006  * Package: TMVA                                                                  *
00007  * Class  : TActivationSigmoid                                                    *
00008  * Web    : http://tmva.sourceforge.net                                           *
00009  *                                                                                *
00010  * Description:                                                                   *
00011  *      Sigmoid activation function for TNeuron                                   *
00012  *                                                                                *
00013  * Authors (alphabetical):                                                        *
00014  *      Matt Jachowski  <jachowski@stanford.edu> - Stanford University, USA       *
00015  *                                                                                *
00016  * Copyright (c) 2005:                                                            *
00017  *      CERN, Switzerland                                                         *
00018  *                                                                                *
00019  * Redistribution and use in source and binary forms, with or without             *
00020  * modification, are permitted according to the terms listed in LICENSE           *
00021  * (http://tmva.sourceforge.net/LICENSE)                                          *
00022  **********************************************************************************/
00023   
00024 //_______________________________________________________________________
00025 //                                                                      
00026 //  Sigmoid activation function for TNeuron. This really simple implementation
00027 //  uses TFormulas and should probably be replaced with something more
00028 //  efficient later.
00029 //                                                                      
00030 //_______________________________________________________________________
00031 
00032 #include <iostream>
00033 
00034 #include "TFormula.h"
00035 #include "TString.h"
00036 #include "TMath.h"
00037 
00038 #ifndef ROOT_TMVA_TActivationSigmoid
00039 #include "TMVA/TActivationSigmoid.h"
00040 #endif
00041 
00042 static const Int_t  UNINITIALIZED = -1;
00043 
00044 ClassImp(TMVA::TActivationSigmoid)
00045 
00046 //______________________________________________________________________________
00047 TMVA::TActivationSigmoid::TActivationSigmoid()
00048 {
00049    // constructor for sigmoid normalized in [0,1]
00050    
00051    fEqn = new TFormula("sigmoid", "1.0/(1.0+TMath::Exp(-x))");
00052    fEqnDerivative = 
00053       new TFormula("derivative", "TMath::Exp(-x)/(1.0+TMath::Exp(-x))^2");
00054 }
00055 
00056 //______________________________________________________________________________
00057 TMVA::TActivationSigmoid::~TActivationSigmoid()
00058 {
00059    // destructor
00060    
00061    if (fEqn != NULL) delete fEqn;
00062    if (fEqnDerivative != NULL) delete fEqnDerivative;
00063 }
00064 
00065 //______________________________________________________________________________
00066 Double_t TMVA::TActivationSigmoid::Eval(Double_t arg)
00067 {
00068    // evaluate the sigmoid
00069 
00070    if (fEqn == NULL) return UNINITIALIZED;
00071    return fEqn->Eval(arg);
00072 
00073    //return EvalFast(arg);
00074 }
00075 
00076 //______________________________________________________________________________
00077 Double_t TMVA::TActivationSigmoid::EvalDerivative(Double_t arg)
00078 {
00079    // evaluate the derivative of the sigmoid
00080 
00081    if (fEqnDerivative == NULL) return UNINITIALIZED;
00082    return fEqnDerivative->Eval(arg);
00083 
00084    //return EvalDerivativeFast(arg);
00085 }
00086 
00087 //______________________________________________________________________________
00088 TString TMVA::TActivationSigmoid::GetExpression()
00089 {
00090    // get expressions for the sigmoid and its derivatives
00091    
00092    TString expr = "";
00093    
00094    if (fEqn == NULL) expr += "<null>";
00095    else              expr += fEqn->GetExpFormula();
00096    
00097    expr += "\t\t";
00098    
00099    if (fEqnDerivative == NULL) expr += "<null>";
00100    else                        expr += fEqnDerivative->GetExpFormula();
00101    
00102    return expr;
00103 }
00104 
00105 //______________________________________________________________________________
00106 void TMVA::TActivationSigmoid::MakeFunction( std::ostream& fout, const TString& fncName ) 
00107 {
00108    // writes the sigmoid activation function source code
00109    fout << "double " << fncName << "(double x) const {" << std::endl;
00110    fout << "   // sigmoid" << std::endl;
00111    fout << "   return 1.0/(1.0+exp(-x));" << std::endl;
00112    fout << "}" << std::endl;
00113 }

Generated on Tue Jul 5 15:25:37 2011 for ROOT_528-00b_version by  doxygen 1.5.1