-
Notifications
You must be signed in to change notification settings - Fork 2
/
data.h
141 lines (108 loc) · 2.76 KB
/
data.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#pragma once
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <vector>
#include "util.h"
namespace con {
namespace {
using std::ifstream;
Vec mean;
}
struct Sample {
Sample() {}
Vec input;
int label;
};
void computeMean(const vector<Sample> &data, Vec *mean) {
if (!data.size()) {
return;
}
mean->clear();
mean->resize(data[0].input.size());
for (int i = 0; i < mean->size(); i++) {
mean->at(i) = 0;
for (int j = 0; j < data.size(); j++) {
mean->at(i) += data[j].input[i];
}
mean->at(i) /= data.size();
}
}
void shuffle(vector<Sample> *data) {
for (int i = 0; i < data->size(); i++) {
int j = rand() % (i + 1);
std::swap(data->at(i), data->at(j));
}
}
void refineMean(const Vec &mean, Vec *input) {
for (int j = 0; j < input->size(); j++) {
input->at(j) -= mean[j];
}
}
void refineMean(const Vec &mean, vector<Sample> *data) {
for (int i = 0; i < data->size(); i++) {
refineMean(mean, &data->at(i).input);
}
}
void readFrom(const string &fileName, vector<Sample> *data) {
cout << "Reading from " << fileName << endl;
ifstream input(fileName.c_str(), ifstream::binary);
if (input.is_open()) {
char *mem = new char[3073];
for (int i = 0; i < 10000; i++) {
if (i % 1000 == 0) {
cout << "Sample " << i << endl;
}
data->push_back(Sample());
input.read(mem, 3073);
data->back().label = mem[0];
for (int j = 1; j <= 3072; j++) {
double x = (double)(unsigned char)mem[j];
data->back().input.push_back(x);
}
}
delete[] mem;
}
}
void readTrain(vector<Sample> *data) {
data->clear();
for (char c = '1'; c <= '5'; c++) {
string fileName = "data/data_batch_";
fileName += c;
fileName += ".bin";
readFrom(fileName, data);
}
cout << "Computing mean" << endl;
computeMean(*data, &mean);
cout << "Refining mean" << endl;
refineMean(mean, data);
cout << "Shuffling" << endl;
shuffle(data);
}
void readTest(vector<Sample> *data) {
data->clear();
readFrom("data/test_batch.bin", data);
refineMean(mean, data);
}
void printOutput(const vector<int> &result) {
std::ofstream output("output", std::ofstream::out);
output << "id,label" << std::endl;
vector<std::string> label = {
"airplane",
"automobile",
"bird",
"cat",
"deer",
"dog",
"frog",
"horse",
"ship",
"truck"
};
for (int i = 0; i < result.size(); i++) {
std::string line = std::to_string(i + 1) + "," + label[result[i]];
output << line << std::endl;
}
}
}