1 //   OpenNN: Open Neural Networks Library
2 //   www.opennn.net
3 //
4 //   L O G I C A L   O P E R A T O R S   A P P L I C A T I O N
5 //
6 //   Artificial Intelligence Techniques SL (Artelnics)
7 //   artelnics@artelnics.com
8 
9 // This is a classical learning problem.
10 
11 // System includes
12 
13 #include <iostream>
14 #include <math.h>
15 #include <time.h>
16 #include <omp.h>
17 
18 // OpenNN includes
19 
20 #include "../../opennn/opennn.h"
21 
22 using namespace OpenNN;
23 
main(void)24 int main(void)
25 {
26     try
27     {
28         cout << "OpenNN. Logical Operations Example." << endl;
29 
30         srand(static_cast<unsigned>(time(nullptr)));
31 
32         // Data set
33 
34         DataSet data_set("../data/logical_operations.csv", ';', true);
35 
36         Tensor<string, 1> uses(8);
37         uses.setValues({"Input","Input","Target","Target","Target","Target","Target","Target"});
38 
39         data_set.set_columns_uses(uses);
40         data_set.set_training();
41 
42         const Tensor<string, 1> inputs_names = data_set.get_input_variables_names();
43         const Tensor<string, 1> targets_names = data_set.get_target_variables_names();
44 
45         const Index input_variables_number = data_set.get_input_variables_number();
46         const Index target_variables_number = data_set.get_target_variables_number();
47 
48         // Neural network
49 
50         const Index hidden_neurons_number = 6;
51 
52         Tensor<Index, 1> architecture(3);
53         architecture.setValues({input_variables_number, hidden_neurons_number, target_variables_number});
54 
55         NeuralNetwork neural_network(NeuralNetwork::Approximation, architecture);
56 
57         neural_network.set_inputs_names(inputs_names);
58         neural_network.set_outputs_names(targets_names);
59 
60         dynamic_cast<PerceptronLayer*>(neural_network.get_trainable_layers_pointers()(0))->set_synaptic_weights_glorot();
61 
62         // Training strategy
63 
64         TrainingStrategy training_strategy(&neural_network, &data_set);
65 
66         training_strategy.set_loss_method(TrainingStrategy::NORMALIZED_SQUARED_ERROR);
67         training_strategy.set_optimization_method(TrainingStrategy::QUASI_NEWTON_METHOD);
68 
69         training_strategy.get_normalized_squared_error_pointer()->set_normalization_coefficient();
70         training_strategy.perform_training();
71 
72         // Print results to screen
73 
74         Tensor<type, 2> inputs(1,2);
75         Tensor<type, 2> outputs(1,6);
76 
77         cout << "X Y AND OR NAND NOR XOR XNOR" << endl;
78 
79         inputs(0,0) = 1.0;
80         inputs(0,1) = 1.0;
81 
82         outputs = neural_network.calculate_outputs(inputs);
83 
84         cout <<"X = 1 Y = 1" << endl << inputs << " " << outputs << endl;
85 
86         inputs(0,0) = 1.0;
87         inputs(0,1) = 0.0;
88 
89         outputs = neural_network.calculate_outputs(inputs);
90 
91         cout << "X = 1 Y = 0" << endl << inputs << " " << outputs << endl;
92 
93         inputs(0,0) = 0.0;
94         inputs(0,1) = 1.0;
95 
96         outputs = neural_network.calculate_outputs(inputs);
97 
98         cout << "X = 0 Y = 1" << endl << inputs << " " << outputs << endl;
99 
100         inputs(0,0) = 0.0;
101         inputs(0,1) = 0.0;
102 
103         outputs = neural_network.calculate_outputs(inputs);
104 
105         cout << "X = 0 Y = 0" << endl << inputs << " " << outputs << endl;
106 
107         // Save results
108 
109         data_set.save("../data/data_set.xml");
110 
111         neural_network.save("../data/neural_network.xml");
112 
113         training_strategy.save("../data/training_strategy.xml");
114 
115         return 0;
116 
117     }
118     catch(exception& e)
119     {
120         cerr << e.what() << endl;
121 
122         return 1;
123     }
124 }
125 
126 
127 // OpenNN: Open Neural Networks Library.
128 // Copyright (C) 2005-2019 Artificial Intelligence Techniques SL
129 //
130 // This library is free software; you can redistribute it and/or
131 // modify it under the terms of the GNU Lesser General Public
132 // License as published by the Free Software Foundation; either
133 // version 2.1 of the License, or any later version.
134 //
135 // This library is distributed in the hope that it will be useful,
136 // but WITHOUT ANY WARRANTY; without even the implied warranty of
137 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
138 // Lesser General Public License for more details.
139 
140 // You should have received a copy of the GNU Lesser General Public
141 // License along with this library; if not, write to the Free Software
142 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
143