Neural network model for regression – MATLAB

Specify the structure of the neural network regression model, including the size of the fully connected layers.

Load the carbig data set, which contains measurements of cars made in the 1970s and early 1980s. Create a matrix X containing the predictor variables Acceleration, Cylinders, and so on. Store the response variable MPG in the variable Y.

load 

carbig

X = [Acceleration Cylinders Displacement Weight]; Y = MPG;

Delete rows of X and Y where either array has missing values.

R = rmmissing([X Y]);
X = R(:,1:end-1);
Y = R(:,end);

Partition the data into training data (XTrain and YTrain) and test data (XTest and YTest). Reserve approximately 20% of the observations for testing, and use the rest of the observations for training.

rng(

"default"

)

% For reproducibility of the partition

c = cvpartition(length(Y),

"Holdout"

,0.20); trainingIdx = training(c);

% Indices for the training set

XTrain = X(trainingIdx,:); YTrain = Y(trainingIdx); testIdx = test(c);

% Indices for the test set

XTest = X(testIdx,:); YTest = Y(testIdx);

Train a neural network regression model. Specify to standardize the predictor data, and to have 30 outputs in the first fully connected layer and 10 outputs in the second fully connected layer. By default, both layers use a rectified linear unit (ReLU) activation function. You can change the activation functions for the fully connected layers by using the Activations name-value argument.

Mdl = fitrnet(XTrain,YTrain,

"Standardize"

,true,

...

"LayerSizes"

,[30 10])
Mdl = 
  RegressionNeuralNetwork
             ResponseName: 'Y'
    CategoricalPredictors: []
        ResponseTransform: 'none'
          NumObservations: 319
               LayerSizes: [30 10]
              Activations: 'relu'
    OutputLayerActivation: 'none'
                   Solver: 'LBFGS'
          ConvergenceInfo: [1x1 struct]
          TrainingHistory: [1000x7 table]


  Properties, Methods

Access the weights and biases for the fully connected layers of the trained model by using the LayerWeights and LayerBiases properties of Mdl. The first two elements of each property correspond to the values for the first two fully connected layers, and the third element corresponds to the values for the final fully connected layer for regression. For example, display the weights and biases for the first fully connected layer.

Mdl.LayerWeights{1}
ans = 

30×4

0.0123 0.0117 -0.0094 0.1175 -0.4081 -0.7849 -0.7201 -2.1720 0.6041 0.1680 -2.3952 0.0934 -3.2332 -2.8360 -1.8264 -1.5723 0.5851 1.5370 1.4623 0.6742 -0.2106 1.2830 -1.7489 -1.5556 0.4800 0.1012 -1.0044 -0.7959 1.8015 -0.5272 -0.7670 0.7496 -1.1428 -0.9902 0.2436 1.2288 -0.0833 -2.4265 0.8388 1.8597 ⋮
Mdl.LayerBiases{1}
ans = 

30×1

-0.4450 -0.8751 -0.3872 -1.1345 0.4499 -2.1555 2.2111 1.2040 -1.4595 0.4639 ⋮

The final fully connected layer has one output. The number of layer outputs corresponds to the first dimension of the layer weights and layer biases.

size(Mdl.LayerWeights{end})
ans = 

1×2

1 10
size(Mdl.LayerBiases{end})
ans = 

1×2

1 1

To estimate the performance of the trained model, compute the test set mean squared error (MSE) for Mdl. Smaller MSE values indicate better performance.

testMSE = loss(Mdl,XTest,YTest)
testMSE = 18.3681

Compare the predicted test set response values to the true response values. Plot the predicted miles per gallon (MPG) along the vertical axis and the true MPG along the horizontal axis. Points on the reference line indicate correct predictions. A good model produces predictions that are scattered near the line.

testPredictions = predict(Mdl,XTest);
plot(YTest,testPredictions,

"."

) hold

on

plot(YTest,YTest) hold

off

xlabel(

"True MPG"

) ylabel(

"Predicted MPG"

)

Figure contains an axes object. The axes object contains 2 objects of type line.