TActivationTanh.cxx

Go to the documentation of this file.
00001 // @(#)root/tmva $Id: TActivationTanh.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  : TActivationTanh                                                       *
00008  * Web    : http://tmva.sourceforge.net                                           *
00009  *                                                                                *
00010  * Description:                                                                   *
00011  *      Tanh activation function (sigmoid normalized in [-1,1] for an ANN.        *
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 //  Tanh activation function for ANN. 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_TActivationTanh
00039 #include "TMVA/TActivationTanh.h"
00040 #endif
00041 
00042 static const Int_t  UNINITIALIZED = -1;
00043 
00044 ClassImp(TMVA::TActivationTanh)
00045 
00046 //______________________________________________________________________________
00047 TMVA::TActivationTanh::TActivationTanh()
00048 {
00049    // constructor for tanh sigmoid (normalized in [-1,1])
00050 
00051    fEqn = new TFormula("sigmoid", "TMath::TanH(x)");
00052    fEqnDerivative = 
00053       new TFormula("derivative", "1-(TMath::TanH(x))^2");
00054 }
00055 
00056 //______________________________________________________________________________
00057 TMVA::TActivationTanh::~TActivationTanh()
00058 {
00059    // destructor
00060 
00061    if (fEqn != NULL) delete fEqn;
00062    if (fEqnDerivative != NULL) delete fEqnDerivative;
00063 }
00064 
00065 //______________________________________________________________________________
00066 Double_t TMVA::TActivationTanh::Eval(Double_t arg)
00067 {
00068    // evaluate the tanh
00069 
00070    if (fEqn == NULL) return UNINITIALIZED;
00071    return fEqn->Eval(arg);
00072 }
00073 
00074 //______________________________________________________________________________
00075 Double_t TMVA::TActivationTanh::EvalDerivative(Double_t arg)
00076 {
00077    // evaluate the derivative
00078 
00079    if (fEqnDerivative == NULL) return UNINITIALIZED;
00080    return fEqnDerivative->Eval(arg);
00081 }
00082 
00083 //______________________________________________________________________________
00084 TString TMVA::TActivationTanh::GetExpression()
00085 {
00086    // get expressions for the tanh and its derivative
00087 
00088    TString expr = "";
00089 
00090    if (fEqn == NULL) expr += "<null>";
00091    else              expr += fEqn->GetExpFormula();
00092 
00093    expr += "\t\t";
00094 
00095    if (fEqnDerivative == NULL) expr += "<null>";
00096    else                        expr += fEqnDerivative->GetExpFormula();
00097 
00098    return expr;
00099 }
00100 
00101 //______________________________________________________________________________
00102 void TMVA::TActivationTanh::MakeFunction( std::ostream& fout, const TString& fncName ) 
00103 {
00104    // writes the sigmoid activation function source code
00105    fout << "double " << fncName << "(double x) const {" << std::endl;
00106    fout << "   // hyperbolic tan" << std::endl;
00107    fout << "   return tanh(x);" << std::endl;
00108    fout << "}" << std::endl;
00109 }

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