Logo ROOT   6.08/07
Reference Guide
TActivationTanh.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 : TActivationTanh *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Tanh activation function (sigmoid normalized in [-1,1] for an ANN. *
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 // Tanh activation function for ANN. This really simple implementation
27 // uses TFormulas and should probably be replaced with something more
28 // efficient later.
29 //
30 //_______________________________________________________________________
31 
32 #include "TMVA/TActivationTanh.h"
33 
34 #include "TMVA/TActivation.h"
35 
36 #include "TFormula.h"
37 #include "TMath.h"
38 #include "TString.h"
39 
40 #include <iostream>
41 
43 
44 ////////////////////////////////////////////////////////////////////////////////
45 /// constructor for tanh sigmoid (normalized in [-1,1])
46 
48 {
49  fFAST=kTRUE;
50 }
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 /// destructor
54 
56 {
57 }
58 
59 ////////////////////////////////////////////////////////////////////////////////
60 /// a fast tanh approximation
61 
63  if (arg > 4.97) return 1;
64  if (arg < -4.97) return -1;
65  float arg2 = arg * arg;
66  float a = arg * (135135.0f + arg2 * (17325.0f + arg2 * (378.0f + arg2)));
67  float b = 135135.0f + arg2 * (62370.0f + arg2 * (3150.0f + arg2 * 28.0f));
68  return a/b;
69 }
70 
71 ////////////////////////////////////////////////////////////////////////////////
72 /// evaluate the tanh
73 
75 {
76  return fFAST ? fast_tanh(arg) : TMath::TanH(arg);
77 }
78 
79 ////////////////////////////////////////////////////////////////////////////////
80 /// evaluate the derivative
81 
83 {
84  Double_t tmp=Eval(arg);
85  return ( 1-tmp*tmp);
86 }
87 
88 ////////////////////////////////////////////////////////////////////////////////
89 /// get expressions for the tanh and its derivative
90 /// whatever that may be good for ...
91 
93 {
94  TString expr = "tanh(x)\t\t (1-tanh()^2)";
95  return expr;
96 }
97 
98 ////////////////////////////////////////////////////////////////////////////////
99 /// writes the sigmoid activation function source code
100 
101 void TMVA::TActivationTanh::MakeFunction( std::ostream& fout, const TString& fncName )
102 {
103  fout << "double " << fncName << "(double x) const {" << std::endl;
104  fout << " // hyperbolic tan" << std::endl;
105  fout << " return tanh(x);" << std::endl;
106  fout << "}" << std::endl;
107 }
Double_t TanH(Double_t)
Definition: TMath.h:436
Double_t fast_tanh(Double_t arg)
a fast tanh approximation
TString GetExpression()
get expressions for the tanh and its derivative whatever that may be good for ... ...
Basic string class.
Definition: TString.h:137
TArc * a
Definition: textangle.C:12
virtual void MakeFunction(std::ostream &fout, const TString &fncName)
writes the sigmoid activation function source code
Double_t EvalDerivative(Double_t arg)
evaluate the derivative
Double_t Eval(Double_t arg)
evaluate the tanh
~TActivationTanh()
destructor
#define ClassImp(name)
Definition: Rtypes.h:279
double Double_t
Definition: RtypesCore.h:55
Abstract ClassifierFactory template that handles arbitrary types.
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
const Bool_t kTRUE
Definition: Rtypes.h:91