classificationLayer
分类输出层
说明
分类层计算具有互斥类的分类和加权分类任务的交叉熵损失。
该层根据前一层的输出大小来推断类的数量。例如,要指定网络的类数 K,您可以在分类层之前包含输出大小为 K 的一个全连接层和一个 softmax 层。
创建一个分类层。layer
= classificationLayer
使用一个或多个名称-值对组设置可选的 layer
= classificationLayer(Name,Value
)Name
、ClassWeights
和 Classes
属性。例如,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'
的分类层
Name
— 层名称
''
(默认) | 字符向量 | 字符串标量
层名称,指定为字符向量或字符串标量。对于 Layer
数组输入,trainNetwork
、assembleNetwork
、layerGraph
和 dlnetwork
函数会自动为层指定名称 ''
。
数据类型: char
| string
ClassWeights
— 加权交叉熵损失的类权重
'none'
(默认) | 正数向量
加权交叉熵损失的类权重,指定为正数向量或 'none'
。
对于向量类权重,每个元素都表示 Classes
属性中对应类的权重。要指定类权重的向量,还必须使用 'Classes'
指定类。
如果 ClassWeights
属性为 'none'
,则该层应用未加权交叉熵损失。
Classes
— 输出层的类
"auto"
(默认) | 分类向量 | 字符串数组 | 字符向量元胞数组
输出层的类,指定为分类向量、字符串数组、字符向量元胞数组或 "auto"
。如果 Classes
是 "auto"
,则软件会在训练时自动设置类。如果您指定字符串数组或字符向量元胞数组 str
,则软件会将输出层的类设置为 categorical(str,str)
。
数据类型: char
| categorical
| string
| cell
输出参数
layer
— 分类层
ClassificationOutputLayer
对象
分类层,以 ClassificationOutputLayer
对象形式返回。
有关串联层以构造卷积神经网络架构的信息,请参阅 Layer
。
详细信息
参考
[1] Bishop, C. M. Pattern Recognition and Machine Learning. Springer, New York, NY, 2006.
扩展功能
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
GPU 代码生成
使用 GPU Coder™ 为 NVIDIA® GPU 生成 CUDA® 代码。
版本历史记录
在 R2016a 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
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)