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
dlgradientmust be called inside adlfevalcallback andlossmust be a scalar traceddlarrayvalue.- Host
dlarrayelementwiseplus,minus,times,rdivide, matrixmtimes, and scalarsum(...,'all')participate in the tape. - Supported
dlnetworkforward passes trace fully-connected, ReLU, ELU, and softmax layers and return gradients for matchingLearnablestrees. - Learnables structs, tables, cells, and
dlnetworkobjects preserveLayerandParametermetadata while replacingValueentries with gradientdlarrayvalues. - 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);
endExpected 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.
Related Deep Learning functions
adamupdate · analyzeNetwork · bilstmLayer · classificationLayer · combvec · convolution1dLayer · crossentropy · dlarray · dlfeval · dlnetwork · dlupdate · eluLayer · exportONNXNetwork · featureInputLayer · forward · fullyConnectedLayer · globalAveragePooling1dLayer · layerGraph · layerNormalizationLayer · lstmLayer · padsequences · regressionLayer · reluLayer · sequenceInputLayer · softmaxLayer · trainingOptions · trainnet · trainNetwork
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how dlgradient is executed, line by line, in Rust.
- View the source for dlgradient in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
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.