Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

classificationLayer

分类输出层

说明

分类层计算具有互斥类的分类和加权分类任务的交叉熵损失。

该层根据前一层的输出大小来推断类的数量。例如,要指定网络的类数 K,您可以在分类层之前包含输出大小为 K 的一个全连接层和一个 softmax 层。

layer = classificationLayer 创建一个分类层。

示例

layer = classificationLayer(Name,Value) 使用一个或多个名称-值对组设置可选的 NameClassWeightsClasses 属性。例如,classificationLayer('Name','output') 创建一个名为 'output' 的分类层。

示例

全部折叠

创建一个名为 'output' 的分类层。

layer = classificationLayer('Name','output')
layer = 
  ClassificationOutputLayer with properties:

            Name: 'output'
         Classes: 'auto'
    ClassWeights: 'none'
      OutputSize: 'auto'

   Hyperparameters
    LossFunction: 'crossentropyex'

Layer 数组中包含一个分类输出层。

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

针对三个名称分别为“cat”、“dog”和“fish”且权重分别为 0.7、0.2 和 0.1 的类,创建一个加权分类层。

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'

在层数组中包含一个加权分类输出层。

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

输入参数

全部折叠

名称-值参数

将可选的参数对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参数名称,Value 是对应的值。名称-值参数必须出现在其他参数后,但对各个参数对组的顺序没有要求。

如果使用的是 R2021a 之前的版本,请使用逗号分隔每个名称和值,并用引号将 Name 引起来。

示例: classificationLayer('Name','output') 创建一个名为 'output' 的分类层

层名称,指定为字符向量或字符串标量。对于 Layer 数组输入,trainnettrainNetworkassembleNetworklayerGraphdlnetwork 函数会自动为层指定名称 ""

classificationLayer 对象将此属性存储为字符向量。

数据类型: char | string

加权交叉熵损失的类权重,指定为正数向量或 'none'

对于向量类权重,每个元素都表示 Classes 属性中对应类的权重。要指定类权重的向量,还必须使用 'Classes' 指定类。

如果 ClassWeights 属性为 'none',则该层应用未加权交叉熵损失。

输出层的类,指定为分类向量、字符串数组、字符向量元胞数组或 "auto"。如果 Classes"auto",则软件会在训练时自动设置类。如果您指定字符串数组或字符向量元胞数组 str,则软件会将输出层的类设置为 categorical(str,str)

数据类型: char | categorical | string | cell

输出参数

全部折叠

分类层,以 ClassificationOutputLayer 对象形式返回。

有关串联层以构造卷积神经网络架构的信息,请参阅 Layer

详细信息

全部折叠

分类层

分类层计算具有互斥类的分类和加权分类任务的交叉熵损失。

对于典型的分类网络,分类层通常在 softmax 层之后。在分类层中,trainNetwork 接受来自 softmax 函数的值,使用交叉熵函数将每个输入赋给 K 个互斥类之一以实现 1-of-K 编码方案 [1]

loss=1Nn=1Ni=1Kwitnilnyni,

其中 N 是样本数,K 是类数,wi 是类 i 的权重,tni 指示第 n 个样本属于第 i 个类,yni 是类 i 的样本 n 的输出,在本例中它是来自 softmax 函数的值。换句话说,yni 是网络将第 n 个输入与类 i 相关联的概率。

参考

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

扩展功能

版本历史记录

在 R2016a 中推出