Main Content

Configure Simulink Template for Rate-Based Incremental Linear Regression

Since R2024a

This example shows how to configure the Simulink® Rate-Based Incremental Learning template to perform incremental linear regression. The template is set up for linear classification, but you can also configure it for regression models. Create and train a linear regression model for incremental learning. Create input data to simulate streaming data for the Simulink Inport blocks. Load the template and configure it for incremental linear regression. Configure the rate transition blocks to specify when the model predicts responses and updates metrics. Run the model in Simulink to predict responses.

Create Incremental Learner Model

Load the robot arm data set.

load robotarm.mat
n = numel(ytrain);  % Number of observations
p = size(Xtrain,2); % Number of predictors

For details on the data set, enter Description at the command line.

Create an incremental linear model object for regression. Specify that the data has p predictors and to standardize the data using an estimation period of 500 observations. Create a workspace variable linearMdl to store the initial incremental learning model.

rng(0,"twister") % For reproducibility
Mdl = incrementalRegressionLinear(NumPredictors=p, ...
    Standardize=true,EstimationPeriod=500);
linearMdl = Mdl;

Create Input Data for Simulink

Simulate streaming data by dividing the training data into chunks of 50 observations. For each chunk, select a single observation as a test set.

numObsPerChunk = 50;
nchunk = floor(n/numObsPerChunk);
for j = 1:nchunk
    ibegin = min(n,numObsPerChunk*(j-1) + 1);
    iend = min(n,numObsPerChunk*j);
    idx = ibegin:iend;   
    Xin(:,:,j) = Xtrain(idx,:);
    Yin(:,j) = ytrain(idx);
    Xtestset(1,:,j) = Xtest(j,:);
end

Convert the training and test set chunks into time series objects.

k = size(Xin,3); % Number of data chunks
t = 0:k-1;
X_ts = timeseries(Xin,t,InterpretSingleRowDataAs3D=true);
Y_ts = timeseries(Yin',t,InterpretSingleRowDataAs3D=true);
Xtest_ts = timeseries(Xtestset,t,InterpretSingleRowDataAs3D=true);

Load and Configure Template to Perform Incremental Linear Regression

Load the Rate-Based Incremental Learning template. The template is also available on the Simulink Start Page under Statistics and Machine Learning.

template = Simulink.createFromTemplate("multirate_linear_classif.sltx");
open_system(template)
Warning: In instantiating linked block '<a href="matlab:open_and_hilite_hyperlink ('untitled/Update Metrics','error')">untitled/Update Metrics</a>' : Invalid setting in Update Metrics block (mask) 'Update Metrics' for parameter 'Metric'

The template is configured for incremental linear classification. Edit the template to configure it for incremental linear regression:

  1. Delete the IncrementalClassificationLinear Fit block.

  2. Add an IncrementalRegressionLinear Fit block and connect it to the existing Inport blocks and the output signal line.

  3. Delete the IncrementalClassificationLinear Predict block.

  4. Add an IncrementalRegressionLinear Predict block and connect it to the existing Rate Transition, Inport, and Outport blocks.

Adjust Sample Times

You can specify how often Simulink updates the performance metrics of the model. Double-click the Update Metrics block, enter 4 in the Sample time dialog box and click OK. You can also specify how often Simulink predicts responses from the test data. Double-click the IncrementalRegressionLinear Predict block, enter 2 in the Sample time dialog box and click OK.

Configure Simulink Model Parameters

Click on the Simulink template canvas to select it as the current system. The template contains five Inport blocks: x, y, x1, y1, and x2. Specify to enable external input and to use the streaming data X_ts and Y_ts as inputs to the IncrementalRegressionLinear Fit block and the Update Metrics block. Use the test data Xtest_ts as the input to the IncrementalRegressionLinear Predict block.

set_param(gcs,LoadExternalInput="on")
set_param(gcs,ExternalInput="X_ts,Y_ts,X_ts,Y_ts,Xtest_ts")

Specify the port dimensions of the predictor data Inport blocks for the IncrementalRegressionLinear Fit block and Update Metrics block (x and x1) as [numObsPerChunk,p], and specify their output data type as double.

xPortNames = {'/x','/x1'};
for indX = 1:numel(xPortNames)
    xNamePath = [gcs,xPortNames{indX}];
    set_param(xNamePath,PortDimensions= ...
        "["+num2str(numObsPerChunk)+","+num2str(p)+"]")
    set_param(xNamePath,OutDataTypeStr="double")
end

Specify the port dimensions of the predictor data Inport block x2 as [1,p], and specify its output data type as double.

set_param([gcs,'/x2'],PortDimensions="["+'1'+","+num2str(p)+"]")
set_param([gcs,'/x2'],OutDataTypeStr="double")

Specify the port dimensions of the response data Inport blocks (y and y1) as [numObsPerChunk,p], and specify their output data type as double.

yPortNames = {'/y','/y1'};
for indY = 1:numel(yPortNames)
    yNamePath = [gcs,yPortNames{indY}];
    set_param(yNamePath,PortDimensions=num2str(numObsPerChunk))
    set_param(yNamePath,OutDataTypeStr="double")
end

Use the fixed-step solver and set the simulation stop time to the number of data chunks.

set_param(gcs,SolverType="Fixed-step")
set_param(gcs,StopTime=num2str(k))

You can set additional tasking and sample time options in the Solver pane of Model Settings on the Modeling tab.

Simulate Model

Click the Run button in the Simulink model to perform incremental learning and predict responses to the test set observations. You can use the Simulation Data Inspector (Simulink) to view the logged data of the Outport blocks.

See Also

| | | | | |

Related Topics