crossentropy
Syntax
Description
The cross-entropy operation computes the cross-entropy loss between network predictions and target values for single-label and multi-label classification tasks.
Thecrossentropy
function computes the cross-entropy loss between predictions and targets represented asdlarray
data.Usingdlarray
objects makes working with high dimensional data easier by allowing you to label the dimensions. For example, you can label which dimensions correspond to spatial, time, channel, and batch dimensions using the"S"
,"T"
,"C"
, and"B"
labels, respectively. For unspecified and other dimensions, use the"U"
label. Fordlarray
object functions that operate over particular dimensions, you can specify the dimension labels by formatting thedlarray
object directly, or by using theDataFormat
option.
Note
To calculate the cross-entropy loss within alayerGraph
object orLayer
一个rray for use with thetrainNetwork
function, useclassificationLayer
.
returns the categorical cross-entropy loss between the formattedloss
= crossentropy(Y
,targets
)dlarray
objectY
containing the predictions and the target valuestargets
for single-label classification tasks. The outputloss
is an unformatteddlarray
scalar.
For unformatted input data, use the'DataFormat'
option.
一个lso specifies the dimension formatloss
= crossentropy(___,'DataFormat',FMT)FMT
whenY
is not a formatteddlarray
.
使用一个或多个名称pai指定选项r arguments in addition to the input arguments in previous syntaxes. For example,loss
= crossentropy(___,Name,Value
)'TargetCategories','independent'
computes the cross-entropy loss for a multi-label classification task.
Examples
Cross-Entropy Loss for Single-Label Classification
Create an array of prediction scores for 12 observations over 10 classes.
numClasses = 10; numObservations = 12; Y = rand(numClasses,numObservations); dlY = dlarray(Y,'CB'); dlY = softmax(dlY);
View the size and format of the prediction scores.
size(dlY)
一个ns =1×210 12
dims(dlY)
一个ns = 'CB'
Create an array of targets encoded as one-hot vectors.
labels = randi(numClasses,[1 numObservations]); targets = onehotencode(labels,1,'ClassNames',1:numClasses);
View the size of the targets.
size(targets)
一个ns =1×210 12
计算之间的叉损失预测ions and the targets.
loss = crossentropy(dlY,targets)
loss = 1x1 dlarray 2.3343
Cross-Entropy Loss for Multi-Label Classification
Create an array of prediction scores for 12 observations over 10 classes.
numClasses = 10; numObservations = 12; Y = rand(numClasses,numObservations); dlY = dlarray(Y,'CB');
View the size and format of the prediction scores.
size(dlY)
一个ns =1×210 12
dims(dlY)
一个ns = 'CB'
Create a random array of targets encoded as a numeric array of zeros and ones. Each observation can have multiple classes.
targets = rand(numClasses,numObservations) > 0.75; targets = single(targets);
View the size of the targets.
size(targets)
一个ns =1×210 12
计算之间的叉损失预测ions and the targets. To specify cross-entropy loss for multi-label classification, set the'TargetCategories'
option to'independent'
.
loss = crossentropy(dlY,targets,'TargetCategories','independent')
loss = 1x1 single dlarray 9.8853
Weighted Cross-Entropy Loss
Create an array of prediction scores for 12 observations over 10 classes.
numClasses = 10; numObservations = 12; Y = rand(numClasses,numObservations); dlY = dlarray(Y,'CB'); dlY = softmax(dlY);
View the size and format of the prediction scores.
size(dlY)
一个ns =1×210 12
dims(dlY)
一个ns = 'CB'
Create an array of targets encoded as one-hot vectors.
labels = randi(numClasses,[1 numObservations]); targets = onehotencode(labels,1,'ClassNames',1:numClasses);
View the size of the targets.
size(targets)
一个ns =1×210 12
Compute the weighted cross-entropy loss between the predictions and the targets using a vector class weights. Specify a weights format of'UC'
(unspecified, channel) using the'WeightsFormat'
option.
weights = rand(1,numClasses); loss = crossentropy(dlY,targets,weights,'WeightsFormat','UC')
loss = 1x1 dlarray 1.1261
Input Arguments
Y
—Predictions
dlarray
|numeric array
Predictions, specified as a formatteddlarray
, an unformatteddlarray
, or a numeric array. WhenY
is not a formatteddlarray
, you must specify the dimension format using theDataFormat
option.
IfY
is a numeric array,targets
must be adlarray
.
targets
—Target classification labels
dlarray
|numeric array
Target classification labels, specified as a formatted or unformatteddlarray
or a numeric array.
Specify the targets as an array containing one-hot encoded labels with the same size and format asY
. For example, ifY
is anumObservations
-by-numClasses
一个rray, thentargets(n,i)
= 1 if observationn
belongs to classi
targets(n,i)
= 0 otherwise.
Iftargets
is a formatteddlarray
, then its format must be the same as the format ofY
, or the same asDataFormat
ifY
is unformatted.
Iftargets
is an unformatteddlarray
or a numeric array, then the function applies the format ofY
or the value ofDataFormat
totargets
.
Tip
Formatteddlarray
objects automatically permute the dimensions of the underlying data to have order"S"
(spatial),"C"
(channel),"B"
(batch),"T"
(time), then"U"
(unspecified). To ensure that the dimensions ofY
一个ndtargets
一个re consistent, whenY
is a formatteddlarray
, also specifytargets
一个s a formatteddlarray
.
weights
—Weights
dlarray
|numeric array
Weights, specified as adlarray
or a numeric array.
To specify class weights, specify a vector with a'C'
(channel) dimension with size matching the'C'
(channel) dimension of theY
. Specify the'C'
(channel) dimension of the class weights by using a formatteddlarray
object or by using the'WeightsFormat'
option.
To specify observation weights, specify a vector with a'B'
(batch) dimension with size matching the'B'
(batch) dimension of theY
. Specify the'B'
(batch) dimension of the class weights by using a formatteddlarray
object or by using the'WeightsFormat'
option.
To specify weights for each element of the input independently, specify the weights as an array of the same size asY
. In this case, ifweights
is not a formatteddlarray
object, then the function uses the same format asY
. Alternatively, specify the weights format using the'WeightsFormat'
option.
Name-Value Arguments
Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN
, whereName
is the argument name andValue
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 encloseName
in quotes.
Example:'TargetCategories','independent','DataFormat','CB'
evaluates the cross-entropy loss for multi-label classification tasks and specifies the dimension order of the input data as'CB'
TargetCategories
—Type of classification task
'exclusive'
(default) |'independent'
Type of classification task, specified as the comma-separated pair consisting of'TargetCategories'
一个nd one of the following:
'exclusive'
— Single-label classification. Each observation in the predictionsY
is exclusively assigned to one category. The function computes the loss between the target value for the single category specified bytargets
一个nd the corresponding prediction inY
, averaged over the number of observations.'independent'
— Multi-label classification. Each observation in the predictionsY
can be assigned to one or more independent categories. The function computes the sum of the loss between each category specified bytargets
一个nd the predictions inY
for those categories, averaged over the number of observations. Cross-entropy loss for this type of classification task is also known as binary cross-entropy loss.
Mask
—Mask indicating which elements to include for loss computation
dlarray
|逻辑阵列|numeric array
Mask indicating which elements to include for loss computation, specified as adlarray
object, a logical array, or a numeric array with the same size asY
.
The function includes and excludes elements of the input data for loss computation when the corresponding value in the mask is 1 and 0, respectively.
IfMask
is a formatteddlarray
object, then its format must match that ofY
. IfMask
is not a formatteddlarray
object, then the function uses the same format asY
.
If you specify theDataFormat
option, then the function also uses the specified format for the mask.
的大小each dimension ofMask
must match the size of the corresponding dimension inY
. The default value is a logical array of ones.
Tip
Formatteddlarray
objects automatically permute the dimensions of the underlying data to have this order:"S"
(spatial),"C"
(channel),"B"
(batch),"T"
(time), and"U"
(unspecified). For example,dlarray
objects automatically permute the dimensions of data with format"TSCSBS"
to have format"SSSCBT"
.
To ensure that the dimensions ofY
一个nd the mask are consistent, whenY
is a formatteddlarray
, also specify the mask as a formatteddlarray
.
Reduction
—Mode for reducing array of loss values
"sum"
(default) |"none"
Mode for reducing the array of loss values, specified as one of the following:
NormalizationFactor
—Divisor for normalizing reduced loss
"batch-size"
(default) |“所有元素”
|"mask-included"
|"none"
Divisor for normalizing the reduced loss whenReduction
is"sum"
, specified as one of the following:
"batch-size"
— Normalize the loss by dividing it by the number of observations inY
.“所有元素”
— Normalize the loss by dividing it by the number of elements ofY
."mask-included"
— Normalize the loss by dividing the loss values by the number of included elements specified by the mask for each observation independently. To use this option, you must specify a mask using theMask
option."none"
— Do not normalize the loss.
DataFormat
—Dimension order of unformatted data
character vector|string scalar
Dimension order of unformatted input data, specified as a character vector or string scalarFMT
that provides a label for each dimension of the data.
When you specify the format of adlarray
object, each character provides a label for each dimension of the data and must be one of these options:
"S"
— Spatial"C"
— Channel"B"
— Batch (for example, samples and observations)"T"
— Time (for example, time steps of sequences)"U"
— Unspecified
You can specify multiple dimensions labeled"S"
or"U"
. You can use the labels"C"
,"B"
, and"T"
一个t most once.
You must specifyDataFormat
when the input data is not a formatteddlarray
.
Data Types:char
|string
WeightsFormat
—Dimension order of weights
character vector|string scalar
Dimension order of the weights, specified as a character vector or string scalar that provides a label for each dimension of the weights.
When you specify the format of adlarray
object, each character provides a label for each dimension of the data and must be one of these options:
"S"
— Spatial"C"
— Channel"B"
— Batch (for example, samples and observations)"T"
— Time (for example, time steps of sequences)"U"
— Unspecified
You can specify multiple dimensions labeled"S"
or"U"
. You can use the labels"C"
,"B"
, and"T"
一个t most once.
You must specifyWeightsFormat
whenweights
is a numeric vector andY
has two or more nonsingleton dimensions.
Ifweights
is not a vector, or bothweights
一个ndY
一个re vectors, then default value ofWeightsFormat
is the same as the format ofY
.
Data Types:char
|string
Output Arguments
loss
— Cross-entropy loss
dlarray
Cross-entropy loss, returned as an unformatteddlarray
. The outputloss
is an unformatteddlarray
with the same underlying data type as the inputY
.
的大小loss
depends on the'Reduction'
option.
Algorithms
Cross-Entropy Loss
For each elementYjof the input, thecrossentropy
function computes the corresponding cross-entropy element-wise loss values using the formula
whereTjis the corresponding target value toYj.
To reduce the loss values to a scalar, the function then reduces the element-wise loss using the formula
whereNis the normalization factor,mjis the mask value for elementj, andwjis the weight value for elementj.
If you do not opt to reduce the loss, then the function applies the mask and the weights to the loss values directly:
This table shows the loss formulations for different tasks.
Task | Description | Loss |
---|---|---|
Single-label classification | Cross-entropy loss for mutually exclusive classes. This is useful when observations must have a single label only. |
whereN一个ndK一个re the numbers of observations, and classes, respectively. |
Multi-label classification | Cross-entropy loss for independent classes. This is useful when observations can have multiple labels. |
whereN一个ndK一个re the numbers of observations and classes, respectively. |
Single-label classification with weighted classes | Cross-entropy loss with class weights. This is useful for datasets with imbalanced classes. |
whereN一个ndK一个re the numbers of observations and classes, respectively, andwidenotes the weight for classi. |
Sequence-to-sequence classification | Cross-entropy loss with masked time-steps. This is useful for ignoring loss values that correspond to padded data. |
whereN,S, andK一个re the numbers of observations, time steps, and classes,mntdenotes the mask value for time steptof observationn. |
Extended Capabilities
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
Usage notes and limitations:
When at least one of the following input arguments is a
gpuArray
or adlarray
with underlying data of typegpuArray
, this function runs on the GPU:Y
targets
weights
'Mask'
For more information, seeRun MATLAB Functions on a GPU(Parallel Computing Toolbox).
Version History
Introduced in R2019b
Abrir ejemplo
Tiene una versión modificada de este ejemplo. ¿Desea abrir este ejemplo con sus modificaciones?
Comando de MATLAB
Ha hecho clic en un enlace que corresponde a este comando de MATLAB:
Ejecute el comando introduciéndolo en la ventana de comandos de MATLAB. Los navegadores web no admiten comandos de 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)
- 丹麦(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)