Logo ROOT   6.14/05
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 /*! \class TMVA::TActivationTanh
25 \ingroup TMVA
26 Tanh activation function for ANN. This really simple implementation
27 uses TFormula and should probably be replaced with something more
28 efficient later.
29 */
30 
31 #include "TMVA/TActivationTanh.h"
32 
33 #include "TMVA/TActivation.h"
34 
35 #include "TFormula.h"
36 #include "TMath.h"
37 #include "TString.h"
38 
39 #include <iostream>
40 
42 
43 ////////////////////////////////////////////////////////////////////////////////
44 /// constructor for tanh sigmoid (normalized in [-1,1])
45 
47 {
48  fFAST=kTRUE;
49 }
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 /// destructor
53 
55 {
56 }
57 
58 ////////////////////////////////////////////////////////////////////////////////
59 /// a fast tanh approximation
60 
62  if (arg > 4.97) return 1;
63  if (arg < -4.97) return -1;
64  float arg2 = arg * arg;
65  float a = arg * (135135.0f + arg2 * (17325.0f + arg2 * (378.0f + arg2)));
66  float b = 135135.0f + arg2 * (62370.0f + arg2 * (3150.0f + arg2 * 28.0f));
67  return a/b;
68 }
69 
70 ////////////////////////////////////////////////////////////////////////////////
71 /// evaluate the tanh
72 
74 {
75  return fFAST ? fast_tanh(arg) : TMath::TanH(arg);
76 }
77 
78 ////////////////////////////////////////////////////////////////////////////////
79 /// evaluate the derivative
80 
82 {
83  Double_t tmp=Eval(arg);
84  return ( 1-tmp*tmp);
85 }
86 
87 ////////////////////////////////////////////////////////////////////////////////
88 /// get expressions for the tanh and its derivative
89 /// whatever that may be good for ...
90 
92 {
93  TString expr = "tanh(x)\t\t (1-tanh()^2)";
94  return expr;
95 }
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 /// writes the sigmoid activation function source code
99 
100 void TMVA::TActivationTanh::MakeFunction( std::ostream& fout, const TString& fncName )
101 {
102  fout << "double " << fncName << "(double x) const {" << std::endl;
103  fout << " // hyperbolic tan" << std::endl;
104  fout << " return tanh(x);" << std::endl;
105  fout << "}" << std::endl;
106 }
Double_t TanH(Double_t)
Definition: TMath.h:656
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:131
TActivationTanh()
constructor for tanh sigmoid (normalized in [-1,1])
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
Tanh activation function for ANN.
Double_t Eval(Double_t arg)
evaluate the tanh
auto * a
Definition: textangle.C:12
~TActivationTanh()
destructor
#define ClassImp(name)
Definition: Rtypes.h:359
double Double_t
Definition: RtypesCore.h:55
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: RtypesCore.h:87