Feedforward Network for Regression

This page illustrates the usage of feedforward networks for regression. For more details about feedforward networks, see Feedforward Network for Classification.

Example

Imagine we have files with training and test data. We create CDenseFeatures (here 64 bit floats aka RealFeatures) and CRegressionLabels as

features_train = RealFeatures(f_feats_train)
features_test = RealFeatures(f_feats_test)
labels_train = RegressionLabels(f_labels_train)
labels_test = RegressionLabels(f_labels_test)
features_train = RealFeatures(f_feats_train);
features_test = RealFeatures(f_feats_test);
labels_train = RegressionLabels(f_labels_train);
labels_test = RegressionLabels(f_labels_test);
RealFeatures features_train = new RealFeatures(f_feats_train);
RealFeatures features_test = new RealFeatures(f_feats_test);
RegressionLabels labels_train = new RegressionLabels(f_labels_train);
RegressionLabels labels_test = new RegressionLabels(f_labels_test);
features_train = Modshogun::RealFeatures.new f_feats_train
features_test = Modshogun::RealFeatures.new f_feats_test
labels_train = Modshogun::RegressionLabels.new f_labels_train
labels_test = Modshogun::RegressionLabels.new f_labels_test
features_train <- RealFeatures(f_feats_train)
features_test <- RealFeatures(f_feats_test)
labels_train <- RegressionLabels(f_labels_train)
labels_test <- RegressionLabels(f_labels_test)
features_train = modshogun.RealFeatures(f_feats_train)
features_test = modshogun.RealFeatures(f_feats_test)
labels_train = modshogun.RegressionLabels(f_labels_train)
labels_test = modshogun.RegressionLabels(f_labels_test)
RealFeatures features_train = new RealFeatures(f_feats_train);
RealFeatures features_test = new RealFeatures(f_feats_test);
RegressionLabels labels_train = new RegressionLabels(f_labels_train);
RegressionLabels labels_test = new RegressionLabels(f_labels_test);
auto features_train = some<CDenseFeatures<float64_t>>(f_feats_train);
auto features_test = some<CDenseFeatures<float64_t>>(f_feats_test);
auto labels_train = some<CRegressionLabels>(f_labels_train);
auto labels_test = some<CRegressionLabels>(f_labels_test);

We create instances of CNeuralLayers and add an input layer, hidden layer and output layer which are building blocks of CNeuralNetwork

dimensions = features_train.get_num_features()
layers = NeuralLayers()
layers = layers.input(dimensions)
layers = layers.rectified_linear(20)
layers = layers.linear(1)
all_layers = layers.done()
dimensions = features_train.get_num_features();
layers = NeuralLayers();
layers = layers.input(dimensions);
layers = layers.rectified_linear(20);
layers = layers.linear(1);
all_layers = layers.done();
int dimensions = features_train.get_num_features();
NeuralLayers layers = new NeuralLayers();
layers = layers.input(dimensions);
layers = layers.rectified_linear(20);
layers = layers.linear(1);
DynamicObjectArray all_layers = layers.done();
dimensions = features_train.get_num_features 
layers = Modshogun::NeuralLayers.new 
layers = layers.input dimensions
layers = layers.rectified_linear 20
layers = layers.linear 1
all_layers = layers.done 
dimensions <- features_train$get_num_features()
layers <- NeuralLayers()
layers <- layers$input(dimensions)
layers <- layers$rectified_linear(20)
layers <- layers$linear(1)
all_layers <- layers$done()
dimensions = features_train:get_num_features()
layers = modshogun.NeuralLayers()
layers = layers:input(dimensions)
layers = layers:rectified_linear(20)
layers = layers:linear(1)
all_layers = layers:done()
int dimensions = features_train.get_num_features();
NeuralLayers layers = new NeuralLayers();
layers = layers.input(dimensions);
layers = layers.rectified_linear(20);
layers = layers.linear(1);
DynamicObjectArray all_layers = layers.done();
auto dimensions = features_train->get_num_features();
auto layers = some<CNeuralLayers>();
layers = layers->input(dimensions);
layers = layers->rectified_linear(20);
layers = layers->linear(1);
auto all_layers = layers->done();

We create a CNeuralNetwork instance by using the above layers and randomly initialize the network parameters by sampling from a gaussian distribution.

network = NeuralNetwork(all_layers)
network.quick_connect()
network.initialize_neural_network()
network = NeuralNetwork(all_layers);
network.quick_connect();
network.initialize_neural_network();
NeuralNetwork network = new NeuralNetwork(all_layers);
network.quick_connect();
network.initialize_neural_network();
network = Modshogun::NeuralNetwork.new all_layers
network.quick_connect 
network.initialize_neural_network 
network <- NeuralNetwork(all_layers)
network$quick_connect()
network$initialize_neural_network()
network = modshogun.NeuralNetwork(all_layers)
network:quick_connect()
network:initialize_neural_network()
NeuralNetwork network = new NeuralNetwork(all_layers);
network.quick_connect();
network.initialize_neural_network();
auto network = some<CNeuralNetwork>(all_layers);
network->quick_connect();
network->initialize_neural_network();

Before training, we need to set appropriate parameters like regularization coefficient, number of epochs, learning rate, etc. as shown below. More parameters can be found in the documentation of CNeuralNetwork.

network.set_l2_coefficient(0.1)
network.set_max_num_epochs(40)
network.set_epsilon(0.0)
network.set_gd_learning_rate(0.1)
network.set_gd_momentum(0.9)
network.set_l2_coefficient(0.1);
network.set_max_num_epochs(40);
network.set_epsilon(0.0);
network.set_gd_learning_rate(0.1);
network.set_gd_momentum(0.9);
network.set_l2_coefficient(0.1);
network.set_max_num_epochs(40);
network.set_epsilon(0.0);
network.set_gd_learning_rate(0.1);
network.set_gd_momentum(0.9);
network.set_l2_coefficient 0.1
network.set_max_num_epochs 40
network.set_epsilon 0.0
network.set_gd_learning_rate 0.1
network.set_gd_momentum 0.9
network$set_l2_coefficient(0.1)
network$set_max_num_epochs(40)
network$set_epsilon(0.0)
network$set_gd_learning_rate(0.1)
network$set_gd_momentum(0.9)
network:set_l2_coefficient(0.1)
network:set_max_num_epochs(40)
network:set_epsilon(0.0)
network:set_gd_learning_rate(0.1)
network:set_gd_momentum(0.9)
network.set_l2_coefficient(0.1);
network.set_max_num_epochs(40);
network.set_epsilon(0.0);
network.set_gd_learning_rate(0.1);
network.set_gd_momentum(0.9);
network->set_l2_coefficient(0.1);
network->set_max_num_epochs(40);
network->set_epsilon(0.0);
network->set_gd_learning_rate(0.1);
network->set_gd_momentum(0.9);

We train the model and apply it to test data.

network.set_labels(labels_train)
network.train(features_train)
labels_predict = network.apply_regression(features_test)
network.set_labels(labels_train);
network.train(features_train);
labels_predict = network.apply_regression(features_test);
network.set_labels(labels_train);
network.train(features_train);
RegressionLabels labels_predict = network.apply_regression(features_test);
network.set_labels labels_train
network.train features_train
labels_predict = network.apply_regression features_test
network$set_labels(labels_train)
network$train(features_train)
labels_predict <- network$apply_regression(features_test)
network:set_labels(labels_train)
network:train(features_train)
labels_predict = network:apply_regression(features_test)
network.set_labels(labels_train);
network.train(features_train);
RegressionLabels labels_predict = network.apply_regression(features_test);
network->set_labels(labels_train);
network->train(features_train);
auto labels_predict = network->apply_regression(features_test);

We can extract the parameters of the trained network.

parameters = network.get_parameters()
parameters = network.get_parameters();
DoubleMatrix parameters = network.get_parameters();
parameters = network.get_parameters 
parameters <- network$get_parameters()
parameters = network:get_parameters()
double[] parameters = network.get_parameters();
auto parameters = network->get_parameters();

Finally, we compute CMeanSquaredError.

err = MeanSquaredError()
mse = err.evaluate(labels_predict, labels_test)
err = MeanSquaredError();
mse = err.evaluate(labels_predict, labels_test);
MeanSquaredError err = new MeanSquaredError();
double mse = err.evaluate(labels_predict, labels_test);
err = Modshogun::MeanSquaredError.new 
mse = err.evaluate labels_predict, labels_test
err <- MeanSquaredError()
mse <- err$evaluate(labels_predict, labels_test)
err = modshogun.MeanSquaredError()
mse = err:evaluate(labels_predict, labels_test)
MeanSquaredError err = new MeanSquaredError();
double mse = err.evaluate(labels_predict, labels_test);
auto err = some<CMeanSquaredError>();
auto mse = err->evaluate(labels_predict, labels_test);