commit 8470beaa2072bcdda5e5c3b438411e2e44b19ebd
parent e0f910c0a16cb8165ab4770891f9c161552830ac
Author: David Freifeld <freifeld.david@gmail.com>
Date: Thu, 25 Jun 2020 12:45:36 -0700
Full boilerplate, lots of segfaults
Diffstat:
| M | bpnn.cpp | | | 69 | ++++++++++----------------------------------------------------------- |
| M | example.cpp | | | 14 | +++++++------- |
2 files changed, 17 insertions(+), 66 deletions(-)
diff --git a/bpnn.cpp b/bpnn.cpp
@@ -37,7 +37,7 @@ Layer::Layer(int batch_sz, int nodes)
dZ = new Eigen::MatrixXd (batch_sz, nodes);
}
-void Layer::initWeights(Layer next)
+void Layer::init_weights(Layer next)
{
weights = new Eigen::MatrixXd (contents->cols(), next.contents->cols());
int nodes = weights->cols();
@@ -57,13 +57,20 @@ Network::Network(char* path, int batch_sz, float learn_rate, float bias_rate)
batches = 0;
}
-void add_layer(int nodes, char* activation)
+void Network::add_layer(int nodes, char* activation)
{
length++;
- layers.push_back(batch_size, nodes);
+ layers.emplace_back(batch_size, nodes);
set_activation(length-1, activation);
}
+void Network::initialize()
+{
+ for (int i = 0; i < length-1; i++) {
+ layers[i].init_weights(layers[i+1]);
+ }
+}
+
void Network::set_activation(int index, char* name)
{
if (strcmp(name, "sigmoid") == 0) {
@@ -300,59 +307,3 @@ void Network::train(int total_epochs)
rewind(data);
}
}
-
-void demo(int total_epochs)
-{
- int linecount = prep_file("./extra.txt", "./shuffled.txt");
- Network net ("./shuffled.txt", 4, 1, 1, 5, 10, 1);
- float epoch_cost = 1000;
- float epoch_accuracy = -1;
- int epochs = 0;
-
- printf("Beginning train on %i instances for %i epochs...\n", linecount, total_epochs);
- 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 <= linecount-net.batch_size; i+=net.batch_size) {
- auto feed_begin = std::chrono::high_resolution_clock::now();
- net.feedforward();
- auto back_begin = std::chrono::high_resolution_clock::now();
- net.backpropagate();
- auto cost_begin = std::chrono::high_resolution_clock::now();
- cost_sum += net.cost();
- auto acc_begin = std::chrono::high_resolution_clock::now();
- acc_sum += net.accuracy();
- auto batch_begin = std::chrono::high_resolution_clock::now();
- if (i != linecount-net.batch_size) {
- net.next_batch();
- }
- auto loop_end = std::chrono::high_resolution_clock::now();
- times[0] += std::chrono::duration_cast<std::chrono::nanoseconds>(back_begin - feed_begin).count() / pow(10,9);
- times[1] += std::chrono::duration_cast<std::chrono::nanoseconds>(cost_begin - back_begin).count() / pow(10,9);
- times[2] += std::chrono::duration_cast<std::chrono::nanoseconds>(acc_begin - cost_begin).count() / pow(10,9);
- times[3] += std::chrono::duration_cast<std::chrono::nanoseconds>(batch_begin - acc_begin).count() / pow(10,9);
- times[4] += std::chrono::duration_cast<std::chrono::nanoseconds>(loop_end - batch_begin).count() / pow(10,9);
- net.batches++;
- }
- epoch_accuracy = 1.0/((float) linecount/net.batch_size) * acc_sum;
- epoch_cost = 1.0/((float) linecount/net.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 - time %f - cost %f - acc %f\n", epochs+1, total_epochs, epochtime, epoch_cost, epoch_accuracy);
- printf("Avg time spent across %i batches: %lf on feedforward, %lf on backprop, %lf on cost, %lf on acc, %lf on next batch.\n", net.batches, times[0]/net.batches, times[1]/net.batches, times[2]/net.batches, times[3]/net.batches, times[4]/net.batches);
- printf("Time spent across epoch: %lf on feedforward, %lf on backprop, %lf on cost, %lf on acc, %lf on next batch, %lf other.\n", times[0], times[1], times[2], times[3], times[4], epochtime-times[0]-times[1]-times[2]-times[3]-times[4]);
- net.batches=1;
- epochs++;
- rewind(net.data);
- }
- // float newvals[4] = {0};
- // FILE* new = fopen("./predict.txt", "r");
- // fscanf(new, "%f, %f, %f, %f", &newvals[0], &newvals[1], &newvals[2], &newvals[3]);
- // net.update_layer(newvals, 4, 0);
- // net.feedforward();
- // net.list_net();
- //net.list_net();
- // printf("Test accuracy: %f\n", net.test("./test.txt"));
-}
diff --git a/example.cpp b/example.cpp
@@ -3,14 +3,14 @@
int main()
{
- Network net ("./extra.txt", 4, 1, 1, 2, 10, 0.1);
- net.set_activation(0, "linear");
- net.set_activation(1, "sigmoid");
- net.set_activation(2, "sigmoid");
- net.set_activation(3, "resig");
- net.list_net();
- net.train(50);
+ Network net ("./extra.txt", 10, 1, 0.01);
+ net.add_layer(4, "linear");
+ net.add_layer(5, "sigmoid");
+ net.add_layer(3, "sigmoid");
+ net.add_layer(1, "resig");
net.list_net();
+ //net.train(50);
+ //net.list_net();
//char line[1024];
//net.stream->getline(line, 1024);
//std::cout << line << "\n";