dlnetwork regression example in matlab

Here's an example on how to use dlnetwork for regression in Matlab:

main.m
% Load the data
load housing_dataset
X = housing{:, 4:19};
Y = housing{:, 3};

% Split data into training and testing sets
cv = cvpartition(size(X, 1), 'HoldOut', 0.3); 
XTrain = X(cv.training,:);
YTrain = Y(cv.training,:);
XTest = X(cv.test,:);
YTest = Y(cv.test,:);

% Define the dlnetwork
numFeatures = size(XTrain, 2);
numHiddenUnits = 10;
numResponses = 1;
layers = [ ...
    sequenceInputLayer(numFeatures)
    fullyConnectedLayer(numHiddenUnits)
    reluLayer
    fullyConnectedLayer(numResponses)
    regressionLayer];

% Set options for training the network
options = trainingOptions('adam', ...
    'MaxEpochs', 500, ...
    'MiniBatchSize', 64, ...
    'InitialLearnRate', 0.001, ...
    'Shuffle', 'every-epoch', ...
    'Verbose', false);

% Train the network
net = trainNetwork(XTrain', YTrain', layers, options);

% Test the network
YPred = predict(net, XTest');
rmse = sqrt(mean((YPred - YTest').^2));
fprintf('Root Mean Squared Error = %.2f\n', rmse);
969 chars
39 lines

In this example, the housing dataset is loaded and split into training and testing sets. A dlnetwork with a sequence input layer, fully connected layer (with ReLu activation) and regression layer is defined. The network is trained using the training set and options set by trainingOptions. Finally, the trained network is used to predict on the testing set, and the RMSE is calculated to evaluate the performance.

gistlibby LogSnag