Main Content

classificationLayer

(Not recommended) Classification output layer

classificationLayer is not 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.

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.

layer = classificationLayer creates a classification layer.

example

layer = classificationLayer(Name,Value) sets the optional 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

collapse all

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

Name-Value Arguments

collapse all

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'

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

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

collapse all

Classification layer, returned as a ClassificationOutputLayer object.

For information on concatenating layers to construct convolutional neural network architecture, see Layer.

More About

collapse all

References

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

Extended Capabilities

expand all

Version History

Introduced in R2016a

collapse all