Main Content

fitrqnet

Train regression quantile neural network

Since R2024b

    Description

    Mdl = fitrqnet(Tbl,ResponseVarName) returns a trained regression quantile neural network model Mdl. The function trains the model using the predictors in the table Tbl and the response values in the ResponseVarName table variable.

    By default, the function uses the median (0.5 quantile).

    Mdl = fitrqnet(Tbl,formula) returns a quantile neural network model trained using the sample data in the table Tbl. The input argument formula is an explanatory model of the response and a subset of the predictor variables in Tbl used to fit Mdl.

    Mdl = fitrqnet(Tbl,Y) returns a quantile neural network model trained using the predictor variables in the table Tbl and the response values in the vector Y.

    Mdl = fitrqnet(X,Y) returns a quantile neural network model trained using the predictors in the matrix X and the response values in the vector Y.

    Mdl = fitrqnet(___,Name=Value) 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 by using the Quantiles name-value argument.

    example

    [Mdl,AggregateOptimizationResults] = fitrqnet(___) also returns AggregateOptimizationResults, which contains hyperparameter optimization results when you specify the OptimizeHyperparameters and HyperparameterOptimizationOptions name-value arguments. You must also specify the ConstraintType and ConstraintBounds options of HyperparameterOptimizationOptions. You can use this syntax to optimize on the compact model size instead of the cross-validation loss, and to solve a set of multiple optimization problems that have the same options but different constraint bounds. (since R2025a)

    Note

    Hyperparameter optimization is supported only for models with one quantile.

    Examples

    collapse all

    Fit a quantile neural network regression model using the 0.25, 0.50, and 0.75 quantiles.

    Load the carbig data set, which contains measurements of cars made in the 1970s and early 1980s. Create a matrix X containing the predictor variables Acceleration, Displacement, Horsepower, and Weight. Store the response variable MPG in the variable Y.

    load carbig
    X = [Acceleration,Displacement,Horsepower,Weight];
    Y = MPG;

    Delete rows of X and Y where either array has missing values.

    R = rmmissing([X Y]);
    X = R(:,1:end-1);
    Y = R(:,end);

    Partition the data into training data (XTrain and YTrain) and test data (XTest and YTest). Reserve approximately 20% of the observations for testing, and use the rest of the observations for training.

    rng(0,"twister") % For reproducibility of the partition
    c = cvpartition(length(Y),"Holdout",0.20);
     
    trainingIdx = training(c);
    XTrain = X(trainingIdx,:);
    YTrain = Y(trainingIdx);
     
    testIdx = test(c);
    XTest = X(testIdx,:);
    YTest = Y(testIdx);

    Train a quantile neural network regression model. Specify to use the 0.25, 0.50, and 0.75 quantiles (that is, the lower quartile, median, and upper quartile). To improve the model fit, standardize the numeric predictors. Use a ridge (L2) regularization term of 1. Adding a regularization term can help prevent quantile crossing.

    Mdl = fitrqnet(XTrain,YTrain,Quantiles=[0.25,0.50,0.75], ...
        Standardize=true,Lambda=0.05)
    Mdl = 
      RegressionQuantileNeuralNetwork
                 ResponseName: 'Y'
        CategoricalPredictors: []
                   LayerSizes: 10
                  Activations: 'relu'
        OutputLayerActivation: 'none'
                    Quantiles: [0.2500 0.5000 0.7500]
    
    
      Properties, Methods
    
    

    Mdl is a RegressionQuantileNeuralNetwork model object. You can use dot notation to access the properties of Mdl. For example, Mdl.LayerWeights and Mdl.LayerBiases contain the weights and biases, respectively, for the fully connected layers of the trained model.

    In this example, you can use the layer weights, layer biases, predictor means, and predictor standard deviations directly to predict the test set responses for each of the three quantiles in Mdl.Quantiles. In general, you can use the predict object function to make quantile predictions.

    firstFCStep = (Mdl.LayerWeights{1})*((XTest-Mdl.Mu)./Mdl.Sigma)' ...
        + Mdl.LayerBiases{1};
    reluStep = max(firstFCStep,0);
    finalFCStep = (Mdl.LayerWeights{end})*reluStep + Mdl.LayerBiases{end};
    predictedY = finalFCStep'
    predictedY = 78×3
    
       13.9602   15.1340   16.6884
       11.2792   12.2332   13.4849
       19.5525   21.7303   23.9473
       22.6950   25.5260   28.1201
       10.4533   11.3377   12.4984
       17.6935   19.5194   21.5152
       12.4312   13.4797   14.8614
       11.7998   12.7963   14.1071
       16.6860   18.3305   20.2070
       24.1142   27.0301   29.7811
       22.2832   25.1327   27.6841
       12.8749   13.9594   15.3917
       12.2328   13.2643   14.6245
       24.0164   26.9150   29.6545
       13.4641   14.5970   16.0957
          ⋮
    
    
    isequal(predictedY,predict(Mdl,XTest))
    ans = logical
       1
    
    

    Each column of predictedY corresponds to a separate quantile (0.25, 0.5, or 0.75).

    Visualize the predictions of the quantile neural network regression model. First, create a grid of predictor values.

    minX = floor(min(X))
    minX = 1×4
    
               8          68          46        1613
    
    
    maxX = ceil(max(X))
    maxX = 1×4
    
              25         455         230        5140
    
    
    gridX = zeros(100,size(X,2));
    for p = 1:size(X,2)
        gridp = linspace(minX(p),maxX(p))';
        gridX(:,p) = gridp;
    end

    Next, use the trained model Mdl to predict the response values for the grid of predictor values.

    gridY = predict(Mdl,gridX)
    gridY = 100×3
    
       31.2419   35.0661   38.6357
       30.8637   34.6317   38.1573
       30.4854   34.1972   37.6789
       30.1072   33.7627   37.2005
       29.7290   33.3283   36.7221
       29.3507   32.8938   36.2436
       28.9725   32.4593   35.7652
       28.5943   32.0249   35.2868
       28.2160   31.5904   34.8084
       27.8378   31.1560   34.3300
       27.4596   30.7215   33.8516
       27.0814   30.2870   33.3732
       26.7031   29.8526   32.8948
       26.3249   29.4181   32.4164
       25.9467   28.9837   31.9380
          ⋮
    
    

    For each observation in gridX, the predict object function returns predictions for the quantiles in Mdl.Quantiles.

    View the gridY predictions for the second predictor (Displacement). Compare the quantile predictions to the true test data values.

    predictorIdx = 2;
    plot(XTest(:,predictorIdx),YTest,".")
    hold on
    plot(gridX(:,predictorIdx),gridY(:,1))
    plot(gridX(:,predictorIdx),gridY(:,2))
    plot(gridX(:,predictorIdx),gridY(:,3))
    hold off
    xlabel("Predictor (Displacement)")
    ylabel("Response (MPG)")
    legend(["True values","0.25 predicted values", ...
        "0.50 predicted values","0.75 predicted values"])
    title("Test Data")

    Figure contains an axes object. The axes object with title Test Data, xlabel Predictor (Displacement), ylabel Response (MPG) contains 4 objects of type line. One or more of the lines displays its values using only markers These objects represent True values, 0.25 predicted values, 0.50 predicted values, 0.75 predicted values.

    The red curve shows the predictions for the 0.25 quantile, the yellow curve shows the predictions for the 0.50 quantile, and the purple curve shows the predictions for the 0.75 quantile. The blue points indicate the true test data values.

    Notice that the quantile prediction curves do not cross each other.

    When training a quantile neural network regression model, you can use a ridge (L2) regularization term to prevent quantile crossing.

    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.

    load carbig
    cars = table(Acceleration,Cylinders,Displacement, ...
        Horsepower,Model_Year,Origin,Weight,MPG);

    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. Use the 0.25, 0.50, and 0.75 quantiles (that is, the lower quartile, median, and upper quartile). To improve the model fit, standardize the numeric predictors before training.

    Mdl = fitrqnet(carsTrain,"MPG",Quantiles=[0.25 0.5 0.75], ...
        Standardize=true);

    Mdl is a RegressionNeuralNetwork model object.

    Determine if the test data predictions for the quantiles in Mdl.Quantiles cross each other by using the predict object function of Mdl. The crossingIndicator output argument contains a value of 1 (true) for any observation with quantile predictions that cross.

    [~,crossingIndicator] = predict(Mdl,carsTest);
    sum(crossingIndicator)
    ans = 
    2
    

    In this example, two of the observations in carsTest have quantile predictions that cross each other.

    To prevent quantile crossing, specify the Lambda name-value argument in the call to fitrqnet. Use a 0.05 ridge (L2) penalty term.

    newMdl = fitrqnet(carsTrain,"MPG",Quantiles=[0.25 0.5 0.75], ...
        Standardize=true,Lambda=0.05);
    [predictedY,newCrossingIndicator] = predict(newMdl,carsTest);
    sum(newCrossingIndicator)
    ans = 
    0
    

    With regularization, the predictions for the test data set do not cross for any observations.

    Visualize the predictions returned by newMdl by using a scatter plot with a reference line. Plot the predicted values along the vertical axis and the true response values along the horizontal axis. Points on the reference line indicate correct predictions.

    plot(carsTest.MPG,predictedY(:,1),".")
    hold on
    plot(carsTest.MPG,predictedY(:,2),".")
    plot(carsTest.MPG,predictedY(:,3),".")
    plot(carsTest.MPG,carsTest.MPG)
    hold off
    xlabel("True MPG")
    ylabel("Predicted MPG")
    legend(["0.25 quantile values","0.50 quantile values", ...
        "0.75 quantile values","Reference line"], ...
        Location="southeast")
    title("Test Data")

    Figure contains an axes object. The axes object with title Test Data, xlabel True MPG, ylabel Predicted MPG contains 4 objects of type line. One or more of the lines displays its values using only markers These objects represent 0.25 quantile values, 0.50 quantile values, 0.75 quantile values, Reference line.

    Blue points correspond to the 0.25 quantile, red points correspond to the 0.50 quantile, and yellow points correspond to the 0.75 quantile.

    For a more in-depth example, see Regularize Quantile Regression Model to Prevent Quantile Crossing.

    Input Arguments

    collapse all

    Sample data used to train the model, specified as a table. Each row of Tbl corresponds to one observation, and each column corresponds to one predictor variable. Optionally, Tbl can contain one additional column for the response variable. Multicolumn variables and cell arrays other than cell arrays of character vectors are not allowed.

    • If Tbl contains the response variable, and you want to use all remaining variables in Tbl as predictors, then specify the response variable by using ResponseVarName.

    • If Tbl contains the response variable, and you want to use only a subset of the remaining variables in Tbl as predictors, then specify a formula by using formula.

    • If Tbl does not contain the response variable, then specify a response variable by using Y. The length of the response variable and the number of rows in Tbl must be equal.

    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 string scalar. For example, if Tbl stores the response variable Y as Tbl.Y, then specify it as "Y". Otherwise, the software treats all columns of Tbl, including Y, as predictors when training the model.

    Data Types: char | string

    Explanatory model of the response variable and a subset of the predictor variables, specified as a character vector or string scalar in the form "Y~x1+x2+x3". In this form, Y represents the response variable, and x1, x2, and x3 represent the predictor variables.

    To specify a subset of variables in Tbl as predictors for training the model, use a formula. If you specify a formula, then the software does not use any variables in Tbl that do not appear in formula.

    The variable names in the formula must be both variable names in Tbl (Tbl.Properties.VariableNames) and valid MATLAB® identifiers. You can verify the variable names in Tbl by using the isvarname function. If the variable names are not valid, then you can convert them by using the matlab.lang.makeValidName function.

    Data Types: char | string

    Response data, specified as a numeric vector. The length of Y must be equal to the number of observations in X or Tbl.

    Data Types: single | double

    Predictor data used to train the model, specified as a numeric matrix.

    By default, the software treats each row of X as one observation, and each column as one predictor.

    The length of Y and the number of observations in X must be equal.

    To specify the names of the predictors in the order of their appearance in X, use the PredictorNames name-value argument.

    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

    Note

    The software treats NaN, empty character vector (''), empty string (""), <missing>, and <undefined> elements as missing values, and removes observations with any of these characteristics:

    • Missing value in the response (for example, Y or ValidationData{2})

    • At least one missing value in a predictor observation (for example, a row in X or ValidationData{1})

    • NaN value or 0 weight (for example, a value in Weights or ValidationData{3})

    Name-Value Arguments

    expand all

    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: fitrqnet(Tbl,"MPG",Quantiles=[0.25 0.5 0.75],Standardize=true) specifies to use the 0.25, 0.5, and 0.75 quantiles and to standardize the data before training.

    Neural Network Options

    expand all

    Quantiles to use for training Mdl, specified as a vector of values in the range [0,1]. The function trains a model that separates the bottom 100*q percent of training responses from the top 100*(1 – q) percent of training responses for each quantile q.

    Example: Quantiles=[0.25 0.5 0.75]

    Data Types: single | double

    Sizes of the fully connected layers in the quantile neural network regression model, specified as a positive integer vector. Element i of LayerSizes is the number of outputs in the fully connected layer i of the neural network model.

    LayerSizes does not include the size of the final fully connected layer. For more information, see Quantile Neural Network Structure.

    Example: LayerSizes=[100 25 10]

    Data Types: single | double

    Activation functions for the fully connected layers of the quantile neural network regression model, specified as a character vector, string scalar, string array, or cell array of character vectors with values from this table.

    ValueDescription
    "relu"

    Rectified linear unit (ReLU) function — Performs a threshold operation on each element of the input, where any value less than zero is set to zero, that is,

    f(x)={x,x00,x<0

    "tanh"

    Hyperbolic tangent (tanh) function — Applies the tanh function to each input element

    "sigmoid"

    Sigmoid function — Performs the following operation on each input element:

    f(x)=11+ex

    "none"

    Identity function — Returns each input element without performing any transformation, that is, f(x) = x

    • If you specify one activation function only, then Activations is the activation function for every fully connected layer of the neural network model, excluding the final fully connected layer (see Quantile Neural Network Structure).

    • If you specify an array of activation functions, then element i of Activations is the activation function for layer i of the neural network model.

    Example: Activations="sigmoid"

    Data Types: char | string | cell

    Function to initialize the fully connected layer weights, specified as "glorot" or "he".

    ValueDescription
    "glorot"Initialize the weights with the Glorot initializer [1] (also known as the Xavier initializer). For each layer, the Glorot initializer independently samples from a uniform distribution with zero mean and variance 2/(I+O), where I is the input size and O is the output size for the layer.
    "he"Initialize the weights with the He initializer [2]. For each layer, the He initializer samples from a normal distribution with zero mean and variance 2/I, where I is the input size for the layer.

    Example: LayerWeightsInitializer="he"

    Data Types: char | string

    Type of initial fully connected layer biases, specified as "zeros" or "ones".

    • If you specify the value "zeros", then each fully connected layer has an initial bias of 0.

    • If you specify the value "ones", then each fully connected layer has an initial bias of 1.

    Example: LayerBiasesInitializer="ones"

    Data Types: char | string

    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

    Regularization term strength, specified as a nonnegative scalar. The software constructs the objective function for minimization from the quantile loss averaged over the quantiles (see Quantile Loss) and the ridge (L2) penalty term.

    Example: Lambda=1e-4

    Data Types: single | double

    Flag to standardize the predictor data, specified as a numeric or logical 0 (false) or 1 (true). If you set Standardize to true, then the software centers and scales each numeric predictor variable by the corresponding column mean and standard deviation. The software does not standardize categorical predictors.

    Example: Standardize=true

    Data Types: single | double | logical

    Convergence Control Options

    expand all

    Verbosity level, specified as 0 or 1. The Verbose name-value argument controls the display of diagnostic information at the command line.

    ValueDescription
    0fitrqnet does not display diagnostic information.
    1fitrqnet periodically displays diagnostic information.

    fitrqnet stores the diagnostic information in Mdl. Use Mdl.ConvergenceInfo.History to access the diagnostic information.

    Example: Verbose=1

    Data Types: single | double

    Frequency of verbose printing, which is the number of iterations between printing diagnostic information at the command line, specified as a positive integer scalar. A value of 1 indicates to print diagnostic information at every iteration.

    Note

    To use this name-value argument, you must set Verbose to 1.

    Example: VerboseFrequency=5

    Data Types: single | double

    Initial step size, specified as a positive scalar or "auto". By default, fitrqnet does not use the initial step size to determine the initial Hessian approximation used in training the model. However, if you specify an initial step size s0, then the initial inverse-Hessian approximation is s00I. 0 is the initial gradient vector, and I is the identity matrix.

    To have fitrqnet determine an initial step size automatically, specify the value as "auto". In this case, the function determines the initial step size by using s0=0.5η0+0.1. s0 is the initial step vector, and η0 is the vector of unconstrained initial weights and biases.

    Example: InitialStepSize="auto"

    Data Types: single | double | char | string

    Maximum number of training iterations, specified as a positive integer scalar.

    The software returns a trained model regardless of whether the training routine successfully converges. Mdl.ConvergenceInfo.ConvergenceCriterion contains convergence information.

    Example: IterationLimit=1e8

    Data Types: single | double

    Relative gradient tolerance, specified as a nonnegative scalar.

    Let t be the loss function at training iteration t, t be the gradient of the loss function with respect to the weights and biases at iteration t, and 0 be the gradient of the loss function at an initial point. If max|t|aGradientTolerance, where a=max(1,min|t|,max|0|), then the training process terminates.

    Example: GradientTolerance=1e-5

    Data Types: single | double

    Loss tolerance, specified as a nonnegative scalar.

    If the function loss at some iteration is smaller than LossTolerance, then the training process terminates.

    Example: LossTolerance=1e-8

    Data Types: single | double

    Step size tolerance, specified as a nonnegative scalar.

    If the step size at some iteration is smaller than StepTolerance, then the training process terminates.

    Example: StepTolerance=1e-4

    Data Types: single | double

    Validation data for training convergence detection, specified as a cell array or a table.

    During the training process, the software periodically estimates the validation loss by using ValidationData. If the validation loss increases more than ValidationPatience times consecutively, then the software terminates the training.

    You can specify ValidationData as a table if you use a table Tbl of predictor data that contains the response variable. In this case, ValidationData must contain the same predictors and response contained in Tbl. The software does not apply weights to observations, even if Tbl contains a vector of weights. To specify weights, you must specify ValidationData as a cell array.

    If you specify ValidationData as a cell array, then it must have the following format:

    • ValidationData{1} must have the same data type and orientation as the predictor data. That is, if you use a predictor matrix X, then ValidationData{1} must be an m-by-p or p-by-m matrix of predictor data that has the same orientation as X. The predictor variables in the training data X and ValidationData{1} must correspond. Similarly, if you use a predictor table Tbl of predictor data, then ValidationData{1} must be a table containing the same predictor variables contained in Tbl. The number of observations in ValidationData{1} and the predictor data can vary.

    • ValidationData{2} must match the data type and format of the response variable, either Y or ResponseVarName. If ValidationData{2} is an array of responses, then it must have the same number of elements as the number of observations in ValidationData{1}. If ValidationData{1} is a table, then ValidationData{2} can be the name of the response variable in the table. If you want to use the same ResponseVarName or formula, you can specify ValidationData{2} as [].

    • Optionally, you can specify ValidationData{3} as an m-dimensional numeric vector of observation weights or the name of a variable in the table ValidationData{1} that contains observation weights. The software normalizes the weights with the validation data so that they sum to 1.

    If you specify ValidationData and want to display the validation loss at the command line, set Verbose to 1.

    Data Types: table | cell

    Number of iterations between validation evaluations, specified as a positive integer scalar. A value of 1 indicates to evaluate validation metrics at every iteration.

    Note

    To use this name-value argument, you must specify ValidationData.

    Example: ValidationFrequency=5

    Data Types: single | double

    Stopping condition for validation evaluations, specified as a nonnegative integer scalar. Training stops if the validation loss is greater than or equal to the minimum validation loss computed so far, ValidationPatience times consecutively. You can check the Mdl.ConvergenceInfo.History table to see the running total of times that the validation loss is greater than or equal to the minimum (Validation Checks).

    Example: ValidationPatience=10

    Data Types: single | double

    Other Regression Options

    expand all

    Categorical predictors list, specified as one of the values in this table. The descriptions assume that the predictor data has observations in rows and predictors in columns.

    ValueDescription
    Vector of positive integers

    Each entry in the vector is an index value indicating that the corresponding predictor is categorical. The index values are between 1 and p, where p is the number of predictors used to train the model.

    If fitrqnet uses a subset of input variables as predictors, then the function indexes the predictors using only the subset. The CategoricalPredictors values do not count any response variable, observation weights variable, or other variable that the function does not use.

    Logical vector

    A true entry means that the corresponding predictor is categorical. The length of the vector is p.

    Character matrixEach row of the matrix is the name of a predictor variable. The names must match the entries in PredictorNames. Pad the names with extra blanks so each row of the character matrix has the same length.
    String array or cell array of character vectorsEach element in the array is the name of a predictor variable. The names must match the entries in PredictorNames.
    "all"All predictors are categorical.

    By default, if the predictor data is in a table (Tbl), fitrqnet assumes that a variable is categorical if it is a logical vector, categorical vector, character array, string array, or cell array of character vectors. If the predictor data is a matrix (X), fitrqnet assumes that all predictors are continuous. To identify any other predictors as categorical predictors, specify them by using the CategoricalPredictors name-value argument.

    For the identified categorical predictors, fitrqnet creates dummy variables using two different schemes, depending on whether a categorical variable is unordered or ordered. For an unordered categorical variable, fitrqnet creates one dummy variable for each level of the categorical variable. For an ordered categorical variable, fitrqnet creates one less dummy variable than the number of categories. For details, see Automatic Creation of Dummy Variables.

    Example: CategoricalPredictors="all"

    Data Types: single | double | logical | char | string | cell

    Predictor variable names, specified as a string array of unique names or cell array of unique character vectors. The functionality of PredictorNames depends on the way you supply the training data.

    • If you supply X and Y, then you can use PredictorNames to assign names to the predictor variables in X.

      • The order of the names in PredictorNames must correspond to the predictor order in X. Assuming that X has the default orientation, with observations in rows and predictors in columns, PredictorNames{1} is the name of X(:,1), PredictorNames{2} is the name of X(:,2), and so on. Also, size(X,2) and numel(PredictorNames) must be equal.

      • By default, PredictorNames is {'x1','x2',...}.

    • If you supply Tbl, then you can use PredictorNames to choose which predictor variables to use in training. That is, fitrqnet uses only the predictor variables in PredictorNames and the response variable during training.

      • PredictorNames must be a subset of Tbl.Properties.VariableNames and cannot include the name of the response variable.

      • By default, PredictorNames contains the names of all predictor variables.

      • A good practice is to specify the predictors for training using either PredictorNames or formula, but not both.

    Example: PredictorNames=["SepalLength","SepalWidth","PetalLength","PetalWidth"]

    Data Types: string | cell

    Response variable name, specified as a character vector or string scalar.

    • If you supply Y, then you can use ResponseName to specify a name for the response variable.

    • If you supply ResponseVarName or formula, then you cannot use ResponseName.

    Example: ResponseName="response"

    Data Types: char | string

    Function for transforming raw response values, specified as a function handle or function name. The default is "none", which means @(y)y, or no transformation. The function should accept a vector (the original response values) and return a vector of the same size (the transformed response values).

    Example: Suppose you create a function handle that applies an exponential transformation to an input vector by using myfunction = @(y)exp(y). Then, you can specify the response transformation as ResponseTransform=myfunction.

    Data Types: char | string | function_handle

    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". Otherwise, the software treats all columns of Tbl, including W, as predictors when training the model.

    By default, Weights is ones(n,1), where n is the number of observations in X or Tbl.

    fitrqnet normalizes the weights to sum to 1.

    Data Types: single | double | char | string

    Cross-Validation Options

    expand all

    Since R2025a

    Flag to train a cross-validated model, specified as "on" or "off".

    If you specify "on", then the software trains a cross-validated model with 10 folds.

    You can override this cross-validation setting using the CVPartition, Holdout, KFold, or Leaveout name-value argument. You can use only one cross-validation name-value argument at a time to create a cross-validated model.

    Alternatively, cross-validate later by passing Mdl to the crossval function.

    Example: CrossVal="on"

    Data Types: char | string

    Since R2025a

    Cross-validation partition, specified as a cvpartition object that specifies the type of cross-validation and the indexing for the training and validation sets.

    To create a cross-validated model, you can specify only one of these four name-value arguments: CVPartition, Holdout, KFold, or Leaveout.

    Example: Suppose you create a random partition for 5-fold cross-validation on 500 observations by using cvp = cvpartition(500,KFold=5). Then, you can specify the cross-validation partition by setting CVPartition=cvp.

    Since R2025a

    Fraction of the data used for holdout validation, specified as a scalar value in the range (0,1). If you specify Holdout=p, then the software completes these steps:

    1. Randomly select and reserve p*100% of the data as validation data, and train the model using the rest of the data.

    2. Store the compact trained model in the Trained property of the cross-validated model.

    To create a cross-validated model, you can specify only one of these four name-value arguments: CVPartition, Holdout, KFold, or Leaveout.

    Example: Holdout=0.1

    Data Types: double | single

    Since R2025a

    Number of folds to use in the cross-validated model, specified as a positive integer value greater than 1. If you specify KFold=k, then the software completes these steps:

    1. Randomly partition the data into k sets.

    2. For each set, reserve the set as validation data, and train the model using the other k – 1 sets.

    3. Store the k compact trained models in a k-by-1 cell vector in the Trained property of the cross-validated model.

    To create a cross-validated model, you can specify only one of these four name-value arguments: CVPartition, Holdout, KFold, or Leaveout.

    Example: KFold=5

    Data Types: single | double

    Since R2025a

    Leave-one-out cross-validation flag, specified as "on" or "off". If you specify Leaveout="on", then for each of the n observations (where n is the number of observations, excluding missing observations, specified in the NumObservations property of the model), the software completes these steps:

    1. Reserve the one observation as validation data, and train the model using the other n – 1 observations.

    2. Store the n compact trained models in an n-by-1 cell vector in the Trained property of the cross-validated model.

    To create a cross-validated model, you can specify only one of these four name-value arguments: CVPartition, Holdout, KFold, or Leaveout.

    Example: Leaveout="on"

    Data Types: char | string

    Note

    You cannot use any cross-validation name-value argument together with the OptimizeHyperparameters name-value argument. You can modify the cross-validation for OptimizeHyperparameters only by using the HyperparameterOptimizationOptions name-value argument.

    Hyperparameter Optimization

    expand all

    Since R2025a

    Parameters to optimize, specified as one of the following:

    • "none" — Do not optimize.

    • "auto" — Use ["Activations","Lambda","LayerSizes","Standardize"].

    • "all" — Optimize all eligible parameters.

    • String array or cell array of eligible parameter names.

    • Vector of optimizableVariable objects, typically the output of hyperparameters.

    You can optimize hyperparameters only when creating a quantile regression model with one quantile (that is, the Quantiles name-value argument has one element).

    The optimization attempts to minimize the cross-validation loss (error) for fitrqnet by varying the parameters. To control the cross-validation type and other aspects of the optimization, use the HyperparameterOptimizationOptions name-value argument. When you use HyperparameterOptimizationOptions, you can use the (compact) model size instead of the cross-validation loss as the optimization objective by setting the ConstraintType and ConstraintBounds options.

    Note

    The values of OptimizeHyperparameters override any values you specify using other name-value arguments. For example, setting OptimizeHyperparameters to "auto" causes fitrqnet to optimize hyperparameters corresponding to the "auto" option and to ignore any specified values for the hyperparameters.

    The eligible parameters for fitrqnet are:

    • Activationsfitrqnet optimizes Activations over the set ["relu","tanh","sigmoid","none"].

    • Lambdafitrqnet optimizes Lambda over log-scaled values in the range [1e-5/NumObservations,1e5/NumObservations].

    • LayerBiasesInitializerfitrqnet optimizes LayerBiasesInitializer over the two values ["zeros","ones"].

    • LayerWeightsInitializerfitrqnet optimizes LayerWeightsInitializer over the two values ["glorot","he"].

    • LayerSizesfitrqnet optimizes over the values 1, 2, and 3 representing the number of fully connected layers, excluding the final fully connected layer. fitrqnet optimizes each fully connected layer separately over 1 through 300 sizes in the layer, sampled on a logarithmic scale.

      Note

      When you use the LayerSizes argument, the iterative display shows the size of each relevant layer. For example, if the current number of fully connected layers is 3, and the three layers are of size 10, 79, and 44 (respectively), the iterative display shows LayerSizes for that iteration as [10 79 44].

      Note

      To access up to five fully connected layers or a different range of sizes in a layer, use hyperparameters to select the optimizable parameters and ranges.

    • Standardizefitrqnet optimizes Standardize over the two values [true,false].

    Set nondefault parameters by passing a vector of optimizableVariable objects that have nondefault values. For example, this code sets the range of NumLayers to [1 5] and optimizes Layer_4_Size and Layer_5_Size:

    load carsmall
    params = hyperparameters("fitrqnet",[Horsepower,Weight],MPG);
    params(1).Range = [1 5];
    params(10).Optimize = true;
    params(11).Optimize = true;

    Pass params as the value of OptimizeHyperparameters.

    By default, the iterative display appears at the command line, and plots appear according to the number of hyperparameters in the optimization. For the optimization and plots, the objective function is log(1 + cross-validation loss). To control the iterative display, set the Verbose option of the HyperparameterOptimizationOptions name-value argument. To control the plots, set the ShowPlots option of the HyperparameterOptimizationOptions name-value argument.

    Example: OptimizeHyperparameters="auto"

    Since R2025a

    Options for optimization, specified as a HyperparameterOptimizationOptions object or a structure. This argument modifies the effect of the OptimizeHyperparameters name-value argument. If you specify HyperparameterOptimizationOptions, you must also specify OptimizeHyperparameters. All the options listed in the following table are optional. However, you must set ConstraintBounds and ConstraintType to return AggregateOptimizationResults. The options that you can set in a structure are the same as those in the HyperparameterOptimizationOptions object.

    OptionValuesDefault
    Optimizer
    • "bayesopt" — Use Bayesian optimization. Internally, this setting calls bayesopt.

    • "gridsearch" — Use grid search with NumGridDivisions values per dimension. "gridsearch" searches in a random order, using uniform sampling without replacement from the grid. After optimization, you can get a table in grid order by using the command sortrows(Mdl.HyperparameterOptimizationResults).

    • "randomsearch" — Search at random among MaxObjectiveEvaluations points.

    "bayesopt"
    ConstraintBounds

    Constraint bounds for N optimization problems, specified as an N-by-2 numeric matrix or []. The columns of ConstraintBounds contain the lower and upper bound values of the optimization problems. If you specify ConstraintBounds as a numeric vector, the software assigns the values to the second column of ConstraintBounds, and zeros to the first column. If you specify ConstraintBounds, you must also specify ConstraintType.

    []
    ConstraintTarget

    Constraint target for the optimization problems, specified as "matlab" or "coder". If ConstraintBounds and ConstraintType are [] and you set ConstraintTarget, then the software sets ConstraintTarget to []. The values of ConstraintTarget and ConstraintType determine the objective and constraint functions. For more information, see HyperparameterOptimizationOptions.

    If you specify ConstraintBounds and ConstraintType, then the default value is "matlab". Otherwise, the default value is [].
    ConstraintType

    Constraint type for the optimization problems, specified as "size" or "loss". If you specify ConstraintType, you must also specify ConstraintBounds. The values of ConstraintTarget and ConstraintType determine the objective and constraint functions. For more information, see HyperparameterOptimizationOptions.

    []
    AcquisitionFunctionName

    Type of acquisition function:

    • "expected-improvement-per-second-plus"

    • "expected-improvement"

    • "expected-improvement-plus"

    • "expected-improvement-per-second"

    • "lower-confidence-bound"

    • "probability-of-improvement"

    Acquisition functions whose names include per-second do not yield reproducible results, because the optimization depends on the run time of the objective function. Acquisition functions whose names include plus modify their behavior when they overexploit an area. For more details, see Acquisition Function Types.

    "expected-improvement-per-second-plus"
    MaxObjectiveEvaluationsMaximum number of objective function evaluations. If you specify multiple optimization problems using ConstraintBounds, the value of MaxObjectiveEvaluations applies to each optimization problem individually.30 for "bayesopt" and "randomsearch", and the entire grid for "gridsearch"
    MaxTime

    Time limit for the optimization, specified as a nonnegative real scalar. The time limit is in seconds, as measured by tic and toc. The software performs at least one optimization iteration, regardless of the value of MaxTime. The run time can exceed MaxTime because MaxTime does not interrupt function evaluations. If you specify multiple optimization problems using ConstraintBounds, the time limit applies to each optimization problem individually.

    Inf
    NumGridDivisionsFor Optimizer="gridsearch", the number of values in each dimension. The value can be a vector of positive integers giving the number of values for each dimension, or a scalar that applies to all dimensions. The software ignores this option for categorical variables.10
    ShowPlotsLogical value indicating whether to show plots of the optimization progress. If this option is true, the software plots the best observed objective function value against the iteration number. If you use Bayesian optimization (Optimizer="bayesopt"), the software also plots the best estimated objective function value. The best observed objective function values and best estimated objective function values correspond to the values in the BestSoFar (observed) and BestSoFar (estim.) columns of the iterative display, respectively. You can find these values in the properties ObjectiveMinimumTrace and EstimatedObjectiveMinimumTrace of Mdl.HyperparameterOptimizationResults. If the problem includes one or two optimization parameters for Bayesian optimization, then ShowPlots also plots a model of the objective function against the parameters.true
    SaveIntermediateResultsLogical value indicating whether to save the optimization results. If this option is true, the software overwrites a workspace variable named "BayesoptResults" at each iteration. The variable is a BayesianOptimization object. If you specify multiple optimization problems using ConstraintBounds, the workspace variable is an AggregateBayesianOptimization object named "AggregateBayesoptResults".false
    Verbose

    Display level at the command line:

    • 0 — No iterative display

    • 1 — Iterative display

    • 2 — Iterative display with additional information

    For details, see the bayesopt Verbose name-value argument and the example Optimize Classifier Fit Using Bayesian Optimization.

    1
    UseParallelLogical value indicating whether to run the Bayesian optimization in parallel, which requires Parallel Computing Toolbox™. Due to the nonreproducibility of parallel timing, parallel Bayesian optimization does not necessarily yield reproducible results. For details, see Parallel Bayesian Optimization.false
    Repartition

    Logical value indicating whether to repartition the cross-validation at every iteration. If this option is false, the optimizer uses a single partition for the optimization.

    A value of true usually gives the most robust results because this setting takes partitioning noise into account. However, for optimal results, true requires at least twice as many function evaluations.

    false
    Specify only one of the following three options.
    CVPartitioncvpartition object created by cvpartitionKFold=5 if you do not specify a cross-validation option
    HoldoutScalar in the range (0,1) representing the holdout fraction
    KFoldInteger greater than 1

    Example: HyperparameterOptimizationOptions=struct(UseParallel=true)

    Output Arguments

    collapse all

    Trained quantile neural network model, returned as a RegressionQuantileNeuralNetwork object, a RegressionPartitionedQuantileModel object, or a cell array of model objects.

    • If you set any of the name-value arguments CrossVal, CVPartition, Holdout, KFold, or Leaveout, then Mdl is a RegressionPartitionedQuantileModel object.

    • If you specify OptimizeHyperparameters and set the ConstraintType and ConstraintBounds options of HyperparameterOptimizationOptions, then Mdl is an N-by-1 cell array of model objects, where N is equal to the number of rows in ConstraintBounds. If none of the optimization problems yields a feasible model, then each cell array value is [].

    • Otherwise, Mdl is a RegressionQuantileNeuralNetwork model object.

    To reference properties of a model object, use dot notation.

    Since R2025a

    Aggregate optimization results for multiple optimization problems, returned as an AggregateBayesianOptimization object. To return AggregateOptimizationResults, you must specify OptimizeHyperparameters and HyperparameterOptimizationOptions. You must also specify the ConstraintType and ConstraintBounds options of HyperparameterOptimizationOptions. For an example that shows how to produce this output, see Hyperparameter Optimization with Multiple Constraint Bounds.

    More About

    collapse all

    Tips

    Algorithms

    collapse all

    Extended Capabilities

    expand all

    Version History

    Introduced in R2024b

    expand all