commit 78c28114203a60bde766c8d326f5229c07c64b55
parent 96ab9f361a59f9226fd4e3e7cd5cd240f13cce37
Author: David Freifeld <freifeld.david@gmail.com>
Date: Fri, 26 Jun 2020 12:17:17 -0700
More fixes, working on speedups
Diffstat:
7 files changed, 30 insertions(+), 32 deletions(-)
diff --git a/bpnn.cpp b/bpnn.cpp
@@ -57,22 +57,18 @@ void Network::add_layer(int nodes, char* name)
layers[length-1].activation = linear;
layers[length-1].activation_deriv = linear_deriv;
}
- if (strcmp(name, "step") == 0) {
+ else if (strcmp(name, "step") == 0) {
layers[length-1].activation = step;
layers[length-1].activation_deriv = step_deriv;
}
- if (strcmp(name, "bipolar") == 0) {
+ else if (strcmp(name, "bipolar") == 0) {
layers[length-1].activation = bipolar;
layers[length-1].activation_deriv = bipolar_deriv;
}
- if (strcmp(name, "lecun_tanh") == 0) {
+ else if (strcmp(name, "lecun_tanh") == 0) {
layers[length-1].activation = lecun_tanh;
layers[length-1].activation_deriv = lecun_tanh_deriv;
}
- if (strcmp(name, "logit") == 0) {
- layers[length-1].activation = logit;
- layers[length-1].activation_deriv = logit_deriv;
- }
else if (strcmp(name, "relu") == 0) {
layers[length-1].activation = rectifier(linear);
layers[length-1].activation_deriv = rectifier(linear_deriv);
diff --git a/example.cpp b/example.cpp
@@ -3,13 +3,14 @@
int main()
{
- Network net ("./data_banknote_authentication.txt", 10, 0.01, 0.001);
- net.add_layer(4, "linear");
- net.add_layer(5, "sigmoid");
- net.set_activation(1, lecun_tanh, lecun_tanh_deriv);
- net.add_layer(1, "resig");
- net.initialize();
- net.list_net();
- net.train(50);
+ // Network net ("./data_banknote_authentication.txt", 10, 0.01, 0.001);
+ // net.add_layer(4, "linear");
+ // net.add_layer(5, "sigmoid");
+ // net.set_activation(1, lecun_tanh, lecun_tanh_deriv);
+ // net.add_layer(1, "resig");
+ // net.initialize();
+ // net.list_net();
+ // net.train(50);
+ printf("%d\n", wc("./bpnn.cpp"));
// net.list_net();
}
diff --git a/example.py b/example.py
@@ -3,11 +3,10 @@ import numpy
import time
init = time.time()
-net = mrbpnn.Network("./data_banknote_authentication.txt", 10, 0.0155, 0.0155);
+net = mrbpnn.Network("./data_banknote_authentication.txt", 10, 0.0155, 0.03);
net.add_layer(4, "linear");
-net.add_layer(5, "sigmoid");
-net.set_activation(1, lecun_tanh, lecun_tanh_deriv);
-net.add_layer(1, "sigmoid");
+net.add_layer(5, "lecun_tanh");
+net.add_layer(1, "resig");
net.initialize();
initend = time.time()
net.train(50);
diff --git a/kerasdemo.py b/kerasdemo.py
@@ -23,7 +23,7 @@ def lecun_tanh(x):
init = time.time()
# load the dataset
-dataset = loadtxt('extra.txt', delimiter=',')
+dataset = loadtxt('data_banknote_authentication.txt', delimiter=',')
# split into input (X) and output (y) variables
X = dataset[:,0:4]
y = dataset[:,4]
@@ -33,7 +33,7 @@ model.add(Dense(4, input_dim=4, activation='linear'))
model.add(Dense(5, activation=lecun_tanh))
model.add(Dense(1, activation='sigmoid'))
# compile the keras model
-opt = keras.optimizers.SGD(lr=0.01)
+opt = keras.optimizers.SGD(lr=0.0155)
model.compile(loss='mse', optimizer=opt, metrics=['accuracy'])
# fit the keras model on the dataset
initend = time.time()
diff --git a/readme.md b/readme.md
@@ -1,7 +1,7 @@
# ML in Parallel
## About
-"ML in Parallel" (better name pending) is a work-in-progress machine learning library written in C++ designed to run quickly and in parallel. Parallelization is achieved by utilizing Google's powerful MapReduce framework with [a custom implementation in C](https://github.com/richardfeynmanrocks/mapreduce), and the compiled nature of C++/C as well as the optimizations enabled by gcc's O2 layer enable further speedups. On the same benchmark task (of running a small neural network for 50 epochs on a specified dataset) "ML in Parallel" can run up to 45x faster than a simple Keras program. Python bindings are being developed for this network to allow similar ease of usage to Keras while maintaining the enhanced speed.
+"ML in Parallel" (better name pending) is a work-in-progress machine learning library written in C++ designed to run quickly and in parallel. Parallelization is achieved by utilizing Google's powerful MapReduce framework with [a custom implementation in C](https://github.com/richardfeynmanrocks/mapreduce), and the compiled nature of C++/C as well as the optimizations enabled by gcc's O2 layer enable further speedups. On the same benchmark task (of running a small neural network for 50 epochs on a specified dataset) In some preliminary benchmarks "ML in Parallel" has ran up to 50x faster than a simple Keras program. This library is also easily accessible
## Benchmark Info
Coming soon: A detailed rundown of the speed of "ML in Parallel" vs popular machine learning libraries for Python (and eventually comparisons to C++ libraries as well) as well as a handy and flexible Python script for creating benchmark graphs on the fly.
diff --git a/utils.cpp b/utils.cpp
@@ -23,9 +23,6 @@ double lecun_tanh_deriv(double x) {return 1.14393 * pow(1.0/cosh(2.0/3 * x),2);}
double tanh(double x) {return tanh(x);} // For sake of symmetry.
double tanh_deriv(double x) {return pow(1.0/cosh(x),2);}
-double logit(double x) {return log(x/(1-x));}
-double logit_deriv(double x) {return 1/(pow(x,2)-x);}
-
double step(double x)
{
if (x > 0) return 1;
@@ -50,7 +47,7 @@ std::function<double(double)> rectifier(double (*activation)(double))
return rectified;
}
-static uintmax_t wc(char const *fname)
+uintmax_t wc(char const *fname)
{
static const auto BUFFER_SIZE = 16*1024;
int fd = open(fname, O_RDONLY);
@@ -58,7 +55,7 @@ static uintmax_t wc(char const *fname)
exit(1);
/* Advise the kernel of our access pattern. */
- // posix_fadvise(fd, 0, 0, 1); // FDADVICE_SEQUENTIAL
+ //posix_fadvise(fd, 0, 0, 1); // FDADVICE_SEQUENTIAL
char buf[BUFFER_SIZE + 1];
uintmax_t lines = 0;
@@ -69,10 +66,16 @@ static uintmax_t wc(char const *fname)
exit(1);
if (!bytes_read)
break;
-
- for(char *p = buf; (p = (char*) memchr(p, '\n', (buf + bytes_read) - p)); ++p)
- printf("Line? %s END\n", buf);
+ char* prevp;
+ for(char *p = buf; (p = (char*) memchr(p, '\n', (buf + bytes_read) - p)); ++p) {
+ if (lines > 1) {
+ for (in i = 0; p+i < prevp; i++) {
+ printf("START%sEND\n", p+i);
+ }
+ }
+ prevp = p;
++lines;
+ }
}
return lines;
diff --git a/utils.hpp b/utils.hpp
@@ -11,10 +11,9 @@ double step(double x);
double step_deriv(double x);
double bipolar(double x);
double bipolar_deriv(double x);
-double logit(double x);
-double logit_deriv(double x);
double lecun_tanh(double x);
double lecun_tanh_deriv(double x);
std::function<double(double)> rectifier(double (*activation)(double));
+uintmax_t wc(char const *fname);
#endif /* MODULE_H */