Estimate airline passengers using OpenNN
This example uses past months’ data to predict the maximum number of passengers an airline will have.
Contents:
1. Application type
The variable to be predicted is discrete (the number of passengers per month), so this is a forecasting project.
The primary goal is to model passenger behavior in the coming months, conditioned on past behavior.
2. Data set
The first step is to prepare the data set, which is the source of information for the forecasting problem.
The data source is the file airline_passengers.csv. It contains the data for this example in comma-separated values (CSV) format and can be loaded as:
// Initialize time series dataset
TimeSeriesDataSet data_set(
"path_to_source/airine_passengers.csv",
";",
true,
false
);
There are two columns and 144 rows. The variables in this problem are:
- months: Unused.
- passengers: Number of passengers in each month, used as input.
We need to preprocess the data for the forecasting model. This involves setting past time steps values and defining the number of future steps before transforming the data using:
// Configure past and future time steps const Index past_time_steps = 1; const Index future_time_steps = 1; data_set.set_past_time_steps(past_time_steps); data_set.set_future_time_steps(future_time_steps);
To get the input and target variable numbers, we use the following command:
// Retrieve number of input and target variables
const Index input_variables_number =
data_set.get_variables_number("Input");
const Index target_variables_number =
data_set.get_variables_number("Target");
For more information on the data set methods, see the DataSet class.
3. Neural network
The second step is to choose the correct neural network architecture. For forecasting problems, it is usually composed of:
- A scaling layer.
- A recurrent layer.
- A perceptron layer.
- An unscaling layer.
- A bounding layer.
The NeuralNetwork class is responsible for building the neural network and adequately organizing the layers of neurons using the following constructor. If you need more complex architectures, you should see the NeuralNetwork class.
// Initialize forecasting neural network
const Index hidden_neurons_number = 3;
ForecastingNetwork neural_network(
{input_variables_number},
{hidden_neurons_number},
{target_variables_number}
);
Therefore, we have already created a good-looking model. Thus, we proceed to the learning process with TrainingStrategy.
4. Training strategy
The third step is to set the training strategy, which is composed of:
- Loss index.
- Optimization algorithm.
Firstly, we construct the training strategy object
// Initialize training strategy TrainingStrategy training_strategy(&neural_network, &data_set);
Next, we set the error term
// Set loss function for forecasting
training_strategy.set_loss_index("MeanSquaredError");
and finally, the optimization algorithm
// Set optimization algorithm for forecasting
training_strategy.set_optimization_algorithm("AdaptiveMomentEstimation");
OpenNN builds the training strategy object by default using the quasi-Newton method as the optimization algorithm and normalized squared error as the loss method. We can now start the training process by using the command:
// Train forecasting model training_strategy.train();
If we need to go further, OpenNN allows control of the optimization, for example:
// Configure Adam optimization parameters
AdaptiveMomentEstimation* adam =
dynamic_cast<AdaptiveMomentEstimation*>(
training_strategy.get_optimization_algorithm()
);
adam->set_loss_goal(type(1.0e-3));
adam->set_maximum_epochs_number(10000);
adam->set_display_period(1000);
training_strategy.train();
For more information on the training strategy methods, see the TrainingStrategy class.
5. Testing analysis
The fourth step is to evaluate our model. For that purpose, we need to use the TestingAnalysis class, whose goal is to validate the model’s generalization performance. Here, we compare the neural network outputs to the corresponding targets in the testing instances of the data set.
As in previous cases, we start by building the testing analysis object
// Initialize testing analysis TestingAnalysis testing_analysis(&neural_network, &data_set);
and performing the testing. In our case, we use linear regression:
// Print goodness-of-fit analysis results testing_analysis.print_goodness_of_fit_analysis();
For more information about the testing analysis methods, see the TestingAnalysis class.
6. Model deployment
Once our model is complete, the neural network is prepared to predict outputs for the upcoming months. This process is called model deployment.
To generate predictions with new data, you can use:
// Compute network outputs neural_network.calculate_outputs();
For instance, the inputs can be calculated by:
// Retrieve input data tensor Tensor<type, 2> inputs = data_set.get_data();
and for outputs, we can write
// Compute network outputs with template dimensions neural_network.calculate_outputs<3, 2>(inputs);
or save the model.
// Export trained model expressions
ModelExpression model_expression(&neural_network);
model_expression.save_c(
"../data/expression.txt",
data_set.get_raw_variables()
);
model_expression.save_python(
"../data/expression.txt",
data_set.get_raw_variables()
);
You can implement the model in Python, PHP, and so on.
7. Full code
Merging all steps, we obtain the following code:
// --------------------
// Data Set
// --------------------
TimeSeriesDataSet data_set(
"path_to_source/airine_passengers.csv",
";",
true,
false
);
const Index past_time_steps = 1;
const Index future_time_steps = 1;
data_set.set_past_time_steps(past_time_steps);
data_set.set_future_time_steps(future_time_steps);
const Index input_variables_number =
data_set.get_variables_number("Input");
const Index target_variables_number =
data_set.get_variables_number("Target");
// --------------------
// Neural Network
// --------------------
const Index hidden_neurons_number = 3;
ForecastingNetwork neural_network(
{input_variables_number},
{hidden_neurons_number},
{target_variables_number}
);
// --------------------
// Training Strategy
// --------------------
TrainingStrategy training_strategy(&neural_network, &data_set);
training_strategy.set_loss_index("MeanSquaredError");
training_strategy.set_optimization_algorithm(
"AdaptiveMomentEstimation"
);
training_strategy.train();
// --------------------
// Testing Analysis
// --------------------
TestingAnalysis testing_analysis(&neural_network, &data_set);
testing_analysis.print_goodness_of_fit_analysis();
// --------------------
// Model Deployment
// --------------------
Tensor<type, 2> inputs = data_set.get_data();
neural_network.calculate_outputs<3, 2>(inputs);
// --------------------
// Save results
// --------------------
ModelExpression model_expression(&neural_network);
model_expression.save_c(
"../data/expression.txt",
data_set.get_raw_variables()
);
model_expression.save_python(
"../data/expression.txt",
data_set.get_raw_variables()
);
This code can be exported to your C++ project.
References:
- Machine Learning Mastery Airline Passengers DataSet.
- Fisher, R.A. «The use of multiple measurements in taxonomic problems» Annual Eugenics, 7, Part II, 179-188 (1936); also in «Contributions to Mathematical Statistics» (John Wiley, NY, 1950).