dlgradient — Differentiate a traced scalar dlarray loss.

dlgradient(loss, targets...) backpropagates through the bounded automatic-differentiation tape installed by dlfeval.

Syntax

gradients = dlgradient(loss, targets...)
[g1,g2] = dlgradient(loss, target1, target2)

How dlgradient works

  • dlgradient must be called inside a dlfeval callback and loss must be a scalar traced dlarray value.
  • Host dlarray elementwise plus, minus, times, rdivide, matrix mtimes, and scalar sum(...,'all') participate in the tape.
  • Supported dlnetwork forward passes trace fully-connected, ReLU, ELU, and softmax layers and return gradients for matching Learnables trees.
  • Learnables structs, tables, cells, and dlnetwork objects preserve Layer and Parameter metadata while replacing Value entries with gradient dlarray values.
  • Implicit-expansion gradients are supported for scalar broadcasts; higher-rank broadcast-gradient reductions, custom layers, recurrent/stateful layers, and complex/autodiff over sparse data are unsupported.

GPU memory and residency

GPU-backed dlarray autodiff is rejected with an explicit provider-gap error. Plain GPU array math remains available outside the Deep Learning tape where the relevant builtin/provider hooks support it.

Example

Differentiate a custom loss

[loss,gradients] = dlfeval(@modelLoss, net, X, T)
function [loss,gradients] = modelLoss(net,X,T)
    Y = forward(net,X);
    loss = sum((Y - T).^2,'all');
    gradients = dlgradient(loss, net.Learnables);
end

Expected output:

gradients matches the learnables tree shape.

Using dlgradient with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how dlgradient changes the result.

Run a small dlgradient example, explain the result, then change one input and compare the output.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how dlgradient is executed, line by line, in Rust.

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.