-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
69 lines (55 loc) · 1.64 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
#include <xorai/network.h>
/* Uncomment the following line to train the model. */
//#define TRAIN_MODEL_EXAMPLE
/* Uncomment the following line to use the model. */
//#define USE_MODEL_EXAMPLE
void train_model(Dataset<f64>& inputs, Dataset<f64>& targets)
{
/* Create a new neural network with the following architecture:
* - 2 input layers
* - 3 hidden layers
* - 1 output layer
* The network is configured with a learning rate of 0.5
* and uses 64-bit floating-point precision. */
Network<f64> network((U64Array){2, 3, 1}, 0.5);
/* Train the model with the given inputs and targets. */
network.train(inputs, targets, 1000);
/* Save the model to a file named `model.xorai` using the
highest precision available for 64-bit floating-point
representation for each weight, bias, and data object. */
network.save("model.xorai", UseMaxPrecision(64));
}
void use_model()
{
/* Load the model from the file `model.xorai`. */
Network<f64> network("model.xorai");
/* Test the model with the given inputs. */
Matrix<f64>* result = network.test(1.0, 1.0);
/* Display the result. */
Matrix<f64>::display(result);
/* Free the memory allocated for the result. */
delete(result);
}
int main()
{
#ifdef TRAIN_MODEL_EXAMPLE
/* Create the Inputs and Targets Datasets. */
Dataset<f64> inputs = {
{0.0, 0.0},
{0.0, 1.0},
{1.0, 0.0},
{1.0, 1.0}
};
Dataset<f64> targets = {
{1.0},
{0.0},
{0.0},
{1.0}
};
train_model(inputs, targets);
#endif
#ifdef USE_MODEL_EXAMPLE
use_model();
#endif
return 0;
}