NetDEM v1.0
Loading...
Searching...
No Matches
32_train_classifier_ellipsoid.cpp

This is an example of how to use the netdem library.

#include "general_net.hpp"
#include "mlpack_utils.hpp"
#include "utils_math.hpp"
using namespace netdem;
using namespace std;
void TrainClassifierEllipsoid() {
string root_dir = "local/examples/netdem/ann_models/ellipsoid_ellipsoid/";
// notes: rows represent features, columns represent data points.
arma::mat dataset;
bool loadedDataset = mlpack::data::Load(
root_dir + "archived/dataset_detection.txt", dataset, true);
if (!loadedDataset) {
cout << "loading data erros ..." << endl;
return;
}
for (int i = 0; i < dataset.n_cols; i++) {
auto tmp_sgn = Math::Sign(dataset(3, i));
dataset(3, i) = tmp_sgn * dataset(3, i);
dataset(4, i) = tmp_sgn * dataset(4, i);
dataset(5, i) = tmp_sgn * dataset(5, i);
dataset(6, i) = tmp_sgn * dataset(6, i);
}
cout << "finished loading data ..." << endl;
// split traning and testing data, prepare inputs and outputs
arma::mat train_data, test_data;
mlpack::data::Split(dataset, train_data, test_data, 0.1);
int num_trains = train_data.n_cols;
int num_tests = test_data.n_cols;
int num_x{7}, num_y{1};
arma::mat train_x = train_data.rows(0, num_x - 1);
arma::mat test_x = test_data.rows(0, num_x - 1);
arma::mat train_y = train_data.row(num_x);
arma::mat test_y = test_data.row(num_x);
cout << "finished preparing training and testing data ..." << endl;
// create the ann model
GeneralNet model;
int num_nodes = 50, num_hidden_layers = 5;
model.AddLayer(MLPackUtils::LayerType::Linear, num_nodes);
model.AddLayer(MLPackUtils::LayerType::LeakyReLU);
for (int i = 0; i < num_hidden_layers - 1; i++) {
model.AddLayer(MLPackUtils::LayerType::Linear, num_nodes);
model.AddLayer(MLPackUtils::LayerType::LeakyReLU);
}
model.AddLayer(MLPackUtils::LayerType::Linear, 2);
model.AddLayer(MLPackUtils::LayerType::LogSoftMax);
cout << "finished creating ann model ..." << endl;
// set training parameters
model.epochs = 100;
model.step_size = 0.001;
model.batch_size = 128;
model.stop_tol = 1.0e-10;
model.decay_rate_moment = 0.99;
model.decay_rate_norm = 0.99;
double test_accuracy_best{0};
for (int i = 0; i < 10; i++) {
model.Train(train_x, train_y);
cout << endl << "finished training ..." << i << endl;
// check the training and testing results
auto pred_y = model.Classify(train_x);
double train_accuracy =
arma::accu(pred_y == train_y) * 1.0 / num_trains * 100.0;
cout << "train mae: " << train_accuracy << " %" << endl;
pred_y = model.Classify(test_x);
double test_accuracy =
arma::accu(pred_y == test_y) * 1.0 / num_tests * 100.0;
cout << "test mae: " << test_accuracy << " %" << endl << endl;
// save the model
model.Save(root_dir + "training/ann_classifier_" + to_string(i) + ".xml",
"detection");
if (test_accuracy > test_accuracy_best) {
test_accuracy_best = test_accuracy;
model.Save(root_dir + "ann_classifier.xml", "detection");
}
}
}
A class representing a general neural network.
Definition general_net.hpp:19
void Train(const arma::mat &data_x, const arma::mat &data_y)
Trains the current neural network model using the given input and output data.
Definition general_net.cpp:48
void Save(std::string const &filename, std::string const &label)
Saves the current neural network model to a file.
Definition general_net.cpp:78
void AddLayer(MLPackUtils::LayerType layer_name,...)
Adds a layer to the current neural network model.
Definition general_net.cpp:12
int batch_size
Machine learning hyper-parameters.
Definition general_net.hpp:24
int epochs
Machine learning hyper-parameters.
Definition general_net.hpp:28
double decay_rate_norm
Machine learning hyper-parameters.
Definition general_net.hpp:26
double decay_rate_moment
Machine learning hyper-parameters.
Definition general_net.hpp:25
double stop_tol
Machine learning hyper-parameters.
Definition general_net.hpp:29
double step_size
Machine learning hyper-parameters.
Definition general_net.hpp:23
arma::mat Classify(const arma::mat &data_x)
Classifies input data based on the current neural network model.
Definition general_net.cpp:65
Definition bond_entry.hpp:7