Function grad

Computes the gradient of a scalar-valued operation with respect to several dependencies.

Operation[] grad (
  Operation objective,
  Operation[] wrt
);

This function provides an implementation of automatic differentiation can be used for greatly simplifying the process of optimising objective functions. The particular technique used by the function is known as reverse mode automatic differentiation.

Parameters

NameDescription
objective The function being differentiated.
wrt The (indirect) dependencies that objective is being differentiated with respect to.

Returns

An array of operations that evaluate to the derivative of objective to each of the elements of wrt.

Example

import std.random : uniform;
import dopt.core : evaluate;

auto x = float32();
auto y = x * x;
auto gradY = grad(y, [x]);

auto r = uniform(-100.0f, 100.0f);

auto gradYwrtX = gradY.evaluate([
    x: Buffer([r])
])[0];

assert(gradYwrtX.as!float[0] == r + r);