MCPcopy Create free account
hub / github.com/ml-explore/mlx / automatic_differentiation

Function automatic_differentiation

examples/cpp/tutorial.cpp:81–94  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

79}
80
81void automatic_differentiation() {
82 auto fn = [](mx::array x) { return mx::square(x); };
83
84 // Computing the derivative function of a function
85 auto grad_fn = mx::grad(fn);
86 // Call grad_fn on the input to get the derivative
87 auto x = mx::array(1.5);
88 auto dfdx = grad_fn(x);
89 // dfdx is 2 * x
90
91 // Get the second derivative by composing grad with grad
92 auto d2fdx2 = mx::grad(mx::grad(fn))(x);
93 // d2fdx2 is 2
94}
95
96int main() {
97 array_basics();

Callers 1

mainFunction · 0.85

Calls 3

squareFunction · 0.85
gradFunction · 0.85
arrayFunction · 0.50

Tested by

no test coverage detected