classificationLayer
Classification output layer
Description
A classification layer computes the cross-entropy loss for classification and weighted classification tasks with mutually exclusive classes.
The layer infers the number of classes from the output size of the previous layer. For example, to specify the number of classes K of the network, you can include a fully connected layer with output size K and a softmax layer before the classification layer.
creates a classification
layer.layer
= classificationLayer
sets the optional layer
= classificationLayer(Name,Value
)Name
, ClassWeights
, and
Classes
properties using one or more name-value pairs. For
example, classificationLayer('Name','output')
creates a
classification layer with the name 'output'
.
Examples
Create 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
Create Weighted Classification Layer
Create a weighted classification layer for three classes with names "cat", "dog", and "fish", with weights 0.7, 0.2, and 0.1, respectively.
classes = ["cat" "dog" "fish"]; classWeights = [0.7 0.2 0.1]; layer = classificationLayer( ... 'Classes',classes, ... 'ClassWeights',classWeights)
layer = ClassificationOutputLayer with properties: Name: '' Classes: [cat dog fish] ClassWeights: [3x1 double] OutputSize: 3 Hyperparameters LossFunction: 'crossentropyex'
Include a weighted classification output layer in a Layer array.
numClasses = numel(classes); layers = [ ... imageInputLayer([28 28 1]) convolution2dLayer(5,20) reluLayer maxPooling2dLayer(2,'Stride',2) fullyConnectedLayer(numClasses) softmaxLayer classificationLayer('Classes',classes,'ClassWeights',classWeights)]
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 3 fully connected layer 6 '' Softmax softmax 7 '' Classification Output Class weighted crossentropyex with 'cat' and 2 other classes
Input Arguments
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.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: classificationLayer('Name','output')
creates a
classification layer with the name 'output'
Name
— Layer name
""
(default) | character vector | string scalar
Layer name, specified as a character vector or a string scalar.
For Layer
array input, the trainnet
, trainNetwork
, assembleNetwork
, layerGraph
, and
dlnetwork
functions automatically assign
names to layers with the name ""
.
The classificationLayer
object stores this property as a character vector.
Data Types: char
| string
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 'Classes'
.
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
Output Arguments
layer
— Classification layer
ClassificationOutputLayer
object
Classification layer, returned as a ClassificationOutputLayer
object.
For information on concatenating layers to construct
convolutional neural network architecture, see Layer
.
More About
Classification 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, is the weight for class i, is the indicator that the nth sample belongs to the ith class, and is the output for sample n for class i, which in this case, is the value from the softmax function. In other words, is the probability that the network associates the nth input with class i.
References
[1] Bishop, C. M. Pattern Recognition and Machine Learning. Springer, New York, NY, 2006.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
The code generator represents characters in an 8-bit ASCII codeset that the locale setting determines. Therefore, the use of non-ASCII characters in class names, layer names, layer description, or network names might result in errors. For more information, see Encoding of Characters in Code Generation (MATLAB Coder).
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
To generate CUDA® or C++ code by using GPU Coder™, you must first construct and train a deep neural network. Once the network is trained and evaluated, you can configure the code generator to generate code and deploy the convolutional neural network on platforms that use NVIDIA® or ARM® GPU processors. For more information, see Deep Learning with GPU Coder (GPU Coder).
For this layer, you can generate code that takes advantage of the NVIDIA CUDA deep neural network library (cuDNN), NVIDIA TensorRT™ high performance inference library, or the ARM
Compute Library
for Mali GPU.The code generator represents characters in an 8-bit ASCII codeset that the locale setting determines. Therefore, the use of non-ASCII characters in class names, layer names, layer description, or network names might result in errors. For more information, see Encoding of Characters in Code Generation (MATLAB Coder).
Version History
Introduced in R2016a
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
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)