1 // OpenNN: Open Neural Networks Library
2 // www.opennn.net
3 //
4 // S I M P L E C L A S S I F I C A T I O N 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 pattern recognition problem.
10
11 // System includes
12
13 #include <iostream>
14 #include <sstream>
15 #include <time.h>
16 #include <stdexcept>
17 #include <omp.h>
18
19 // OpenNN includes
20
21 #include "../../opennn/opennn.h"
22
23 using namespace OpenNN;
24
main(void)25 int main(void)
26 {
27 try
28 {
29 cout << "OpenNN. Simple classification example." << endl;
30
31 // Data set
32
33 DataSet data_set("../data/simple_pattern_recognition.csv", ';', true);
34
35 data_set.split_samples_random();
36
37 const Tensor<string, 1> inputs_names = data_set.get_input_variables_names();
38
39 const Tensor<string, 1> targets_names = data_set.get_target_variables_names();
40
41 Tensor<string, 1> scaling_inputs_methods(inputs_names.dimension(0));
42
43 scaling_inputs_methods.setConstant("MinimumMaximum");
44
45 const Tensor<Descriptives, 1> inputs_descriptives = data_set.scale_input_variables(scaling_inputs_methods);
46
47 // Neural network
48
49 Tensor<Index, 1> neural_network_architecture(3);
50
51 neural_network_architecture.setValues({2, 10, 1});
52
53 NeuralNetwork neural_network(NeuralNetwork::Classification, neural_network_architecture);
54
55 neural_network.set_inputs_names(inputs_names);
56
57 neural_network.set_outputs_names(targets_names);
58
59 ScalingLayer* scaling_layer_pointer = neural_network.get_scaling_layer_pointer();
60
61 scaling_layer_pointer->set_descriptives(inputs_descriptives);
62
63 // Training strategy
64
65 TrainingStrategy training_strategy(&neural_network, &data_set);
66
67 training_strategy.set_loss_method(TrainingStrategy::NORMALIZED_SQUARED_ERROR);
68
69 training_strategy.get_loss_index_pointer()->set_regularization_method(LossIndex::NoRegularization);
70
71 training_strategy.set_optimization_method(TrainingStrategy::QUASI_NEWTON_METHOD);
72
73 const OptimizationAlgorithm::Results training_strategy_results = training_strategy.perform_training();
74
75 // Testing analysis
76
77 data_set.unscale_input_variables_minimum_maximum(inputs_descriptives);
78
79 TestingAnalysis testing_analysis(&neural_network, &data_set);
80
81 Tensor<type, 1> binary_classification_tests = testing_analysis.calculate_binary_classification_tests();
82
83 Tensor<Index, 2> confusion = testing_analysis.calculate_confusion();
84
85 // Save results
86
87 data_set.save("../data/data_set.xml");
88
89 neural_network.save("../data/neural_network.xml");
90 neural_network.save_expression_python("../data/expression.py");
91
92 training_strategy.save("../data/training_strategy.xml");
93 training_strategy_results.save("../data/training_strategy_results.dat");
94
95
96 cout << "Bye" << endl;
97
98 return 0;
99 }
100 catch(exception& e)
101 {
102 cerr << e.what() << endl;
103
104 return 1;
105 }
106 }
107
108
109 // OpenNN: Open Neural Networks Library.
110 // Copyright (C) 2005-2019 Artificial Intelligence Techniques SL
111 //
112 // This library is free software; you can redistribute it and/or
113 // modify it under the terms of the GNU Lesser General Public
114 // License as published by the Free Software Foundation; either
115 // version 2.1 of the License, or any later version.
116 //
117 // This library is distributed in the hope that it will be useful,
118 // but WITHOUT ANY WARRANTY; without even the implied warranty of
119 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
120 // Lesser General Public License for more details.
121
122 // You should have received a copy of the GNU Lesser General Public
123 // License along with this library; if not, write to the Free Software
124 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
125