-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
92 lines (83 loc) · 2.66 KB
/
main.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
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <vector>
#include "NN.cpp"
int main()
{
// Initialize the neural network
NN neural_network;
// Add layers dynamically
neural_network.add(new Linear(2, 3));
neural_network.add(new Relu());
neural_network.add(new Linear(3, 3));
neural_network.add(new Relu());
neural_network.add(new Linear(3, 1));
neural_network.add(new Sigmoid());
// Example input data
std::vector<std::vector<double>> X = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
std::vector<std::vector<double>> y = {{0}, {1}, {1}, {0}};
// Train the network
neural_network.fit(X, y, 10000, 0.01);
// Test the network using XOR example
std::vector<double> input = {0, 0};
std::vector<double> output_prob = neural_network.predict(input);
std::vector<double> output = {0};
if (output_prob[0] > 0.5)
{
output = {1};
}
else
{
output = {0};
}
std::cout << "Input: " << input[0] << ", " << input[1] << std::endl;
std::cout << "Output Probability: " << output_prob[0] << std::endl;
std::cout << "Output: " << output[0] << std::endl;
std::cout << "Expected Output: " << 0 << std::endl;
std::cout << "----------------------" << std::endl;
input = {0, 1};
output_prob = neural_network.predict(input);
if (output_prob[0] > 0.5)
{
output = {1};
}
else
{
output = {0};
}
std::cout << "Input: " << input[0] << ", " << input[1] << std::endl;
std::cout << "Output Probability: " << output_prob[0] << std::endl;
std::cout << "Output: " << output[0] << std::endl;
std::cout << "Expected Output: " << 1 << std::endl;
std::cout << "----------------------" << std::endl;
input = {1, 0};
output_prob = neural_network.predict(input);
if (output_prob[0] > 0.5)
{
output = {1};
}
else
{
output = {0};
}
std::cout << "Input: " << input[0] << ", " << input[1] << std::endl;
std::cout << "Output Probability: " << output_prob[0] << std::endl;
std::cout << "Output: " << output[0] << std::endl;
std::cout << "Expected Output: " << 1 << std::endl;
std::cout << "----------------------" << std::endl;
input = {1, 1};
output_prob = neural_network.predict(input);
if (output_prob[0] > 0.5)
{
output = {1};
}
else
{
output = {0};
}
std::cout << "Input: " << input[0] << ", " << input[1] << std::endl;
std::cout << "Output Probability: " << output_prob[0] << std::endl;
std::cout << "Output: " << output[0] << std::endl;
std::cout << "Expected Output: " << 0 << std::endl;
std::cout << "----------------------" << std::endl;
return 0;
}