ROOT  6.06/09
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 <iostream>
33 
34 #include "TFormula.h"
35 #include "TString.h"
36 #include "TMath.h"
37 
38 #ifndef ROOT_TMVA_TActivationTanh
39 #include "TMVA/TActivationTanh.h"
40 #endif
41 
42 
44 
45 ////////////////////////////////////////////////////////////////////////////////
46 /// constructor for tanh sigmoid (normalized in [-1,1])
47 
48 TMVA::TActivationTanh::TActivationTanh()
49 {
50  fFAST=kTRUE;
51 }
52 
53 ////////////////////////////////////////////////////////////////////////////////
54 /// destructor
55 
57 {
58 }
59 
60 ////////////////////////////////////////////////////////////////////////////////
61 /// a fast tanh approximation
62 
64  if (arg > 4.97) return 1;
65  if (arg < -4.97) return -1;
66  float arg2 = arg * arg;
67  float a = arg * (135135.0f + arg2 * (17325.0f + arg2 * (378.0f + arg2)));
68  float b = 135135.0f + arg2 * (62370.0f + arg2 * (3150.0f + arg2 * 28.0f));
69  return a/b;
70 }
71 
72 ////////////////////////////////////////////////////////////////////////////////
73 /// evaluate the tanh
74 
76 {
77  return fFAST ? fast_tanh(arg) : TMath::TanH(arg);
78 }
79 
80 ////////////////////////////////////////////////////////////////////////////////
81 /// evaluate the derivative
82 
84 {
85  Double_t tmp=Eval(arg);
86  return ( 1-tmp*tmp);
87 }
88 
89 ////////////////////////////////////////////////////////////////////////////////
90 /// get expressions for the tanh and its derivative
91 /// whatever that may be good for ...
92 
94 {
95  TString expr = "tanh(x)\t\t (1-tanh()^2)";
96  return expr;
97 }
98 
99 ////////////////////////////////////////////////////////////////////////////////
100 /// writes the sigmoid activation function source code
101 
102 void TMVA::TActivationTanh::MakeFunction( std::ostream& fout, const TString& fncName )
103 {
104  fout << "double " << fncName << "(double x) const {" << std::endl;
105  fout << " // hyperbolic tan" << std::endl;
106  fout << " return tanh(x);" << std::endl;
107  fout << "}" << std::endl;
108 }
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
ClassImp(TMVA::TActivationTanh) TMVA
constructor for tanh sigmoid (normalized in [-1,1])
Double_t EvalDerivative(Double_t arg)
evaluate the derivative
Double_t Eval(Double_t arg)
evaluate the tanh
~TActivationTanh()
destructor
double Double_t
Definition: RtypesCore.h:55
Abstract ClassifierFactory template that handles arbitrary types.
const Bool_t kTRUE
Definition: Rtypes.h:91