Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 * *
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 * (see tmva/doc/LICENSE) *
22 **********************************************************************************/
23
24/*! \class TMVA::TActivationTanh
25\ingroup TMVA
26Tanh activation function for ANN.
27*/
28
30
31#include "TMVA/TActivation.h"
32
33#include "TMath.h"
34#include "TString.h"
35
36#include <iostream>
37
38
39////////////////////////////////////////////////////////////////////////////////
40/// a fast tanh approximation
41
43 if (arg > 4.97) return 1;
44 if (arg < -4.97) return -1;
45 float arg2 = arg * arg;
46 float a = arg * (135135.0f + arg2 * (17325.0f + arg2 * (378.0f + arg2)));
47 float b = 135135.0f + arg2 * (62370.0f + arg2 * (3150.0f + arg2 * 28.0f));
48 return a/b;
49}
50
51////////////////////////////////////////////////////////////////////////////////
52/// evaluate the tanh
53
55{
56 return fFAST ? fast_tanh(arg) : TMath::TanH(arg);
57}
58
59////////////////////////////////////////////////////////////////////////////////
60/// evaluate the derivative
61
63{
64 Double_t tmp=Eval(arg);
65 return ( 1-tmp*tmp);
66}
67
68////////////////////////////////////////////////////////////////////////////////
69/// get expressions for the tanh and its derivative
70/// whatever that may be good for ...
71
73{
74 TString expr = "tanh(x)\t\t (1-tanh()^2)";
75 return expr;
76}
77
78////////////////////////////////////////////////////////////////////////////////
79/// writes the Tanh sigmoid activation function source code
80
82{
83 if (fFAST) {
84 fout << "double " << fncName << "(double x) const {" << std::endl;
85 fout << " // fast hyperbolic tan approximation" << std::endl;
86 fout << " if (x > 4.97) return 1;" << std::endl;
87 fout << " if (x < -4.97) return -1;" << std::endl;
88 fout << " float x2 = x * x;" << std::endl;
89 fout << " float a = x * (135135.0f + x2 * (17325.0f + x2 * (378.0f + x2)));" << std::endl;
90 fout << " float b = 135135.0f + x2 * (62370.0f + x2 * (3150.0f + x2 * 28.0f));" << std::endl;
91 fout << " return a / b;" << std::endl;
92 fout << "}" << std::endl;
93 } else {
94 fout << "double " << fncName << "(double x) const {" << std::endl;
95 fout << " // hyperbolic tan" << std::endl;
96 fout << " return tanh(x);" << std::endl;
97 fout << "}" << std::endl;
98 }
99}
#define b(i)
Definition RSha256.hxx:100
#define a(i)
Definition RSha256.hxx:99
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Double_t Eval(Double_t arg) override
evaluate the tanh
Double_t fast_tanh(Double_t arg)
a fast tanh approximation
void MakeFunction(std::ostream &fout, const TString &fncName) override
writes the Tanh sigmoid activation function source code
Double_t EvalDerivative(Double_t arg) override
evaluate the derivative
TString GetExpression() override
get expressions for the tanh and its derivative whatever that may be good for ...
Basic string class.
Definition TString.h:138
Double_t TanH(Double_t)
Returns the hyperbolic tangent of x.
Definition TMath.h:629