-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_classification.cpp
More file actions
82 lines (63 loc) · 1.65 KB
/
binary_classification.cpp
File metadata and controls
82 lines (63 loc) · 1.65 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
#include <bits/stdc++.h>
#include <fstream>
#include "./neuralnetwork/neuralnetwork.h"
//hyperparameters
#define LEARNING_RATE 0.01
#define LAMBDA 0.1
#define BATCH_ITERATION 50
using namespace std;
int main()
{
ifstream ifs("data.ssv");
int M;
ifs >> M;
Matrix X = Matrix(2, M);
Matrix Y = Matrix(1, M);
for (int i = 0; i < M; i++)
{
ifs >> X[0][i] >> X[1][i];
ifs >> Y[0][i];
}
NeuralNetwork nn = NeuralNetwork();
nn.add_layer(new ReluLayer(2, 32, LEARNING_RATE, LAMBDA));
nn.add_layer(new ReluLayer(32, 16, LEARNING_RATE, LAMBDA));
nn.add_layer(new ReluLayer(16, 16, LEARNING_RATE, LAMBDA));
nn.add_layer(new ReluLayer(16, 8, LEARNING_RATE, LAMBDA));
nn.add_layer(new SigmoidLayer(8, 1, LEARNING_RATE, LAMBDA));
nn.add_loss_function(new BinaryCrossEntropyLoss());
for (int i = 0; i < 5; i++)
{
float cost = nn.train_batch(X, Y, BATCH_ITERATION);
cout << "Cost after " << (i + 1) * BATCH_ITERATION << " iterations: " << cost << endl;
}
float accuracy = 0;
Matrix Y_pred = nn.predict(X);
for (int i = 0; i < M; i++)
{
float y_pred = Y_pred[0][i] > 0.5 ? 1 : 0;
accuracy += (y_pred == Y[0][i]) ? 1 : 0;
}
accuracy /= M;
cout << "Accuracy: " << accuracy << endl;
Matrix X_plot = Matrix(2, 40000);
int cnt = 0;
for (int i = -100; i < 100; i++)
{
for (int j = -100; j < 100; j++)
{
float x = i * 1.0 / 100;
float y = j * 1.0 / 100;
X_plot[0][cnt] = x;
X_plot[1][cnt] = y;
cnt++;
}
}
Matrix Y_plot = nn.predict(X_plot);
ofstream ofs("result.ssv");
for (int i = 0; i < 40000; i++)
{
ofs << X_plot[0][i] << ' ' << X_plot[1][i] << ' ' << Y_plot[0][i] << '\n';
}
cout << "Results saved." << endl;
return 0;
}