-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp-neural-network.cpp
More file actions
84 lines (68 loc) · 3.1 KB
/
cpp-neural-network.cpp
File metadata and controls
84 lines (68 loc) · 3.1 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
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
// cpp-neural-network.cpp : This file contains the 'main' function.
#include <iostream>
#include <fstream>
#include "globalFunctions.h"
#include "fileClasses.h"
#include "networkClasses.h"
using namespace std;
/*GLOBAL FILE LOCATIONS*/
// Images
const string trainImagesPath = "C:/Users/benhu/PycharmProjects/Data Sets/mnist-dataset/versions/1/train-images-idx3-ubyte";
const string trainImagesFile = "/train-images-idx3-ubyte";
const string testImagesPath = "C:/Users/benhu/PycharmProjects/Data Sets/mnist-dataset/versions/1/t10k-images-idx3-ubyte";
const string testImagesFile = "/t10k-images-idx3-ubyte";
// Labels
const string trainLabelsPath = "C:/Users/benhu/PycharmProjects/Data Sets/mnist-dataset/versions/1/train-labels-idx1-ubyte";
const string trainLabelsFile = "/train-labels-idx1-ubyte";
const string testLabelsPath = "C:/Users/benhu/PycharmProjects/Data Sets/mnist-dataset/versions/1/t10k-labels-idx1-ubyte";
const string testLabelsFile = "/t10k-labels-idx1-ubyte";
int main() {
// Get training data
File trainingImageData = File(trainImagesPath, trainImagesFile);
File trainingLabelData = File(trainLabelsPath, trainLabelsFile);
int start_index = 0; // image index to start on
int end_index = 60000; // image index to finish on
int batch_size = 16; // batch size for weight / bias adjustment
int epoch = 3; // epoch number, how many times to review dataset
float alpha = 0.1; // learning rate
vector<Image> training_images = trainingImageData.getImageVector(start_index, end_index);
vector<char> training_labels = trainingLabelData.getLabelVector(start_index, end_index);
int final_layer_count = 10; // labels are 0, 1, 2, ... , 9
// zip together the training images and their corresponding labels
for (int i = start_index; i < end_index; i++) {
training_images[i].setLabel(training_labels[i]);
}
// create the neural network
NeuralNetwork nn = NeuralNetwork(
{
trainingImageData.image_size, // input (layer zero)
80, // layer one
20, // layer two
final_layer_count // final layer (layer three)
},
RElU_const, // activation to use
alpha // learning rate
);
// train the neural network
nn.trainVector(
training_images,
batch_size,
epoch
);
// Get test data
File testImageData = File(testImagesPath, testImagesFile);
File testLabelData = File(testLabelsPath, testLabelsFile);
int start_index_test = 0;
int end_index_test = 10000;
vector<Image> test_images = testImageData.getImageVector(start_index_test, end_index_test);
vector<char> test_labels = testLabelData.getLabelVector(start_index_test, end_index_test);
// zip together the test images and their corresponding labels
for (int i = start_index_test; i < end_index_test; i++) {
test_images[i].setLabel(test_labels[i]);
}
// Test the new parameters on the training set
nn.testVector(test_images);
// Save network as a JSON object
nn.toJson();
return 0;
}