commit f8c9df06069f8b85b9f8ee610df4cd8cce9b9115
parent 1efffd4ab4ee1ad898852d0409bceeff28eb029e
Author: David Freifeld <freifeld.david@gmail.com>
Date: Mon, 18 Jan 2021 14:17:16 -0800
Rational function approximation of tanh
Diffstat:
1 file changed, 10 insertions(+), 11 deletions(-)
diff --git a/src/utils.cpp b/src/utils.cpp
@@ -18,9 +18,7 @@
#include <sys/stat.h>
#include <eigen3/Eigen/Dense>
-inline float sgn(float val) {
- return (0.0f < val) - (val < 0.0f);
-}
+inline float sgn(float val) {return (0.0f < val) - (val < 0.0f);}
double fexp(double val)
{
@@ -28,15 +26,14 @@ double fexp(double val)
return *reinterpret_cast<double*>(&tmp);
}
-float ftanh(float val)
+float ftanh(float x)
{
- return sgn(val) * (1 - 2/(fexp(2*abs(val))+1));
+ return (x*(10+pow(x,2))*(60+pow(x,2)))/
+ (600+(270*pow(x,2))+(11*pow(x,4))+(pow(x,6)/24));
}
-float fcosh(float val)
-{
- return (fexp(val) + fexp(-val)) * 0.5;
-}
+//float ftanh(float val) {return sgn(val) * (1 - 2/(fexp(2*abs(val))+1));}
+float fcosh(float val) {return (fexp(val) + fexp(-val)) * 0.5;}
// A bunch of hardcoded activation functions. Avoids much of the slowness of custom functions.
// Although the std::function makes it not the fastest way, the functionality is worth it.
@@ -48,8 +45,10 @@ float sigmoid_deriv(float x) {return 1.0/(1+fexp(-x)) * (1 - 1.0/(1+fexp(-x)));}
float linear(float x) {return x;}
float linear_deriv(float x) {return 1;}
-float lecun_tanh(float x) {return 1.7159 * ftanh((2.0/3) * x);}
-float lecun_tanh_deriv(float x) {return 1.14393 * pow(1.0/fcosh(2.0/3 * x),2);}
+float lecun_tanh(float x) {
+ //std::cout << ftanh(x) << " vs " << tanh(x) << "\n";
+ return 1.7159 * ftanh(0.66f * x);}
+float lecun_tanh_deriv(float x) {return 1.14393 * pow(1.0/fcosh(0.66f * x), 2);}
float inverse_logit(float x) {return (fexp(x)/(fexp(x)+1));}
float inverse_logit_deriv(float x) {return (fexp(x)/pow(fexp(x)+1, 2));}