This is an example of how to use the netdem library.
using namespace std;
void TrainClassifierEllipsoidPlane() {
string root_dir = "local/examples/netdem/ann_models/ellipsoid_plane/";
arma::mat dataset;
bool loadedDataset = mlpack::data::Load(
root_dir + "archived/dataset_detection.txt", dataset, true);
if (!loadedDataset) {
cout << "loading data erros ..." << endl;
return;
}
cout << "finished loading data ..." << endl;
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{4}, 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;
int num_nodes = 25, 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;
double test_accuracy_best{0};
for (int i = 0; i < 10; i++) {
model.
Train(train_x, train_y);
cout << endl << "finished training ..." << i << endl;
double train_accuracy =
arma::accu(pred_y == train_y) * 1.0 / num_trains * 100.0;
cout << "train accuracy: " << train_accuracy << " %" << endl;
double test_accuracy =
arma::accu(pred_y == test_y) * 1.0 / num_tests * 100.0;
cout << "test accuracy: " << test_accuracy << " %" << endl << endl;
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 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