crossentropy — Compute cross-entropy loss for numeric predictions and targets.
crossentropy(Y,T) computes cross-entropy over same-sized host numeric arrays, host dlarray compatibility objects, or plain resident real gpuArray tensors.
Syntax
loss = crossentropy(Y, T, weights, Name, Value)How crossentropy works
- Inputs must be finite, non-empty numeric arrays, host-backed
dlarrayobjects, or plain realgpuArraytensors with the same number of elements. - Predictions are clipped to a small open interval before taking logarithms; targets must be probabilities in the range [0, 1].
- The third positional argument or
Weightsname-value option may provide scalar, prediction-sized, channel-sized, or observation-sized weights. - Supported name-value options are
ClassificationMode,TargetCategories,Reduction,NormalizationFactor,DataFormat,WeightsFormat,Weights, andMask. DataFormatandWeightsFormatvalidate RunMat's supported deep-learning labelsS,C,B,T, andU;WeightsFormatmaps weight dimensions to prediction dimensions by label.ClassificationModesupports single-label/exclusive and multi-label/independent aliases.Reductionsupportssumandnone; unreduced output preserves the prediction shape.NormalizationFactorsupportsall-elements,batch-size,mask-included,none, and positive numeric scalars.Maskmust be a full-size logical or binary numeric array.- Plain real
gpuArrayprediction and target tensors use provider-side cross-entropy kernels for same-shape losses. Scalar, prediction-sized, channel-sized, and observation-sized host weights are materialized as temporary provider inputs; full prediction-sized resident realgpuArrayweights and masks are consumed directly.Reduction="none"returns a residentgpuArray; scalar reductions reduce on the provider and read back only the scalar result. - Host
dlarrayinputs return hostdlarraycompatibility outputs; reduced scalar losses and unreduced loss arrays are returned as unformatteddlarrayobjects. - Datastore/category target expansion, automatic differentiation, GPU-backed
dlarray,NormalizationFactor="mask-included"for GPU inputs, and non-full-size resident GPU weight remapping withWeightsFormatremain future work.
GPU memory and residency
crossentropy preserves residency for plain real gpuArray prediction and target tensors through the runmat-accelerate crossentropy_terms provider hook. Unreduced losses remain resident; scalar reductions use provider reduction and read back only the scalar. Host weights and masks that can be expanded from scalar, prediction-sized, channel-sized, or observation-sized forms are uploaded as temporary provider tensors. Resident GPU weights and masks stay on the provider when they are real and exactly prediction-sized; WeightsFormat remapping and NormalizationFactor="mask-included" still fall back to the host path.
Examples
Compute Loss
loss = crossentropy([0.8 0.2], [1 0])Expected output:
Scalar loss.Use Class Weights
loss = crossentropy(Y, T, weights, "DataFormat", "CB")Expected output:
Weighted scalar loss normalized by batch size.Unreduced Multi-Label Loss
losses = crossentropy(Y, T, "ClassificationMode", "multi-label", "Reduction", "none")Expected output:
Array of per-element losses.Using crossentropy with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how crossentropy changes the result.
Run a small crossentropy example, explain the result, then change one input and compare the output.
Related Deep Learning functions
adamupdate · analyzeNetwork · bilstmLayer · classificationLayer · combvec · convolution1dLayer · dlarray · dlfeval · dlgradient · dlnetwork · dlupdate · eluLayer · exportONNXNetwork · featureInputLayer · forward · fullyConnectedLayer · globalAveragePooling1dLayer · layerGraph · layerNormalizationLayer · lstmLayer · padsequences · regressionLayer · reluLayer · sequenceInputLayer · softmaxLayer · trainingOptions · trainnet · trainNetwork
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.