commit f3466f7dd07d9be57b724d9b09ae6adb228f89ea
parent f3928cc2f8c6d187381ddc06272116920a49eb95
Author: David Freifeld <freifeld.david@gmail.com>
Date: Sun, 28 Jun 2020 15:13:45 -0700
Switch to icpc and more compile flags
Diffstat:
3 files changed, 7 insertions(+), 46 deletions(-)
diff --git a/Makefile b/Makefile
@@ -2,22 +2,17 @@
CXXFLAGS: mr_bpnn_2.cpp bpnn.cpp mapreduce.a
g++ -w -shared -std=c++11 -undefined dynamic_lookup `python3 -m pybind11 --includes` mr_bpnn_2.cpp bpnn.cpp utils.cpp mapreduce.a -o mrbpnn`python3-config --extension-suffix`
-all: executable
-
-test: CXXFLAGS = -w -shared -std=c++11 -undefined dynamic_lookup `python3 -m pybind11 --includes` mr_bpnn_2.cpp bpnn.cpp utils.cpp mapreduce.a -o mrbpnn`python3-config --extension-suffix` -O0
-test: build
-
fast: CXXFLAGS = -w -shared -std=c++11 -undefined dynamic_lookup `python3 -m pybind11 --includes` mr_bpnn_2.cpp bpnn.cpp utils.cpp mapreduce.a -o mrbpnn`python3-config --extension-suffix` -O3
fast: build
-faster: CXXFLAGS = -w -shared -std=c++11 -undefined dynamic_lookup `python3 -m pybind11 --includes` mr_bpnn_2.cpp bpnn.cpp utils.cpp mapreduce.a -o mrbpnn`python3-config --extension-suffix` -O3 -mavx -mfma -march=native -mfpmath=sse -D NDEBUG
+faster: CXXFLAGS = -w -shared -std=c++11 -undefined dynamic_lookup `python3 -m pybind11 --includes` mr_bpnn_2.cpp bpnn.cpp utils.cpp mapreduce.a ${MKLROOT}/lib/libmkl_intel_ilp64.a ${MKLROOT}/lib/libmkl_intel_thread.a ${MKLROOT}/lib/libmkl_core.a -liomp5 -lpthread -lm -ldl -o mrbpnn`python3-config --extension-suffix` -O3 -mavx -mfma -march=native -mfpmath=sse -DMKL_ILP64 -I${MKLROOT}/include -D EIGEN_USE_MKL_ALL -D NDEBUG
faster: build
-tradeoffs: CXXFLAGS = -w -shared -std=c++11 -undefined dynamic_lookup `python3 -m pybind11 --includes` mr_bpnn_2.cpp bpnn.cpp utils.cpp mapreduce.a -o mrbpnn`python3-config --extension-suffix` -O3 -mavx -mfma -march=native -mfpmath=sse -ffinite-math-only -fno-math-errno -ffp-contract=fast -D NDEBUG
+tradeoffs: CXXFLAGS = -w -shared -std=c++11 -undefined dynamic_lookup `python3 -m pybind11 --includes` mr_bpnn_2.cpp bpnn.cpp utils.cpp mapreduce.a ${MKLROOT}/lib/libmkl_intel_ilp64.a ${MKLROOT}/lib/libmkl_intel_thread.a ${MKLROOT}/lib/libmkl_core.a -liomp5 -lpthread -lm -ldl -o mrbpnn`python3-config --extension-suffix` -O3 -mavx -mfma -march=native -mfpmath=sse -DMKL_ILP64 -I${MKLROOT}/include -no-prec-div -no-prec-sqrt -fimf-precision=low -fast-transcendentals -ffinite-math-only -fno-math-errno -ffp-contract=fast -D EIGEN_USE_MKL_ALL -D NDEBUG
tradeoffs: build
reckless: CXXFLAGS = -O3
reckless: build
build:
- $(CXX) $(CXXFLAGS)
+ icpc $(CXXFLAGS)
diff --git a/bpnn.cpp b/bpnn.cpp
@@ -108,19 +108,16 @@ void Network::set_activation(int index, std::function<double(double)> custom, st
void Network::feedforward()
{
- //auto early_activ_begin = std::chrono::high_resolution_clock::now();
for (int j = 0; j < layers[0].contents->rows(); j++) {
for (int k = 0; k < layers[0].contents->cols(); k++) {
(*layers[0].dZ)(j,k) = layers[0].activation_deriv((*layers[0].contents)(j,k));
(*layers[0].contents)(j,k) = layers[0].activation((*layers[0].contents)(j,k));
}
}
- //auto mul_begin = std::chrono::high_resolution_clock::now();
for (int i = 0; i < length-1; i++) {
*layers[i+1].contents = (*layers[i].contents) * (*layers[i].weights);
*layers[i+1].contents += *layers[i+1].bias;
}
- //auto activ_begin = std::chrono::high_resolution_clock::now();
for (int i = 1; i < length; i++) {
for (int j = 0; j < layers[i].contents->rows(); j++) {
for (int k = 0; k < layers[i].contents->cols(); k++) {
@@ -129,11 +126,6 @@ void Network::feedforward()
}
}
}
- //auto feed_end = std::chrono::high_resolution_clock::now();
- //double early_activ = std::chrono::duration_cast<std::chrono::nanoseconds>(mul_begin - early_activ_begin).count() / pow(10,9);
- //double mul = std::chrono::duration_cast<std::chrono::nanoseconds>(activ_begin - mul_begin).count() / pow(10,9);
- //double activ = std::chrono::duration_cast<std::chrono::nanoseconds>(feed_end - activ_begin).count() / pow(10,9);
- //printf("%lf on early activation, %lf on multiply, %lf on rest of activation\n", early_activ, mul, activ);
}
void Network::list_net()
@@ -195,13 +187,11 @@ void Network::update_layer(float* vals, int datalen, int index)
int Network::next_batch()
{
- //auto init_begin = std::chrono::high_resolution_clock::now();
char line[1024] = {' '};
int inputs = layers[0].contents->cols();
int datalen = batch_size * inputs;
float batch[datalen];
int label = -1;
- //auto get_begin = std::chrono::high_resolution_clock::now();
for (int i = 0; i < batch_size; i++) {
if (fgets(line, 1024, data)==NULL) {
break;
@@ -215,12 +205,8 @@ int Network::next_batch()
}
(*labels)(i, 0) = strtod(p, NULL);
}
- //auto get_end = std::chrono::high_resolution_clock::now();
float* batchptr = batch;
update_layer(batchptr, datalen, 0);
- //auto update_end = std::chrono::high_resolution_clock::now();
- //std::cout << " INIT " << std::chrono::duration_cast<std::chrono::nanoseconds>(get_begin - init_begin).count() / pow(10,9) << " GET " << std::chrono::duration_cast<std::chrono::nanoseconds>(get_end - get_begin).count() / pow(10,9) << " UPDATE " << std::chrono::duration_cast<std::chrono::nanoseconds>(update_end - get_end).count() / pow(10,9) << " TOTAL " << std::chrono::duration_cast<std::chrono::nanoseconds>(update_end - init_begin).count() / pow(10,9) << "\n";
- // std::cout << "Next batch is\n" << *layers[0].contents << "\nwith labels\n"<<*labels << "\n\n";
return 0;
}
@@ -294,38 +280,21 @@ void Network::train(int total_epochs)
// printf("Beginning train on %i instances for %i epochs...\n", instances, total_epochs);
double batch_time = 0;
while (epochs < total_epochs) {
- //auto ep_begin = std::chrono::high_resolution_clock::now();
float cost_sum = 0;
float acc_sum = 0;
- double times[5] = {0};
for (int i = 0; i <= instances-batch_size; i+=batch_size) {
- //auto batch_begin = std::chrono::high_resolution_clock::now();
if (i != instances-batch_size) { // Don't try to advance batch on final batch.
next_batch();
}
- //auto feed_begin = std::chrono::high_resolution_clock::now();
feedforward();
- //auto back_begin = std::chrono::high_resolution_clock::now();
backpropagate();
- //auto cost_begin = std::chrono::high_resolution_clock::now();
cost_sum += cost();
- //auto acc_begin = std::chrono::high_resolution_clock::now();
acc_sum += accuracy();
- //auto loop_end = std::chrono::high_resolution_clock::now();
- // times[0] += std::chrono::duration_cast<std::chrono::nanoseconds>(feed_begin - batch_begin).count() / pow(10,9);
- //times[1] += std::chrono::duration_cast<std::chrono::nanoseconds>(back_begin - feed_begin).count() / pow(10,9);
- //times[2] += std::chrono::duration_cast<std::chrono::nanoseconds>(cost_begin - back_begin).count() / pow(10,9);
- //times[3] += std::chrono::duration_cast<std::chrono::nanoseconds>(acc_begin - cost_begin).count() / pow(10,9);
- //times[4] += std::chrono::duration_cast<std::chrono::nanoseconds>(loop_end - acc_begin).count() / pow(10,9);
batches++;
}
epoch_accuracy = 1.0/((float) instances/batch_size) * acc_sum;
epoch_cost = 1.0/((float) instances/batch_size) * cost_sum;
- //auto ep_end = std::chrono::high_resolution_clock::now();
- //double epochtime = (double) std::chrono::duration_cast<std::chrono::nanoseconds>(ep_end-ep_begin).count() / pow(10,9);
printf("Epoch %i/%i - cost %f - acc %f\n", epochs+1, total_epochs, epoch_cost, epoch_accuracy);
- //printf("Avg time spent across %i batches: %lf on next batch, %lf on feedforward, %lf on backprop, %lf on cost, %lf on acc.\n", batches, times[0]/batches, times[1]/batches, times[2]/batches, times[3]/batches, times[4]/batches);
- //printf("Time spent across epoch: %lf on next batch, %lf on feedforward, %lf on backprop, %lf on cost, %lf on acc.\n\n", times[0], times[1], times[2], times[3], times[4], epochtime-times[0]-times[1]-times[2]-times[3]-times[4]);
batches=1;
epochs++;
rewind(data);
diff --git a/kerasdemo.py b/kerasdemo.py
@@ -18,25 +18,22 @@ from keras import backend as K
from keras.models import Sequential
from keras.layers import Activation
from keras.layers import Dense
-def lecun_tanh(x):
- return 1.7159 * K.tanh((2.0/3) * x)
-
init = time.time()
# load the dataset
-dataset = loadtxt('./data_banknote_authentication.txt', delimiter=',')
+dataset = loadtxt('data_banknote_authentication.txt', delimiter=',')
# split into input (X) and output (y) variables
-X = dataset[:,1:5]
-y = dataset[:,1]
+X = dataset[:,0:4]
+y = dataset[:,4]
# define the keras model
model = Sequential()
model.add(Dense(4, input_dim=4, activation='linear'))
+model.add(Dense(5, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# compile the keras model
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()
-print(model.summary())
model.fit(X, y, epochs=50, batch_size=10)
# evaluate the keras model
#_, accuracy = model.evaluate(X, y)