Classification layer
A classification layer computes the cross entropy loss for multi-class classification problems with mutually exclusive classes.
Create a classification layer using classificationLayer
.
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)
. The default value is
'auto'
.
Data Types: char
| categorical
| string
| cell
OutputSize
— Size of the output'auto'
(default) | positive integerThis 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.
Name
— Layer name''
(default) | character vector | string scalar
Layer name, specified as a character vector or a string scalar.
To include a layer in a layer graph, you must specify a nonempty unique layer name. If you train
a series network with the layer and Name
is set to ''
,
then the software automatically assigns a name to the layer at training time.
Data Types: char
| string
NumInputs
— Number of inputsNumber of inputs of the layer. This layer accepts a single input only.
Data Types: double
InputNames
— Input names{'in'}
(default)Input names of the layer. This layer accepts a single input only.
Data Types: cell
NumOutputs
— Number of outputsNumber of outputs of the layer. The layer has no outputs.
Data Types: double
OutputNames
— Output names{}
(default)Output names of the layer. The layer has no outputs.
Data Types: cell
Create a classification layer with the name 'output'
.
layer = classificationLayer('Name','output')
layer = ClassificationOutputLayer with properties: Name: 'output' Classes: 'auto' 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 '' Convolution 20 5x5 convolutions with stride [1 1] and padding [0 0 0 0] 3 '' ReLU ReLU 4 '' 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
A classification layer computes the cross entropy loss for multi-class classification problems with mutually exclusive classes.
For typical classification networks, the classification layer must follow
the 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 indicator that the ith sample belongs to the jth class, and is the output for sample i for class j, which in this case, is the value from the softmax function. That is, it is the probability that the network associates the ith input with class j.
ClassNames
property will be removedNot recommended starting in R2018b
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.
[1] Bishop, C. M. Pattern Recognition and Machine Learning. Springer, New York, NY, 2006.
You have a modified version of this example. Do you want to open this example with your edits?