-
Notifications
You must be signed in to change notification settings - Fork 0
/
MNIST_bin.h
64 lines (51 loc) · 1.39 KB
/
MNIST_bin.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include "Dataset.h"
#include <cstdio>
namespace nn {
class MNIST_bin : public Dataset {
public:
static const int INPUTS = 784, OUTPUTS = 10;
MNIST_bin(const char* train_file, const char* test_file)
: train(train_file), test(test_file)
{}
std::vector<DataEntry> get_train_set() override {
FILE* mnist_train = fopen(train, "rb");
std::vector<DataEntry> dataset;
mnist_entry item;
while (fread(&item, sizeof(item), 1, mnist_train) > 0) {
DataEntry entry(INPUTS, OUTPUTS);
for (int i = 0; i < OUTPUTS; i++) {
entry.label[i] = (item.label == i) ? 1 : 0;
}
for (int i = 0; i < INPUTS; i++) {
entry.data[i] = item.data[i] / 255.0;
}
dataset.push_back(std::move(entry));
}
return dataset;
}
std::vector<DataEntry> get_test_set() override {
FILE* mnist_test = fopen(test, "rb");
std::vector<DataEntry> dataset;
mnist_entry item;
while (fread(&item, sizeof(item), 1, mnist_test) > 0) {
DataEntry entry(INPUTS, OUTPUTS);
for (int i = 0; i < OUTPUTS; i++) {
entry.label[i] = (item.label == i) ? 1 : 0;
}
for (int i = 0; i < INPUTS; i++) {
entry.data[i] = item.data[i] / 255.0;
}
dataset.push_back(std::move(entry));
}
return dataset;
}
private:
struct mnist_entry {
int label;
unsigned char data[INPUTS];
};
const char *train, *test;
};
}