This is an example of how to use the netdem library.
using namespace std;
void TrainRegressorTrimeshPlane() {
string root_dir = "local/examples/netdem/ann_models/trimesh_plane/";
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++) {
dataset(4, i) = sqrt(dataset(4, i)) * 40.0;
dataset(5, i) = dataset(5, i) * 40.0;
}
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{5};
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;
int num_nodes = 20;
model.
AddLayer(MLPackUtils::LayerType::Linear, num_nodes);
model.
AddLayer(MLPackUtils::LayerType::LeakyReLU);
model.
AddLayer(MLPackUtils::LayerType::Linear, num_nodes);
model.
AddLayer(MLPackUtils::LayerType::LeakyReLU);
model.
AddLayer(MLPackUtils::LayerType::Linear, num_nodes);
model.
AddLayer(MLPackUtils::LayerType::LeakyReLU);
model.
AddLayer(MLPackUtils::LayerType::Linear, num_nodes);
model.
AddLayer(MLPackUtils::LayerType::LeakyReLU);
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;
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;
auto pred_y = model.
Predict(train_x);
double train_accuracy = MLPackUtils::GetMAE(train_y, pred_y);
cout << "train mae: " << train_accuracy << endl;
double test_accuracy = MLPackUtils::GetMAE(pred_y, test_y);
cout << "test mae: " << test_accuracy << endl << endl;
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
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
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