Main Content

Configure Simulink Template for Conditionally Enabled Incremental Linear Regression

Since R2024a

This example shows how to configure the Simulink® Enabled Execution for Incremental Learning template to perform incremental linear regression. Create and train a linear regression model for incremental learning. Edit the blocks in the template and prepare data for the input ports. Configure the conditional logic subsystem block, which determines when Simulink fits the model and updates the performance 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 Enabled Execution for Incremental Learning template. The template is also available on the Simulink Start Page under Statistics and Machine Learning.

template = Simulink.createFromTemplate("enabled_linear_regr.sltx");
open_system(template)

Configure Conditional Logic Subsystem

Double-click the Conditional Logic subsystem block. You can modify the sample conditional logic model, which determines when the system fits the incremental learning model and calculates performance metrics. For example, you can change the triggering levels for window loss and cumulative loss, and whether to use the IsWarm and CanPredict properties of the incremental learning model when triggering.

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