Logo ROOT   6.08/07
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 namespace TMVA
21 {
22 namespace DNN
23 {
24 
25 //______________________________________________________________________________
26 template<typename AFloat>
28  const TCpuMatrix<AFloat> &/*A*/)
29 {
30  auto f = [](AFloat) {return 1.0;};
31  B.Map(f);
32 }
33 
34 //______________________________________________________________________________
35 template<typename AFloat>
37 {
38  auto f = [](AFloat x) {return (x < 0.0) ? 0.0 : x;};
39  B.Map(f);
40 }
41 
42 //______________________________________________________________________________
43 template<typename AFloat>
45  const TCpuMatrix<AFloat> &A)
46 {
47  auto f = [](AFloat x) {return (x < 0.0) ? 0.0 : 1.0;};
48  B.MapFrom(f, A);
49 }
50 
51 //______________________________________________________________________________
52 template<typename AFloat>
54 {
55  auto f = [](AFloat x) {return 1.0 / (1.0 + exp(-x));};
56  B.Map(f);
57 }
58 
59 //______________________________________________________________________________
60 template<typename AFloat>
62  const TCpuMatrix<AFloat> &A)
63 {
64  auto f = [](AFloat x) {
65  AFloat sig = 1.0 / (1.0 + exp(-x));
66  return sig * (1.0 - sig);
67  };
68  B.MapFrom(f, A);
69 }
70 
71 //______________________________________________________________________________
72 template<typename AFloat>
74 {
75  auto f = [](AFloat x) {return tanh(x);};
76  B.Map(f);
77 }
78 
79 //______________________________________________________________________________
80 template<typename AFloat>
82  const TCpuMatrix<AFloat> &A)
83 {
84  auto f = [](AFloat x) {
85  AFloat t = tanh(x);
86  return 1 - t * t;
87  };
88  B.MapFrom(f, A);
89 }
90 
91 //______________________________________________________________________________
92 template<typename AFloat>
94 {
95  auto f = [](AFloat x) {return fabs(x);};
96  B.Map(f);
97 }
98 
99 //______________________________________________________________________________
100 template<typename AFloat>
102  const TCpuMatrix<AFloat> &A)
103 {
104  auto f = [](AFloat x) {
105  return (x < 0.0) ? -1.0 : 1.0;
106  };
107  B.MapFrom(f, A);
108 }
109 
110 //______________________________________________________________________________
111 template<typename AFloat>
113 {
114  auto f = [](AFloat x) {return x / (1 + fabs(x));};
115  B.Map(f);
116 }
117 
118 //______________________________________________________________________________
119 template<typename AFloat>
121  const TCpuMatrix<AFloat> &A)
122 {
123  auto f = [](AFloat x) {
124  x = 1.0 + fabs(x);
125  x = 1.0 / (x * x);
126  return x;
127  };
128  B.MapFrom(f, A);
129 }
130 
131 //______________________________________________________________________________
132 template<typename AFloat>
134 {
135  auto f = [](AFloat x) {return exp(- x * x);};
136  B.Map(f);
137 }
138 
139 //______________________________________________________________________________
140 template<typename AFloat>
142  const TCpuMatrix<AFloat> &A)
143 {
144  auto f = [](AFloat x) {return - 2.0 * x * exp(- x * x);};
145  B.MapFrom(f, A);
146 }
147 
148 } // namespace DNN
149 } // namespace TMVA
static double B[]
static void Sigmoid(TCpuMatrix< Scalar_t > &B)
The TCpuMatrix class.
Definition: CpuMatrix.h:46
double tanh(double)
static void TanhDerivative(TCpuMatrix< Scalar_t > &B, const TCpuMatrix< Scalar_t > &A)
static double A[]
static void SoftSign(TCpuMatrix< Scalar_t > &B)
Double_t x[n]
Definition: legend1.C:17
static void SymmetricReluDerivative(TCpuMatrix< Scalar_t > &B, const TCpuMatrix< Scalar_t > &A)
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
void MapFrom(Function_t &f, const TCpuMatrix &A)
Same as maps but takes the input values from the matrix A and writes the results in this matrix...
Definition: CpuMatrix.h:133
static void ReluDerivative(TCpuMatrix< Scalar_t > &B, const TCpuMatrix< Scalar_t > &A)
static void SymmetricRelu(TCpuMatrix< Scalar_t > &B)
static void IdentityDerivative(TCpuMatrix< Scalar_t > &B, const TCpuMatrix< Scalar_t > &A)
double f(double x)
static void SoftSignDerivative(TCpuMatrix< Scalar_t > &B, const TCpuMatrix< Scalar_t > &A)
void Map(Function_t &f)
Map the given function over the matrix elements.
Definition: CpuMatrix.h:118
Abstract ClassifierFactory template that handles arbitrary types.
static void Gauss(TCpuMatrix< Scalar_t > &B)
static void Tanh(TCpuMatrix< Scalar_t > &B)
static void Relu(TCpuMatrix< Scalar_t > &B)
double exp(double)
static void SigmoidDerivative(TCpuMatrix< Scalar_t > &B, const TCpuMatrix< Scalar_t > &A)
static void GaussDerivative(TCpuMatrix< Scalar_t > &B, const TCpuMatrix< Scalar_t > &A)