-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmnist_loader.h
More file actions
39 lines (32 loc) · 1.02 KB
/
Copy pathmnist_loader.h
File metadata and controls
39 lines (32 loc) · 1.02 KB
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
#ifndef MNIST_LOADER_H
#define MNIST_LOADER_H
#include <vector>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
pair<vector<vector<double>>, vector<vector<double>>> loadMNIST(const string& filename, int numSamples = -1) {
vector<vector<double>> inputs, targets;
ifstream file(filename);
string line;
getline(file, line);
int count = 0;
while (getline(file, line) && (numSamples == -1 || count < numSamples)) {
stringstream lineStream(line);
string token;
getline(lineStream, token, ',');
int label = stoi(token);
vector<double> target(10, 0.0);
target[label] = 1.0;
targets.push_back(target);
vector<double> pixels;
while (getline(lineStream, token, ',')) {
pixels.push_back(stoi(token) / 255.0);
}
inputs.push_back(pixels);
count++;
}
cout << "Loaded " << count << " samples from " << filename << endl;
return {inputs, targets};
}
#endif