loss
Description
returns the quantile loss for the trained quantile neural network regression model
L = loss(Mdl,Tbl,ResponseVarName)Mdl. The function uses the predictor data in the table
Tbl and the response values in the
ResponseVarName table variable. For more information, see Quantile Loss.
specifies options using one or more name-value arguments in addition to any of the input
argument combinations in previous syntaxes. For example, you can specify the quantiles for
which to return loss values.L = loss(___,Name=Value)
Examples
Compute the quantile loss for a quantile neural network regression model.
Load the carbig data set, which contains measurements of cars made in the 1970s and early 1980s. Create a table containing the predictor variables Acceleration, Cylinders, Displacement, and so on, as well as the response variable MPG. View the first eight observations.
load carbig cars = table(Acceleration,Cylinders,Displacement, ... Horsepower,Model_Year,Origin,Weight,MPG); head(cars)
Acceleration Cylinders Displacement Horsepower Model_Year Origin Weight MPG
____________ _________ ____________ __________ __________ _______ ______ ___
12 8 307 130 70 USA 3504 18
11.5 8 350 165 70 USA 3693 15
11 8 318 150 70 USA 3436 18
12 8 304 150 70 USA 3433 16
10.5 8 302 140 70 USA 3449 17
10 8 429 198 70 USA 4341 15
9 8 454 220 70 USA 4354 14
8.5 8 440 215 70 USA 4312 14
Remove rows of cars where the table has missing values.
cars = rmmissing(cars);
Categorize the cars based on whether they were made in the USA.
cars.Origin = categorical(cellstr(cars.Origin)); cars.Origin = mergecats(cars.Origin,["France","Japan",... "Germany","Sweden","Italy","England"],"NotUSA");
Partition the data into training and test sets using cvpartition. Use approximately 80% of the observations as training data, and 20% of the observations as test data.
rng(0,"twister") % For reproducibility of the data partition c = cvpartition(height(cars),"Holdout",0.20); trainingIdx = training(c); carsTrain = cars(trainingIdx,:); testIdx = test(c); carsTest = cars(testIdx,:);
Train a quantile neural network regression model using the carsTrain training data. Specify MPG as the response variable. Then, compute the quantile loss using the carsTest test data.
Mdl = fitrqnet(carsTrain,"MPG");
L = loss(Mdl,carsTest)L = 3.2199
Retrain the model with standardized numeric predictors, and then compute the test set quantile loss.
newMdl = fitrqnet(carsTrain,"MPG",Standardize=true);
newL = loss(newMdl,carsTest)newL = 0.9196
The retrained model has a lower quantile loss.
Input Arguments
Trained quantile neural network regression model, specified as a RegressionQuantileNeuralNetwork or CompactRegressionQuantileNeuralNetwork model object.
Sample data, specified as a table. Each row of Tbl corresponds
to one observation, and each column corresponds to one predictor variable. Optionally,
Tbl can contain additional columns for the response variable and
the observation weights. Tbl must contain all of the predictors
used to train Mdl. Multicolumn variables and cell arrays other than
cell arrays of character vectors are not allowed.
If
Tblcontains the response variable used to trainMdl, then you do not need to specifyResponseVarNameorY.If you trained
Mdlusing sample data contained in a table, then the input data forlossmust also be in a table.If you set
Standardizetotrueinfitrqnetwhen trainingMdl, then the software standardizes the numeric columns of the predictor data using the corresponding means (Mdl.Mu) and standard deviations (Mdl.Sigma).
Data Types: table
Response variable name, specified as the name of a variable in
Tbl. The response variable must be a numeric vector.
You must specify ResponseVarName as a character vector or cell
array of character vectors. For example, if Tbl stores the response
variable as Tbl.Y, then specify ResponseVarName
as "Y". Otherwise, the software treats the Y
column of Tbl as a predictor.
Data Types: char | string
Predictor data, specified as a numeric matrix. By default,
loss assumes that each row of X
corresponds to one observation, and each column corresponds to one predictor variable.
Note
If you orient your predictor matrix so that observations correspond to columns
and specify ObservationsIn="columns", then you might experience a
significant reduction in computation time.
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: loss(Mdl,Tbl,"Response",Quantiles=[0.25 0.5 0.75]) specifies
to compute the quantile loss for the 0.25, 0.5, and 0.75 quantiles.
Quantiles for which to compute the loss, specified as a vector of values in
Mdl.Quantiles. The function returns the loss for each quantile
separately.
Example: Quantiles=[0.4 0.6]
Data Types: single | double | char | string
Loss function, specified as "quantile" or a function handle.
"quantile"— Quantile loss. For more information, see Quantile Loss.Function handle — To specify a custom loss function, use a function handle. The function must have this form:
lossval = lossfun(Y,YFit,W,q)
The output argument
lossvalis a numeric scalar.You specify the function name (
).lossfunYis a length-n numeric vector of observed responses, where n is the number of observations inTblorX.YFitis a length-n numeric vector of corresponding predicted responses.Wis an n-by-1 numeric vector of observation weights.qis a numeric scalar in the range [0,1] corresponding to a quantile.
Example: LossFun=@lossfun
Data Types: char | string | function_handle
Predictor data observation dimension, specified as "rows" or
"columns".
Note
If you orient your predictor matrix so that observations correspond to columns
and specify ObservationsIn="columns", then you might experience a
significant reduction in computation time. You cannot specify
ObservationsIn="columns" for predictor data in a table.
Example: ObservationsIn="columns"
Data Types: char | string
Since R2025a
Predicted response value to use for observations with missing predictor values,
specified as "quantile", "omitted", a numeric
scalar, or a numeric vector.
| Value | Description |
|---|---|
"quantile" | loss uses the specified quantile of the
observed response values in the training data as the predicted response value
for observations with missing predictor values. |
"omitted" | loss excludes observations with missing
predictor values from the loss computation. |
| Numeric scalar or vector |
|
If an observation is missing an observed response value or an observation weight,
then loss does not use the observation in the loss
computation.
Example: PredictionForMissingValue="omitted"
Data Types: single | double | char | string
Observation weights, specified as a nonnegative numeric vector or the name of a
variable in Tbl. The software weights each observation in
X or Tbl with the corresponding value in
Weights. The length of Weights must equal
the number of observations in X or
Tbl.
If you specify the input data as a table Tbl, then
Weights can be the name of a variable in
Tbl that contains a numeric vector. In this case, you must
specify Weights as a character vector or string scalar. For
example, if the weights vector W is stored as
Tbl.W, then specify it as "W".
By default, Weights is ones(n,1), where
n is the number of observations in X or
Tbl. If you supply weights, then
loss computes the weighted loss and normalizes the
weights to sum to 1.
Data Types: single | double | char | string
Output Arguments
Algorithms
Version History
Introduced in R2024bYou can compute the loss for a compact quantile regression model (CompactRegressionQuantileNeuralNetwork).
You can also specify the prediction values for observations with missing predictor
values by using the PredictionForMissingValues name-value argument. In previous releases,
observations with missing predictor values have NaN predictions. That is,
the behavior is equivalent to PredictionForMissingValue=NaN.
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.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)