perObservationLoss
R2026bPer observation regression error of model for incremental neural network
Since R2026b
Description
specifies additional options using one or more Err = perObservationLoss(Mdl,X,Y,Name=Value)Name=Value
arguments.
Examples
Load the robot arm data set. Obtain the sample size n and the number of predictor variables p.
load robotarm
n = numel(ytrain);
p = size(Xtrain,2);For details on the data set, enter Description at the command line.
Create an incremental neural network model for regression. Configure the model as follows:
Specify a metrics warm-up period of 1000 observations.
Specify a metrics window size of 500 observations.
Specify no estimation period and use the FreeRex solver.
Mdl = incrementalRegressionNeuralNetwork(MetricsWarmupPeriod=1000,MetricsWindowSize=500,... EstimationPeriod=0,TrainingOptions=incrementalTrainingOptions("freerex"))
Mdl =
incrementalRegressionNeuralNetwork
IsWarm: 0
Metrics: [1×2 table]
ResponseTransform: 'none'
LayerSizes: 10
Activations: "relu"
OutputLayerActivation: "none"
Solver: "freerex"
Properties, Methods
Mdl is an incrementalRegressionNeuralNetwork model object configured for incremental learning.
Preallocate the number of variables in each chunk for creating a stream of data and variables to store the performance metrics.
rng(0,"twister") % For reproducibility numObsPerChunk = 50; nchunk = floor(n/numObsPerChunk); L = zeros(nchunk,1); % To store loss values PoL = zeros(nchunk,50); % To store per observation loss values
Simulate a data stream with incoming chunks of 50 observations each. For each iteration:
Call
updateMetricsandFitto measure the cumulative performance and the performance within a window of observations and fit the model to the incoming data. Overwrite the previous incremental model with the new one.Call
lossto compute the mean squared error on the incoming data andperObservationLossto compute the squared error for each observation and store the performance metrics.
for j = 1:nchunk ibegin = min(n,numObsPerChunk*(j-1) + 1); iend = min(n,numObsPerChunk*j); idx = ibegin:iend; Mdl = updateMetricsAndFit(Mdl,Xtrain(idx,:),ytrain(idx)); L(j) = loss(Mdl,Xtrain(idx,:),ytrain(idx)); PoL(j,:) = perObservationLoss(Mdl,Xtrain(idx,:),ytrain(idx)); end
PerObservationLoss computes the regression loss (squared error) for each observation in each chunk of data after the warm up period (after IsWarm property is 1 (or true)). PoL is an nchunk-by-numObsPerChunk matrix, which, in this example corresponds to a 143-by-50 matrix. Each row corresponds to a window of observation in the stream and each column corresponds to an observation in the corresponding window. The default warmup period is 1000 observations, which corresponds to 20 chunks of incoming data. Hence, first 19 rows of PoL only has NaN values. loss starts computing the mean squared error for each window of data, whether the model is warm or not, so computes the regression error for the first 19 chunks as well. L is a 143-by-1 vector. Each value in L corresponds to the mean of the squared error values in each row of PoL.
Compute the difference between L and the row mean of PoL, and display the values 20 to 25.
diff = abs(L-mean(PoL,2)); diff(20:25)
ans = 6×1
10-15 ×
0.2220
0
0.2220
0.1110
0.2220
0
The difference between the two vectors is negligible.
Input Arguments
Incremental learning model, specified as an incrementalRegressionNeuralNetwork model object. You can create
Mdl directly or by converting a supported, traditionally trained
machine learning model using the incrementalLearner function. For
more details, see the corresponding reference page.
Batch of predictor data with which to compute the per observation loss, specified as
a floating-point matrix of n observations and
Mdl.NumPredictors predictor variables. The value of the
ObservationsIn name-value argument determines the orientation of
the variables and observations.
The length of the observation labels Y and the number of observations in X must be equal; Y( is the label of observation j (row or column) in j)X.
Note
perObservationLoss supports only floating-point
input predictor data. If your input data includes categorical data, you must prepare an encoded
version of the categorical data. Use dummyvar to convert each categorical variable
to a numeric matrix of dummy variables. Then, concatenate all dummy variable matrices and any
other numeric predictors. For more details, see Dummy Variables.
Data Types: single | double
Batch of responses with which to compute the per observation loss, specified as a floating-point vector.
The length of Y and the number of observations in
X must be equal; Y(
is the response for observation j (row or column) in
j)X.
Data Types: single | double
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN, where Name is
the argument name and Value is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Example: ObservationsIn="columns" specifies that the observations are
in columns.
Orientation of data in X, specified as either
"rows" or "columns".
Example: ObservationsIn="columns"
Loss function, specified as "mse" (mean squared error) or a
function handle. To specify a custom loss function, use function handle notation. The
function must have this form:
lossval = lossfcn(Y,YFit)
The output argument
lossvalis a floating-point scalar.You specify the function name (
).lossfcnYis a length n numeric vector of observed responses.YFitis a length n numeric vector of corresponding predicted responses.
Example: LossFun=@lossfcn
Data Types: char | string | function_handle
Type of output loss, specified as "average" or
"per-response".
| Value | Description |
|---|---|
"average" | perObservationLoss averages the loss values across all
response variables and returns a scalar value. |
"per-response" | perObservationLoss returns a vector, where each element
is the loss for one response variable. |
Example: OutputType="per-response"
Data Types: char | string
Flag to standardize the response data before computing the loss, specified as a
numeric or logical 0 (false) or
1 (true). If you set
StandardizeResponses to true, then the
software centers and scales each response variable by the corresponding variable mean
and standard deviation in the training data.
Specify StandardizeResponses as true when
you have multiple response variables with very different scales and
OutputType is "average".
Example: StandardizeResponses=true
Data Types: single | double | logical
Version History
Introduced in R2026b
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Seleccione un país/idioma
Seleccione un país/idioma para obtener contenido traducido, si está disponible, y ver eventos y ofertas de productos y servicios locales. Según su ubicación geográfica, recomendamos que seleccione: .
También puede seleccionar uno de estos países/idiomas:
Cómo obtener el mejor rendimiento
Seleccione China (en idioma chino o inglés) para obtener el mejor rendimiento. Los sitios web de otros países no están optimizados para ser accedidos desde su ubicación geográfica.
América
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)