-
Notifications
You must be signed in to change notification settings - Fork 0
/
classification.cpp
83 lines (70 loc) · 1.52 KB
/
classification.cpp
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
#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()
{
int M = 1000;
Matrix X = Matrix(2, M, 'u');
Matrix Y = Matrix(4, M);
for (int i = 0; i < X.m; i++)
{
if (X[0][i] < 0.5 && X[1][i] < 0.5)
{
Y[0][i] = 1;
}
else if (X[0][i] >= 0.5 && X[1][i] >= 0.5)
{
Y[1][i] = 1;
}
else if (X[0][i] < 0.5 && X[1][i] >= 0.5)
{
Y[2][i] = 1;
}
else
{
Y[3][i] = 1;
}
}
NeuralNetwork nn = NeuralNetwork();
nn.add_layer(new ReluLayer(2, 16, LEARNING_RATE, LAMBDA));
nn.add_layer(new ReluLayer(16, 8, LEARNING_RATE, LAMBDA));
nn.add_layer(new ReluLayer(8, 8, LEARNING_RATE, LAMBDA));
nn.add_layer(new SoftmaxLayer(8, 4, LEARNING_RATE, LAMBDA));
nn.add_loss_function(new SoftmaxCrossEntropyLoss());
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++)
{
int max_idx1 = -1;
int max_idx2 = -1;
float nax1 = -1;
float nax2 = -1;
for (int j = 0; j < 4; j++)
{
if (Y_pred[j][i] > nax1)
{
nax1 = Y_pred[j][i];
max_idx1 = j;
}
if (Y[j][i] > nax2)
{
nax2 = Y_pred[j][i];
max_idx2 = j;
}
}
accuracy += (max_idx1 == max_idx2) ? 1 : 0;
}
accuracy /= M;
cout << "Accuracy: " << accuracy << endl;
return 0;
}