commit 47676f11c6f8260e0aa2350557de3b0d0290af48
parent 53bab8ea1ab08422d3137c9181726367c7d3b451
Author: David Freifeld <freifeld.david@gmail.com>
Date: Fri, 25 Sep 2020 19:46:00 -0700
Everything is broken now
Diffstat:
5 files changed, 27 insertions(+), 97 deletions(-)
diff --git a/checks.cpp b/checks.cpp
@@ -45,10 +45,10 @@ void regularization_check(int& sanity_passed, int& total_checks)
net.list_net();
Network copy1 = explicit_copy(net);
Network copy2 = explicit_copy(net);
- copy1.next_batch();
+ copy1.next_batch(copy1.data);
copy1.feedforward();
- copy2.next_batch();
+ copy2.next_batch(copy2.data);
copy2.feedforward();
net.list_net();
if (copy1.cost() > copy2.cost()) {
@@ -64,7 +64,7 @@ void zero_check(int& sanity_passed, int& total_checks)
{
Network net = default_net();
std::cout << "Zero-cost check...";
- net.next_batch();
+ net.next_batch(net.data);
float finalcost;
for (int i = 0; i < 10000; i++) {
net.feedforward();
@@ -87,7 +87,7 @@ void floating_point_check(int& sanity_passed, int& total_checks)
{
Network net = default_net();
std::cout << "Update floating-point check...";
- net.next_batch();
+ net.next_batch(net.data);
net.feedforward();
net.backpropagate();
for (int i = 0; i < net.length-1; i++) {
diff --git a/example.cpp b/example.cpp
@@ -19,7 +19,7 @@
double bench(int batch_sz, int epochs)
{
auto start = std::chrono::high_resolution_clock::now();
- Network net ("./data_banknote_authentication.bak.bin", batch_sz, 0.0155, 0.03, L2, 0, 0.9);
+ Network net ("./data_banknote_authentication.txt", batch_sz, 0.0155, 0.03, L2, 0, 0.9);
net.add_layer(4, "linear", linear, linear_deriv);
net.add_layer(5, "lecun_tanh", lecun_tanh, lecun_tanh_deriv);
net.add_layer(2, "linear", linear, linear_deriv);
@@ -34,7 +34,6 @@ double bench(int batch_sz, int epochs)
int main(int argc, char** argv)
{
if (argc < 2) {
- prep();
std::cout << "Invalid command! Either pass a special option or pass two integers - batch_size and epochs (in that order)." << "\n";
exit(1);
}
diff --git a/src/bpnn.cpp b/src/bpnn.cpp
@@ -15,6 +15,8 @@
#define SHUFFLED_PATH "./shuffled.txt"
#define VAL_PATH "./test.txt"
#define TRAIN_PATH "./train.txt"
+#define VAL_BIN_PATH "./test.bin"
+#define TRAIN_BIN_PATH "./train.bin"
#if (AVX)
#define cwise_product(a,b) avx_product(a, b)
@@ -74,8 +76,10 @@ Network::Network(char* path, int batch_sz, float learn_rate, float bias_rate, Re
{
int total_instances = prep_file(path, SHUFFLED_PATH);
val_instances = split_file(SHUFFLED_PATH, total_instances, ratio);
- data = open(TRAIN_PATH, O_RDONLY | O_NONBLOCK);
- val_data = open(VAL_PATH, O_RDONLY | O_NONBLOCK);
+ prep(TRAIN_PATH, TRAIN_BIN_PATH);
+ prep(VAL_PATH, VAL_BIN_PATH);
+ data = open(TRAIN_BIN_PATH, O_RDONLY | O_NONBLOCK);
+ val_data = open(VAL_BIN_PATH, O_RDONLY | O_NONBLOCK);
instances = total_instances - val_instances;
assert(batch_size > 0 || batch_size < instances);
decay = [this]() -> void {};
@@ -340,14 +344,14 @@ float Network::validate(char* path)
float costsum = 0;
float accsum = 0;
for (int i = 0; i <= val_instances-batch_size; i+=batch_size) {
- next_batch();
+ next_batch(val_data);
feedforward();
costsum += cost();
accsum += accuracy();
}
val_acc = 1.0/(static_cast<float>(val_instances/batch_size)) * accsum;
val_cost = 1.0/(static_cast<float>(val_instances/batch_size)) * costsum;
- val_data = open(VAL_PATH, O_RDONLY & O_NONBLOCK);
+ val_data = open(VAL_PATH, O_RDONLY | O_NONBLOCK);
return 0;
}
@@ -357,7 +361,7 @@ void Network::train()
float acc_sum = 0;
for (int i = 0; i <= instances-batch_size; i+=batch_size) {
if (early_stop == true && get_val_cost() < threshold) return;
- if (i != instances-batch_size) next_batch();
+ if (i != instances-batch_size) next_batch(data);
feedforward();
backpropagate();
cost_sum += cost();
@@ -369,7 +373,7 @@ void Network::train()
validate(VAL_PATH);
if (silenced == false) printf("Epoch %i complete - cost %f - acc %f - val_cost %f - val_acc %f\n", epochs, epoch_cost, epoch_acc, val_cost, val_acc);
batches=1;
- data = open(TRAIN_PATH, O_RDONLY | O_NONBLOCK);
+ data = open("./train.bin", O_RDONLY | O_NONBLOCK);
decay();
epochs++;
}
diff --git a/src/bpnn.hpp b/src/bpnn.hpp
@@ -19,7 +19,7 @@
#include <fcntl.h>
#include <unistd.h>
-#define BUFFER_SIZE 16*1024
+#define BUFFER_SIZE 600*1024
enum Regularization {L1, L2};
class Layer {
@@ -95,7 +95,7 @@ public:
float cost();
float accuracy();
void backpropagate();
- int next_batch();
+ int next_batch(int fd);
float validate(char* path);
void train();
@@ -123,7 +123,7 @@ struct ValueError : public std::exception
return error_message;
}
};
-
+void prep(char* rname, char* wname);
#define MAXLINE 1024
#if (!RECKLESS)
diff --git a/src/data.cpp b/src/data.cpp
@@ -12,11 +12,10 @@ inline float scan(char **p)
return n*neg;
}
-void prep()
+void prep(char* rname, char* wname)
{
- FILE* wptr = fopen("../../data_banknote_authentication.bin", "wb");
- static const auto BUFFER_SIZE = 512*1024;
- int fd = open("../../data_banknote_authentication.txt", O_RDONLY | O_NONBLOCK);
+ FILE* wptr = fopen(wname, "wb");
+ int fd = open(rname, O_RDONLY | O_NONBLOCK);
if(fd == -1) {
printf("fd == -1\n");
}
@@ -40,97 +39,25 @@ void prep()
}
}
-float mmap_read()
+int Network::next_batch(int fd)
{
- static const auto BUFFER_SIZE = 600*1024;
- int fd = open("../../data_banknote_authentication.bin", O_RDONLY | O_NONBLOCK);
- if(fd == -1) {
- printf("Cannot open file.");
- exit(1);
- }
- int rc, ii;
- struct stat st;
- size_t size;
- rc = fstat(fd, &st);
- size=st.st_size;
- float* ptr = (float*) mmap(0, size, PROT_READ, MAP_SHARED, fd, 0);
- madvise(ptr, size, POSIX_MADV_SEQUENTIAL);
- char buf[BUFFER_SIZE];
uintmax_t lines = 0;
- float tmp;
- for (ii=0; ii < size/sizeof *ptr; ii++) {
- // Nothin.
- }
- rc = munmap(ptr, size);
- close(fd);
- return tmp;
-}
-
-float vec_read()
-{
-#define CHUNK_SZ (200*1024)
-#define BUFFER_SZ (600*1024)
-#define NUM_CHUNKS (BUFFER_SZ/CHUNK_SZ)
- int fd = open("../../data_banknote_authentication.bin", O_RDONLY | O_NONBLOCK);
- char rawbuf[BUFFER_SZ];
- char* buf = (char*) rawbuf;
- iovec iovecs[NUM_CHUNKS];
- for (int i = 0; i < BUFFER_SZ; i+=CHUNK_SZ) {
- iovecs[i/CHUNK_SZ].iov_base = buf + i;
- iovecs[i/CHUNK_SZ].iov_len = CHUNK_SZ;
- }
- while(size_t bytes_read = readv(fd, iovecs, NUM_CHUNKS))
- {
- if(bytes_read == (size_t)-1) {
- printf("\n%zu\n", bytes_read);
- printf("bytes_read == (size_t)-1\n");
- exit(1);
- }
- if (!bytes_read) break;
- }
- return 0;
-}
-
-float std_read(int BUFFER_SIZE)
-{
- int fd = open("../../data_banknote_authentication.bin", O_RDONLY | O_NONBLOCK);
- fcntl(fd, F_RDADVISE);
- if(fd == -1) {
-
-
-int Network::next_batch()
-{
- uintmax_t lines = 0;
- while(size_t bytes_read = read(data, buf, BUFFER_SIZE))
+ while(size_t bytes_read = read(fd, buf, BUFFER_SIZE))
{
if (!bytes_read) break;
for(char *p = buf; p < buf+BUFFER_SIZE && lines < 10;) {
- for (int i=0; i<<layers[0].contents->cols(); ++i) {
+ for (int i=0; i<layers[0].contents->cols(); ++i) {
+ printf("%f\n", *((float*)p));
(*layers[0].contents)(lines,i) = *((float*)p);
p += sizeof(float);
}
+ printf("%f\n", *((float*)p));
(*labels)(lines,0) = *((float*)p);
p += sizeof(float);
++lines;
}
+
}
- // char line[MAXLINE];
- // int inputs = layers[0].contents->cols();
- // int datalen = batch_size * inputs;
- // float batch[datalen];
- // for (int i = 0; i < batch_size; i++) {
- // fgets(line, MAXLINE, data);
- // char *p;
- // p = strtok(line,",");
- // for (int j = 0; j < inputs; j++) {
- // batch[j + (i * inputs)] = strtod(p, NULL);
- // p = strtok(NULL,",");
- // }
- // (*labels)(i, 0) = strtod(p, NULL);
- // }
- // float* batchptr = batch;
- // update_layer(batchptr, datalen, 0);
- // return 0;
}
int prep_file(char* path, char* out_path)