Logo ROOT   6.18/05
Reference Guide
ActivationFunctions.cxx
Go to the documentation of this file.
1// @(#)root/tmva/tmva/dnn:$Id$
2// Author: Simon Pfreundschuh 19/07/16
3
4/*************************************************************************
5 * Copyright (C) 2016, Simon Pfreundschuh *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12 ///////////////////////////////////////////////////////////////////
13 // Implementation of the activation functions for multi-threaded //
14 // CPU architectures using Roots TThreadExecutor and BLAS. //
15 ///////////////////////////////////////////////////////////////////
16
18#include <math.h>
19
20#ifdef R__HAS_VDT
21#include "vdt/tanh.h"
22#define TANH_IMPL_X vdt::fast_tanhf(x)
23#else
24#define TANH_IMPL_X tanh(x)
25#endif
26
27
28namespace TMVA
29{
30namespace DNN
31{
32
33//______________________________________________________________________________
34template<typename AFloat>
36 const TCpuMatrix<AFloat> &/*A*/)
37{
38 auto f = [](AFloat) {return 1.0;};
39 B.Map(f);
40}
41
42//______________________________________________________________________________
43template<typename AFloat>
45{
46 auto f = [](AFloat x) {return (x < 0.0) ? 0.0 : x;};
47 B.Map(f);
48}
49
50//______________________________________________________________________________
51template<typename AFloat>
53 const TCpuMatrix<AFloat> &A)
54{
55 auto f = [](AFloat x) {return (x < 0.0) ? 0.0 : 1.0;};
56 B.MapFrom(f, A);
57}
58
59//______________________________________________________________________________
60template<typename AFloat>
62{
63 auto f = [](AFloat x) {return 1.0 / (1.0 + exp(-x));};
64 B.Map(f);
65}
66
67//______________________________________________________________________________
68template<typename AFloat>
70 const TCpuMatrix<AFloat> &A)
71{
72 auto f = [](AFloat x) {
73 AFloat sig = 1.0 / (1.0 + exp(-x));
74 return sig * (1.0 - sig);
75 };
76 B.MapFrom(f, A);
77}
78
79//______________________________________________________________________________
80template<typename AFloat>
82{
83 auto f = [](AFloat x) {return TANH_IMPL_X;};
84 B.Map(f);
85}
86
87//______________________________________________________________________________
88template<typename AFloat>
90 const TCpuMatrix<AFloat> &A)
91{
92 auto f = [](AFloat x) {
93 AFloat t = TANH_IMPL_X;
94 return 1 - t * t;
95 };
96 B.MapFrom(f, A);
97}
98
99//______________________________________________________________________________
100template<typename AFloat>
102{
103 auto f = [](AFloat x) {return fabs(x);};
104 B.Map(f);
105}
106
107//______________________________________________________________________________
108template<typename AFloat>
110 const TCpuMatrix<AFloat> &A)
111{
112 auto f = [](AFloat x) {
113 return (x < 0.0) ? -1.0 : 1.0;
114 };
115 B.MapFrom(f, A);
116}
117
118//______________________________________________________________________________
119template<typename AFloat>
121{
122 auto f = [](AFloat x) {return x / (1 + fabs(x));};
123 B.Map(f);
124}
125
126//______________________________________________________________________________
127template<typename AFloat>
129 const TCpuMatrix<AFloat> &A)
130{
131 auto f = [](AFloat x) {
132 x = 1.0 + fabs(x);
133 x = 1.0 / (x * x);
134 return x;
135 };
136 B.MapFrom(f, A);
137}
138
139//______________________________________________________________________________
140template<typename AFloat>
142{
143 auto f = [](AFloat x) {return exp(- x * x);};
144 B.Map(f);
145}
146
147//______________________________________________________________________________
148template<typename AFloat>
150 const TCpuMatrix<AFloat> &A)
151{
152 auto f = [](AFloat x) {return - 2.0 * x * exp(- x * x);};
153 B.MapFrom(f, A);
154}
155
156} // namespace DNN
157} // namespace TMVA
#define TANH_IMPL_X
#define f(i)
Definition: RSha256.hxx:104
double exp(double)
The TCpuMatrix class.
Definition: CpuMatrix.h:89
static void SymmetricRelu(TCpuMatrix< Scalar_t > &B)
static void Gauss(TCpuMatrix< Scalar_t > &B)
static void SymmetricReluDerivative(TCpuMatrix< Scalar_t > &B, const TCpuMatrix< Scalar_t > &A)
static void Relu(TCpuMatrix< Scalar_t > &B)
static void SoftSign(TCpuMatrix< Scalar_t > &B)
static void TanhDerivative(TCpuMatrix< Scalar_t > &B, const TCpuMatrix< Scalar_t > &A)
static void SoftSignDerivative(TCpuMatrix< Scalar_t > &B, const TCpuMatrix< Scalar_t > &A)
static void IdentityDerivative(TCpuMatrix< Scalar_t > &B, const TCpuMatrix< Scalar_t > &A)
static void Tanh(TCpuMatrix< Scalar_t > &B)
static void Sigmoid(TCpuMatrix< Scalar_t > &B)
static void SigmoidDerivative(TCpuMatrix< Scalar_t > &B, const TCpuMatrix< Scalar_t > &A)
static void ReluDerivative(TCpuMatrix< Scalar_t > &B, const TCpuMatrix< Scalar_t > &A)
static void GaussDerivative(TCpuMatrix< Scalar_t > &B, const TCpuMatrix< Scalar_t > &A)
Double_t x[n]
Definition: legend1.C:17
static double B[]
static double A[]
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
create variable transformations