MCPcopy Create free account
hub / github.com/davisking/dlib / main

Function main

examples/mlp_ex.cpp:26–85  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

24
25
26int main()
27{
28 // The mlp takes column vectors as input and gives column vectors as output. The dlib::matrix
29 // object is used to represent the column vectors. So the first thing we do here is declare
30 // a convenient typedef for the matrix object we will be using.
31
32 // This typedef declares a matrix with 2 rows and 1 column. It will be the
33 // object that contains each of our 2 dimensional samples. (Note that if you wanted
34 // more than 2 features in this vector you can simply change the 2 to something else)
35 typedef matrix<double, 2, 1> sample_type;
36
37
38 // make an instance of a sample matrix so we can use it below
39 sample_type sample;
40
41 // Create a multi-layer perceptron network. This network has 2 nodes on the input layer
42 // (which means it takes column vectors of length 2 as input) and 5 nodes in the first
43 // hidden layer. Note that the other 4 variables in the mlp's constructor are left at
44 // their default values.
45 mlp::kernel_1a_c net(2,5);
46
47 // Now let's put some data into our sample and train on it. We do this
48 // by looping over 41*41 points and labeling them according to their
49 // distance from the origin.
50 for (int i = 0; i < 1000; ++i)
51 {
52 for (int r = -20; r <= 20; ++r)
53 {
54 for (int c = -20; c <= 20; ++c)
55 {
56 sample(0) = r;
57 sample(1) = c;
58
59 // if this point is less than 10 from the origin
60 if (sqrt((double)r*r + c*c) <= 10)
61 net.train(sample,1);
62 else
63 net.train(sample,0);
64 }
65 }
66 }
67
68 // Now we have trained our mlp. Let's see how well it did.
69 // Note that if you run this program multiple times you will get different results. This
70 // is because the mlp network is randomly initialized.
71
72 // each of these statements prints out the output of the network given a particular sample.
73
74 sample(0) = 3.123;
75 sample(1) = 4;
76 cout << "This sample should be close to 1 and it is classified as a " << net(sample) << endl;
77
78 sample(0) = 13.123;
79 sample(1) = 9.3545;
80 cout << "This sample should be close to 0 and it is classified as a " << net(sample) << endl;
81
82 sample(0) = 13.123;
83 sample(1) = 0;

Callers

nothing calls this directly

Calls 2

sqrtFunction · 0.50
trainMethod · 0.45

Tested by

no test coverage detected