commit e32f2a9777726208d9f3877c6baceb849403aa2e
parent 17ace7c4da8fd6e11435e2ebac925bd6ccf44f4e
Author: David Freifeld <freifeld.david@gmail.com>
Date: Fri, 9 Apr 2021 21:16:37 -0700
Merge branch 'perf'
Diffstat:
2 files changed, 43 insertions(+), 10 deletions(-)
diff --git a/src/bpnn.hpp b/src/bpnn.hpp
@@ -12,6 +12,11 @@
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
+<<<<<<< HEAD
+=======
+#include <gsl/gsl_assert>
+#include <lz4.h>
+>>>>>>> perf
namespace Jacobian {
#define BUFFER_SIZE 600*1024
diff --git a/src/utils.cpp b/src/utils.cpp
@@ -18,27 +18,55 @@
#include <sys/stat.h>
#include <Eigen/Dense>
+<<<<<<< HEAD
#include "utils.hpp"
namespace Jacobian {
namespace activations {
float sigmoid(float x) {return 1.0/(1+exp(-x));}
float sigmoid_deriv(float x) {return 1.0/(1+exp(-x)) * (1 - 1.0/(1+exp(-x)));}
+=======
+inline float sgn(float val) {return (0.0f < val) - (val < 0.0f);}
+
+double fexp(double val)
+{
+ long tmp = static_cast<long>(1512775 * val + 1072632447) << 32;
+ return *reinterpret_cast<double*>(&tmp);
+}
+
+float ftanh(float x)
+{
+ return (x*(10+pow(x,2))*(60+pow(x,2)))/
+ (600+(270*pow(x,2))+(11*pow(x,4))+(pow(x,6)/24));
+}
+
+//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.
+// Yes, these functions may be a frustrating to read but they're just equations and I want to conserve space.
+
+float sigmoid(float x) {return 1.0/(1+fexp(-x));}
+float sigmoid_deriv(float x) {return 1.0/(1+fexp(-x)) * (1 - 1.0/(1+fexp(-x)));}
+>>>>>>> perf
float linear(float x) {return x;}
float linear_deriv(float x) {return 1;}
-float lecun_tanh(float x) {return 1.7159 * tanh((2.0/3) * x);}
-float lecun_tanh_deriv(float x) {return 1.14393 * pow(1.0/cosh(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 (exp(x)/(exp(x)+1));}
-float inverse_logit_deriv(float x) {return (exp(x)/pow(exp(x)+1, 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));}
-float softplus(float x) {return log(1+exp(x));}
-float softplus_deriv(float x) {return exp(x)/(exp(x)+1);}
+float softplus(float x) {return log(1+fexp(x));}
+float softplus_deriv(float x) {return fexp(x)/(fexp(x)+1);}
-float cloglog(float x) {return 1-exp(-exp(x));}
-float cloglog_deriv(float x) {return exp(x-exp(x));}
+float cloglog(float x) {return 1-fexp(-fexp(x));}
+float cloglog_deriv(float x) {return fexp(x-fexp(x));}
float step(float x)
{
@@ -55,8 +83,8 @@ float bipolar(float x)
}
float bipolar_deriv(float x) {return 0;}
-float bipolar_sigmoid(float x) {return (1-exp(-x))/(1+exp(-x));}
-float bipolar_sigmoid_deriv(float x) {return (2*exp(x))/(pow(exp(x)+1,2));}
+float bipolar_sigmoid(float x) {return (1-fexp(-x))/(1+fexp(-x));}
+float bipolar_sigmoid_deriv(float x) {return (2*fexp(x))/(pow(fexp(x)+1,2));}
float hard_tanh(float x) {return fmax(-1, fmin(1,x));}
float hard_tanh_deriv(float x)