NetDEM v1.0
Loading...
Searching...
No Matches
34_train_regressor_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 TrainRegressorEllipsoid() {
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_resolution.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);
dataset(7, i) = dataset(7, i) * 40.0;
}
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{7};
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.rows(num_x, num_x + num_y - 1);
arma::mat test_y = test_data.rows(num_x, num_x + num_y - 1);
cout << "finished preparing training and testing data ..." << endl;
// create the ann 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, train_y.n_rows);
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{1.0e8};
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.Predict(train_x);
double train_accuracy = MLPackUtils::GetMAE(train_y, pred_y);
cout << "train mae: " << train_accuracy << endl;
pred_y = model.Predict(test_x);
double test_accuracy = MLPackUtils::GetMAE(pred_y, test_y);
cout << "test mae: " << test_accuracy << endl << endl;
// save the model
model.Save(root_dir + "training/ann_regressor" + to_string(i) + ".xml",
"resolution");
if (test_accuracy < test_accuracy_best) {
test_accuracy_best = test_accuracy;
model.Save(root_dir + "ann_regressor.xml", "resolution");
}
}
}
A class that represents a feedforward neural network for regression.
Definition regression_net.hpp:21
int epochs
The number of epochs used for training.
Definition regression_net.hpp:42
double stop_tol
The stopping tolerance used for determining when to stop training.
Definition regression_net.hpp:45
void Save(std::string const &filename, std::string const &label)
Saves the neural network model to disk.
Definition regression_net.cpp:99
int batch_size
The batch size used for mini-batch gradient descent during training.
Definition regression_net.hpp:30
double step_size
The step size used for optimization during training.
Definition regression_net.hpp:27
double decay_rate_norm
The decay rate used for weight normalization during training.
Definition regression_net.hpp:36
void AddLayer(MLPackUtils::LayerType layer_name,...)
Adds a layer to the neural network model.
Definition regression_net.cpp:15
void Train(const arma::mat &data_x, const arma::mat &data_y)
Trains the neural network model with data.
Definition regression_net.cpp:51
double decay_rate_moment
The decay rate used for momentum during training.
Definition regression_net.hpp:33
arma::mat Predict(const arma::mat &data_x)
Predicts with the neural network model using input data.
Definition regression_net.cpp:62
Definition bond_entry.hpp:7