commit f3928cc2f8c6d187381ddc06272116920a49eb95
parent 7ca66f87df1e216d55ba34233c99085892a9ca44
Author: David Freifeld <freifeld.david@gmail.com>
Date: Sun, 28 Jun 2020 11:39:48 -0700
More work on speed + benchmarking
Diffstat:
5 files changed, 24 insertions(+), 12 deletions(-)
diff --git a/bpnn.cpp b/bpnn.cpp
@@ -62,10 +62,6 @@ void Network::add_layer(int nodes, char* name)
layers[length-1].activation = step;
layers[length-1].activation_deriv = step_deriv;
}
- else if (strcmp(name, "tanh") == 0) {
- layers[length-1].activation = mytanh;
- layers[length-1].activation_deriv = tanh_deriv;
- }
else if (strcmp(name, "lecun_tanh") == 0) {
layers[length-1].activation = lecun_tanh;
layers[length-1].activation_deriv = lecun_tanh_deriv;
diff --git a/example.cpp b/example.cpp
@@ -1,18 +1,34 @@
#include "bpnn.hpp"
#include "utils.hpp"
#include "unistd.h"
+#include <ctime>
int main()
{
- // sleep(30);
+ sleep(30);
Network net ("./data_banknote_authentication.txt", 10, 0.01, 0.001);
net.add_layer(4, "linear");
net.add_layer(5, "lecun_tanh");
- net.set_activation(1, lecun_tanh, lecun_tanh_deriv);
net.add_layer(1, "resig");
net.initialize();
// net.list_net();
net.train(500);
net.list_net();
//printf("%i\n", wc("./data_banknote_authentication.txt"));
+
+ // double x = 0.4235;
+ // auto bench_start = std::chrono::high_resolution_clock::now();
+ // x = tanh(x);
+ // printf("%lf", x);
+ // auto tanh_end = std::chrono::high_resolution_clock::now();
+ // cosh(x);
+ // auto cosh_end = std::chrono::high_resolution_clock::now();
+ // exp(x);
+ // auto exp_end = std::chrono::high_resolution_clock::now();
+ // log(x);
+ // auto log_end = std::chrono::high_resolution_clock::now();
+ // x = x - (1/3 * pow(x, 3)) + (2/15 * pow(x, 5)) - (17/315 * pow(x, 7));
+ // printf("%lf", x);
+ // auto pow_end = std::chrono::high_resolution_clock::now();
+ // std::cout << " TANH " << std::chrono::duration_cast<std::chrono::nanoseconds>(tanh_end - bench_start).count() << " COSH " << std::chrono::duration_cast<std::chrono::nanoseconds>(cosh_end - tanh_end).count() << " EXP " << std::chrono::duration_cast<std::chrono::nanoseconds>(exp_end - cosh_end).count() << " BETTER TANH? " << std::chrono::duration_cast<std::chrono::nanoseconds>(log_end - exp_end).count() << " POW " << std::chrono::duration_cast<std::chrono::nanoseconds>(pow_end - log_end).count() << "\n";
}
diff --git a/example.py b/example.py
@@ -6,11 +6,11 @@ def bench():
init = time.time()
net = mrbpnn.Network("./data_banknote_authentication.txt", 10, 0.0155, 0.03);
net.add_layer(4, "linear");
- net.add_layer(5, "lecun_tanh");
+ net.add_layer(5, "relu");
net.add_layer(1, "resig");
net.initialize();
initend = time.time()
- net.train(1);
+ net.train(50);
end = time.time()
return (end-init)
# print("%s: init %s" % (end-init, initend-init))
diff --git a/kerasdemo.py b/kerasdemo.py
@@ -23,7 +23,7 @@ def lecun_tanh(x):
init = time.time()
# load the dataset
-dataset = loadtxt('exoplanets.txt', delimiter=',')
+dataset = loadtxt('./data_banknote_authentication.txt', delimiter=',')
# split into input (X) and output (y) variables
X = dataset[:,1:5]
y = dataset[:,1]
diff --git a/utils.cpp b/utils.cpp
@@ -11,6 +11,9 @@
// 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.
+
+//double tanhapprox(double x) {return x - (1/3 * pow(x, 3)) + (2/15 * pow(x, 5)) - (17/315 * pow(x, 7));}
+
double sigmoid(double x) {return 1.0/(1+exp(-x));}
double sigmoid_deriv(double x) {return 1.0/(1+exp(-x)) * (1 - 1.0/(1+exp(x)));}
@@ -20,9 +23,6 @@ double linear_deriv(double x) {return 1;}
double lecun_tanh(double x) {return 1.7159 * tanh((2.0/3) * x);}
double lecun_tanh_deriv(double x) {return 1.14393 * pow(1.0/cosh(2.0/3 * x),2);}
-double mytanh(double x) {return tanh(x);}
-double tanh_deriv(double x) {return pow(1.0/cosh(x),2);}
-
double inverse_logit(double x) {return (exp(x)/(exp(x)+1));}
double inverse_logit_deriv(double x) {return (exp(x)/pow(exp(x)+1, 2));}