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 dlarray objects, or plain real gpuArray tensors 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 Weights name-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, and Mask.
  • DataFormat and WeightsFormat validate RunMat's supported deep-learning labels S, C, B, T, and U; WeightsFormat maps weight dimensions to prediction dimensions by label.
  • ClassificationMode supports single-label/exclusive and multi-label/independent aliases.
  • Reduction supports sum and none; unreduced output preserves the prediction shape.
  • NormalizationFactor supports all-elements, batch-size, mask-included, none, and positive numeric scalars.
  • Mask must be a full-size logical or binary numeric array.
  • Plain real gpuArray prediction 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 real gpuArray weights and masks are consumed directly. Reduction="none" returns a resident gpuArray; scalar reductions reduce on the provider and read back only the scalar result.
  • Host dlarray inputs return host dlarray compatibility outputs; reduced scalar losses and unreduced loss arrays are returned as unformatted dlarray objects.
  • Datastore/category target expansion, automatic differentiation, GPU-backed dlarray, NormalizationFactor="mask-included" for GPU inputs, and non-full-size resident GPU weight remapping with WeightsFormat remain 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.

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.