classificationNeuralNetworkComponent
Description
classificationNeuralNetworkComponent is a pipeline component that creates a
feedforward, fully connected neural network for classification. The pipeline component uses
the functionality of the fitcnet function
during the learn phase to train a neural network model. The component uses the functionality
of the predict and
loss functions
during the run phase to evaluate the model on new data.
Creation
Syntax
Description
creates a pipeline component for a neural network model for classification.component = classificationNeuralNetworkComponent
sets writable Properties using one or more
name-value arguments. For example, you can specify the activation functions,
regularization term strength, and size of fully connected layers.component = classificationNeuralNetworkComponent(Name=Value)
Properties
Structural Parameters
The software sets structural parameters when you create the component. You cannot modify structural parameters after creating the component.
This property is read-only after the component is created.
Observation weights flag, specified as 0 (false)
or 1 (true). If UseWeights is
true, the component adds a third input "Weights" to the
Inputs component property, and a third input tag
3 to the InputTags component
property.
Example: c = classificationNeuralNetworkComponent(UseWeights=1)
Data Types: logical
Learn Parameters
The software sets learn parameters when you create the component. You can modify learn
parameters using dot notation any time before you use the learn object
function. Any unset learn parameters use the corresponding default values.
Activation functions for the fully connected layers of the neural network model, specified as one or more of the values in this table.
| Value | Description |
|---|---|
"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, |
"tanh" | Hyperbolic tangent (tanh) function — Applies the |
"sigmoid" | Sigmoid function — Performs the following operation on each input element: |
"none" | Identity function — Returns each input element without performing any transformation, that is, f(x) = x |
If Activations is one of the table values, the component uses
the specified activation function for each of the fully connected layers of the model,
excluding the final fully connected layer. The activation function for the final fully
connected layer is always softmax. For more information, see Neural Network Structure.
If Activations is a string array or cell array of character
vectors containing multiple table values, the component uses the
ith element of Activations for the
ith fully connected layer of the model.
Example: c =
classificationNeuralNetworkComponent(Activations="sigmoid")
Example: c.Activations = ["relu","tanh"]
Data Types: char | string | cell
Misclassification cost, specified as a square matrix or a structure.
If
Costis a square matrix,Cost(i,j)is the cost of classifying a point into classjif its true class isi.If
Costis a structureS, it has two fields:S.ClassificationCosts, which contains the cost matrix; andS.ClassNames, which contains the group names and defines the class order of the rows and columns of the cost matrix.
The default is Cost(i,j)=1 if i~=j, and
Cost(i,j)=0 if i=j.
Example: c = classificationNeuralNetworkComponent(Cost=[0 1; 2
0])
Example: c.Cost = [0 2; 1 0]
Data Types: single | double | struct
Relative gradient tolerance, specified as a nonnegative scalar.
Let be the loss function at training iteration t, be the gradient of the loss function with respect to the weights and biases at iteration t, and be the gradient of the loss function at an initial point. If , where , the training process terminates.
Example: c =
classificationNeuralNetworkComponent(GradientTolerance=1e-5)
Example: c.GradientTolerance = 1e-7
Data Types: single | double
Initial step size, specified as a positive scalar or "auto". By
default, the component does not use the initial step size to determine the initial
Hessian approximation used to train the model. However, if you specify an initial step
size , then the initial inverse-Hessian approximation is . is the initial gradient vector, and is the identity matrix.
If you specify "auto", the component determines an initial step
size by using . is the initial step vector, and is the vector of unconstrained initial weights and biases.
Example: c =
classificationNeuralNetworkComponent(InitialStepSize="auto")
Example: c.InitialStepSize = 0.2
Data Types: single | double | char | string
Maximum number of training iterations, specified as a positive integer scalar.
When the component completes IterationLimit training
iterations, it updates TrainedModel regardless of whether the training routine successfully
converges.
Example: c =
classificationNeuralNetworkComponent(IterationLimit=1e8)
Example: c.IterationLimit = 1e5
Data Types: single | double
Regularization term strength, specified as a nonnegative scalar. The component forms the objective function for minimization from the cross-entropy loss function and the ridge (L2) penalty term.
Example: c =
classificationNeuralNetworkComponent(Lambda=1e-4)
Example: c.Lambda = 1e-2
Data Types: single | double
Type of initial fully connected layer biases, specified as
"zeros" or "ones".
If you specify "zeros", each fully connected layer has an
initial bias value of 0. If you specify "ones",
each fully connected layer has an initial bias value of 1.
Example: c =
classificationNeuralNetworkComponent(LayerBiasesInitializer="ones")
Example: c.LayerBiasesInitializer = "zeros"
Data Types: char | string
Size of the fully connected layers in the neural network model, specified as a
positive integer vector. The ith element of
LayerSizes is the number of outputs in the
ith fully connected layer of the neural network model.
LayerSizes does not include the size of the final fully
connected layer, which uses the softmax activation function. For more information, see
Neural Network Structure.
Example: c = classificationNeuralNetworkComponent(LayerSizes=[100 25
10])
Example: c.LayerSizes = [50 20 10]
Data Types: single | double
Function used to initialize the fully connected layer weights, specified as
"glorot" or "he".
If you specify
"glorot", the component initializes the weights using 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 variance2/(I+O), whereIis the input size andOis the output size for the layer.If you specify
"he", the component initializes the weights using the He initializer [2]. For each layer, the He initializer samples from a normal distribution with zero mean and variance2/I, whereIis the input size for the layer.
Example: c =
classificationNeuralNetworkComponent(LayerWeightsInitializer="he")
Example: c.LayerWeightsInitializer = "glorot"
Data Types: char | string
Loss tolerance, specified as a nonnegative scalar.
If the function loss at an iteration is smaller than
LossTolerance, the training process terminates.
Example: c =
classificationNeuralNetworkComponent(LossTolerance=1e-8)
Example: c.LossTolerance = 1e-5
Data Types: single | double
Prior probabilities for each class, specified as a value in this table.
| Value | Description |
|---|---|
"empirical" | The class prior probabilities are the class relative frequencies. The class relative
frequencies are determined by the second data argument of
learn. |
"uniform" | All class prior probabilities are equal to 1/K, where K is the number of classes. |
| numeric vector | A numeric vector with one value for each class. Each element is a class prior probability.
The component normalizes the elements such that they sum to
1. |
| structure | A structure
|
If you set UseWeights to true, the component
renormalizes the weights to add up to the value of the prior probability in
the respective class.
Example: c = classificationNeuralNetworkComponent(Prior="uniform")
Example: c.Prior = "empirical"
Data Types: single | double | char | string | struct
Flag to standardize the predictor data, specified as a numeric or logical
0 (false) or 1
(true). If Standardize is
true, the component centers and scales each numeric predictor
variable by the corresponding column mean and standard deviation. The component does
not standardize categorical predictors.
Example: c =
classificationNeuralNetworkComponent(Standardize=true)
Example: c.Standardize = 0
Data Types: single | double | logical
Step size tolerance, specified as a nonnegative scalar.
If the step size at an iteration is smaller than
StepTolerance, the training process terminates.
Example: c =
classificationNeuralNetworkComponent(StepTolerance=1e-4)
Example: c.StepTolerance = 1e-8
Data Types: single | double
Run Parameters
The software sets run parameters when you create the component. You can modify the run parameters using dot notation at any time. Any unset run parameters use the corresponding default values.
Loss function, specified as a built-in loss function name or a function handle.
This table lists the available built-in loss functions.
| Value | Description |
|---|---|
"binodeviance" | Binomial deviance |
"classifcost" | Observed misclassification cost |
"classiferror" | Misclassified rate in decimal |
"crossentropy" | Cross-entropy loss |
"exponential" | Exponential loss |
"hinge" | Hinge loss |
"logit" | Logistic loss |
"mincost" | Minimal expected misclassification cost (for classification scores that are posterior probabilities) |
"quadratic" | Quadratic loss |
To specify a custom loss function, use function handle notation. For
more information on custom loss functions, see LossFun.
Example: c =
classificationNeuralNetworkComponent(LossFun="classifcost")
Example: c.LossFun = "hinge"
Data Types: char | string | function_handle
Score transformation, specified as a built-in function name or a function handle.
This table summarizes the available built-in score transform functions.
| Value | Description |
|---|---|
"doublelogit" | 1/(1 + e–2x) |
"invlogit" | log(x / (1 – x)) |
"ismax" | Sets the score for the class with the largest score to 1, and sets the scores for all other classes to 0 |
"logit" | 1/(1 + e–x) |
"none" or "identity" | x (no transformation) |
"sign" | –1 for x < 0 0 for x = 0 1 for x > 0 |
"symmetric" | 2x – 1 |
"symmetricismax" | Sets the score for the class with the largest score to 1, and sets the scores for all other classes to –1 |
"symmetriclogit" | 2/(1 + e–x) – 1 |
To specify a custom score transform function, use function handle notation. The function must accept a matrix containing the original scores and return a matrix of the same size containing the transformed scores.
Example: c = classificationNeuralNetworkComponent(ScoreTransform="logit")
Example: c.ScoreTransform = "symmetric"
Data Types: char | string | function_handle
Component Properties
The software sets component properties when you create the component. You can modify the
component properties (excluding HasLearnables and
HasLearned) using dot notation at any time. You cannot modify the
HasLearnables and HasLearned properties
directly.
Component identifier, specified as a character vector or string scalar.
Example: c =
classificationNeuralNetworkComponent(Name="NeuralNetwork")
Example: c.Name = "NeuralNetworkClassifier"
Data Types: char | string
Names of the input ports, specified as a character vector, string array, or cell
array of character vectors. If UseWeights is true, the component adds the input port
"Weights" to Inputs.
Example: c =
classificationNeuralNetworkComponent(Inputs=["X","Y"])
Example: c.Inputs = ["X1","Y1"]
Data Types: char | string | cell
Names of the output ports, specified as a character vector, string array, or cell array of character vectors.
Example: c =
classificationNeuralNetworkComponent(Outputs=["Class","ClassScore","LossVal"])
Example: c.Outputs = ["X","Y","Z"]
Data Types: char | string | cell
Tags that enable the automatic connection of the component inputs with other
components or pipelines, specified as a nonnegative integer vector. If you specify
InputTags, the number of tags must match the number of inputs
in Inputs. If
UseWeights is true, the component adds a third input tag to
InputTags.
Example: c = classificationNeuralNetworkComponent(InputTags=[0
1])
Example: c.InputTags = [1 0]
Data Types: single | double
Tags that enable the automatic connection of the component outputs with other
components or pipelines, specified as a nonnegative integer vector. If you specify
OutputTags, the number of tags must match the number of outputs
in Outputs.
Example: c = classificationNeuralNetworkComponent(OutputTags=[1 0
4])
Example: c.OutputTags=[1 2 0]
Data Types: single | double
This property is read-only.
Indicator for the learnables, returned as 1
(true). A value of 1 indicates that the
component contains Learnables.
Data Types: logical
This property is read-only.
Indicator showing the learning status of the component, returned as
0 (false) or 1
(true). A value of 1 indicates that the
learn object function has been applied to the component and the
Learnables are nonempty.
Data Types: logical
Learnables
The software sets learnables when you use the learn object
function. You cannot modify learnables directly.
This property is read-only.
Trained model, returned as a CompactClassificationNeuralNetwork model object.
Object Functions
learn | Initialize and evaluate pipeline or component |
run | Execute pipeline or component for inference after learning |
reset | Reset pipeline or component |
series | Connect components in series to create pipeline |
parallel | Connect components or pipelines in parallel to create pipeline |
view | View diagram of pipeline inputs, outputs, components, and connections |
Examples
Create a classificationNeuralNetworkComponent pipeline
component.
component = classificationNeuralNetworkComponent
component =
classificationNeuralNetworkComponent with properties:
Name: "ClassificationNeuralNetwork"
Inputs: ["Predictors" "Response"]
InputTags: [1 2]
Outputs: ["Predictions" "Scores" "Loss"]
OutputTags: [1 0 0]
Learnables (HasLearned = false)
TrainedModel: []
Structural Parameters (locked)
UseWeights: 0
Show all parameters
component is a
classificationNeuralNetworkComponent object that contains one
learnable, TrainedModel. This property remains empty until you pass
data to the component during the learn phase.
To use the sigmoid activation function for the fully connected layers of the neural
network model, set the Activations property of the component to
"sigmoid".
component.Activations = "sigmoid";Load the ionosphere data set and save the data in two
tables.
load ionosphere
X = array2table(X);
Y = array2table(Y);Use the learn object function to train the
classificationNeuralNetworkComponent object using the entire data
set.
component = learn(component,X,Y)
component =
classificationNeuralNetworkComponent with properties:
Name: "NeuralNetwork"
Inputs: ["Predictors" "Response"]
InputTags: [1 2]
Outputs: ["Predictions" "Scores" "Loss"]
OutputTags: [1 0 0]
Learnables (HasLearned = true)
TrainedModel: [1×1 classreg.learning.classif.CompactClassificationNeuralNetwork]
Structural Parameters (locked)
UseWeights: 0
Learn Parameters (locked)
Activations: 'sigmoid'
Show all parameters
Note that the HasLearned property is set to true, which indicates
that the software trained the neural network model TrainedModel. You
can use component to classify new data using the
run object function.
More About
The default neural network classifier has the following layer structure.
| Structure | Description |
|---|---|
|
| Input — This layer corresponds to the predictor data in the first data argument
of the learn or run function. |
First fully connected layer — This layer has 10 outputs by default. You can widen the layer or add more fully connected layers to
the network by specifying the You can find the weights and biases
for this layer in the | |
ReLU activation function — The component applies this activation function to the first fully connected layer. You can change the activation
function by specifying the | |
Final fully connected layer — This layer has K outputs, where K is the number of classes in the response variable. You can find the weights and biases for this layer in the
| |
Softmax function (for both binary and multiclass classification) — The component applies this activation function to the final fully connected layer. The function takes each input xi and returns the following, where K is the number of classes in the response variable: The results correspond to the predicted classification scores (or posterior probabilities). | |
| Output — This layer corresponds to the predicted class labels. |
References
[1] Glorot, Xavier, and Yoshua Bengio. “Understanding the Difficulty of Training Deep Feedforward Neural Networks.” In Proceedings of the 13th International Conference on Artificial Intelligence and Statistics, 2010, pp. 249–256.
[2] He, Kaiming, Xiangyu Zhang, Shaoqing Ren, and Jian Sun. “Delving Deep into Rectifiers: Surpassing Human-Level Performance on Imagenet Classification.” In Proceedings of the IEEE International Conference on Computer Vision, 2015, pp. 1026–1034.
Version History
Introduced in R2026a
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)
