ClassificationOutputLayer
(Not recommended) Classification output layer
ClassificationOutputLayer
objects are recommended. Use the trainnet
function and set the loss function to "crossentropy"
instead. For
more information, see Version
History.
Description
A classification layer computes the cross-entropy loss for classification and weighted classification tasks with mutually exclusive classes.
Creation
Create a classification layer using classificationLayer
.
Properties
Classification Output
ClassWeights
— Class weights for weighted cross-entropy loss
'none'
(default) | vector of positive numbers
Class weights for weighted cross-entropy loss, specified as a vector
of positive numbers or 'none'
.
For vector class weights, each element represents the weight for the
corresponding class in the Classes
property. To specify a vector of class weights, you must also specify
the classes using the Classes
option.
If the ClassWeights
property is
'none'
, then the layer applies unweighted
cross-entropy loss.
Classes
— Classes of the output layer
"auto"
(default) | categorical vector | string array | cell array of character vectors
Classes of the output layer, specified as a categorical vector, string array, cell array of character vectors, or "auto"
. If Classes
is "auto"
, then the software automatically sets the classes at training time. If you specify the string array or cell array of character vectors str
, then the software sets the classes of the output layer to categorical(str,str)
.
Data Types: char
| categorical
| string
| cell
OutputSize
— Size of the output
'auto'
(default) | positive integer
This property is read-only.
Size of the output, specified as a positive integer. This value is
the number of labels in the data. Before the training, the output size
is set to 'auto'
.
LossFunction
— Loss function for training
'crossentropyex'
This property is read-only.
Loss function for training, specified as
'crossentropyex'
, which stands for
Cross Entropy Function for k Mutually
Exclusive Classes.
Layer
Name
— Layer name
""
(default) | character vector | string scalar
Layer name, specified as a character vector or a string scalar. For Layer
array input, the trainNetwork
function automatically assigns names to layers with the name ""
.
The ClassificationOutputLayer
object stores this property as a character vector.
Data Types: char
| string
NumInputs
— Number of inputs
1
(default)
Number of inputs to the layer, returned as 1
. This layer accepts a single input only.
Data Types: double
InputNames
— Input names
{'in'}
(default)
Input names, returned as {'in'}
. This layer accepts a single input only.
Data Types: cell
NumOutputs
— Number of outputs
0
(default)
Number of outputs of the layer, returned as 0
. This layer has no outputs.
Data Types: double
OutputNames
— Output names
{}
(default)
Output names of the layer, returned as {}
. This layer has no outputs.
Data Types: cell
Examples
Create Classification Layer
Create a classification layer.
Create a classification layer with the name 'output'
.
layer = classificationLayer('Name','output')
layer = ClassificationOutputLayer with properties: Name: 'output' Classes: 'auto' ClassWeights: 'none' OutputSize: 'auto' Hyperparameters LossFunction: 'crossentropyex'
Include a classification output layer in a Layer
array.
layers = [ ... imageInputLayer([28 28 1]) convolution2dLayer(5,20) reluLayer maxPooling2dLayer(2,'Stride',2) fullyConnectedLayer(10) softmaxLayer classificationLayer]
layers = 7x1 Layer array with layers: 1 '' Image Input 28x28x1 images with 'zerocenter' normalization 2 '' 2-D Convolution 20 5x5 convolutions with stride [1 1] and padding [0 0 0 0] 3 '' ReLU ReLU 4 '' 2-D Max Pooling 2x2 max pooling with stride [2 2] and padding [0 0 0 0] 5 '' Fully Connected 10 fully connected layer 6 '' Softmax softmax 7 '' Classification Output crossentropyex
More About
Classification Output Layer
A classification layer computes the cross-entropy loss for classification and weighted classification tasks with mutually exclusive classes.
For typical classification networks, the classification layer usually
follows a softmax layer. In the classification layer, trainNetwork
takes the values from the softmax function and assigns each input to one of the
K mutually exclusive classes using the cross entropy function for a
1-of-K coding scheme [1]:
where N is the number of samples, K is the number of classes, wi is the weight for class i, tni is the indicator that sample n belongs to class i, and yni is the output for sample n for class i, which in this case, is the value from the softmax function. In other words, yni is the probability that the network associates observation n with class i.
References
[1] Bishop, C. M. Pattern Recognition and Machine Learning. Springer, New York, NY, 2006.
Version History
Introduced in R2016aR2024a: Not recommended
Starting in R2024a, ClassificationOutputLayer
objects are not recommended,
use the trainnet
and
set the loss function to "crossentropy"
instead.
There are no plans to remove support for ClassificationOutputLayer
objects. However, the trainnet
function has these advantages and is
recommended instead:
trainnet
supportsdlnetwork
objects, which support a wider range of network architectures that you can create or import from external platforms.trainnet
enables you to easily specify loss functions. You can select from built-in loss functions or specify a custom loss function.trainnet
outputs adlnetwork
object, which is a unified data type that supports network building, prediction, built-in training, visualization, compression, verification, and custom training loops.trainnet
is typically faster thantrainNetwork
.
This table shows some typical usages of the trainNetwork
function with ClassificationOutputLayer
objects and how to update your code to use the trainnet
function instead.
Not Recommended | Recommended |
---|---|
net = trainNetwork(data,layers,options) , where layers contains a ClassificationOutputLayer object. | net = trainnet(data,layers,"crossentropy",options); layers specifies same network without a ClassificationOutputLayer object. |
net = trainNetwork(data,layers,options) , where layers contains a ClassificationOutputLayer object with ClassWeights set to a numeric vector. | lossFcn = @(Y,T) crossentropy(Y,T,Weights=weights);
net = trainnet(data,layers,"crossentropy",options); weights specifies the class weights and layers specifies same network without a ClassificationOutputLayer object. |
R2018b: ClassNames
property will be removed
ClassNames
will be removed. Use Classes
instead. To
update your code, replace all instances of ClassNames
with
Classes
. There are some differences between the properties that
require additional updates to your code.
The ClassNames
property of the output layer is a cell array of character
vectors. The Classes
property is a categorical array. To use the value
of Classes
with functions that require cell array input, convert the
classes using the cellstr
function.
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.
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)