Main Content

ClassificationOutputLayer

Classification layer

Description

A classification layer computes the cross-entropy loss for classification and weighted classification tasks with mutually exclusive classes.

Creation

Create a classification layer usingclassificationLayer.

Properties

expand all

Classification Output

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 theClassesproperty. To specify a vector of class weights, you must also specify the classes using'Classes'.

If theClassWeightsproperty is'none', then the layer applies unweighted cross-entropy loss.

Classes of the output layer, specified as a categorical vector, string array, cell array of character vectors, or"auto". IfClassesis"auto", then the software automatically sets the classes at training time. If you specify the string array or cell array of character vectorsstr, then the software sets the classes of the output layer tocategorical(str,str).

Data Types:char|categorical|string|cell

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'.

This property is read-only.

Loss function for training, specified as'crossentropyex', which stands forCross Entropy Function forkMutually Exclusive Classes.

Layer

Layer name, specified as a character vector or a string scalar. ForLayerarray input, thetrainNetwork,assembleNetwork,layerGraph, anddlnetworkfunctions automatically assign names to layers with the name''.

Data Types:char|string

This property is read-only.

Number of inputs of the layer. This layer accepts a single input only.

Data Types:double

This property is read-only.

Input names of the layer. This layer accepts a single input only.

Data Types:cell

Number of outputs of the layer. The layer has no outputs.

Data Types:double

Output names of the layer. The layer has no outputs.

Data Types:cell

Examples

collapse all

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 aLayerarray.

layers = [...imageInputLayer([28 28 1]) convolution2dLayer(5,20) reluLayer maxPooling2dLayer(2,'Stride',2) fullyConnectedLayer(10) softmaxLayer classificationLayer]
层x1 = 7层阵列层:1”的形象nput 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 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)]
层x1 = 7层阵列层:1”的形象nput 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

More About

expand all

References

[1] Bishop, C. M.Pattern Recognition and Machine Learning. Springer, New York, NY, 2006.

Version History

Introduced in R2016a

expand all