-
Notifications
You must be signed in to change notification settings - Fork 0
/
MNIST.h
58 lines (47 loc) · 1.27 KB
/
MNIST.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
#pragma once
#include "Dataset.h"
namespace nn {
class MNIST : public Dataset {
public:
static const int INPUTS = 784, OUTPUTS = 10;
MNIST(const char* train_file, const char* test_file)
: train(train_file), test(test_file)
{}
std::vector<DataEntry> get_train_set() {
int label;
FILE* mnist_train = fopen(train, "r");
std::vector<DataEntry> dataset;
while (fscanf(mnist_train, "%d", &label) > 0) {
DataEntry entry(INPUTS, OUTPUTS);
for (int i = 0; i < OUTPUTS; i++) {
entry.label[i] = (label == i) ? 1 : 0;
}
for (int i = 0; i < INPUTS; i++) {
fscanf(mnist_train, "%lf", &entry.data[i]);
entry.data[i] /= 255.0;
}
dataset.push_back(std::move(entry));
}
return dataset;
}
std::vector<DataEntry> get_test_set() {
int label;
FILE* mnist_test = fopen(test, "r");
std::vector<DataEntry> dataset;
while (fscanf(mnist_test, "%d", &label) > 0) {
DataEntry entry(INPUTS, OUTPUTS);
for (int i = 0; i < OUTPUTS; i++) {
entry.label[i] = (label == i) ? 1 : 0;
}
for (int i = 0; i < INPUTS; i++) {
fscanf(mnist_test, "%lf", &entry.data[i]);
entry.data[i] /= 255.0;
}
dataset.push_back(std::move(entry));
}
return dataset;
}
private:
const char *train, *test;
};
}