| 79 | } |
| 80 | |
| 81 | void 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 | |
| 96 | int main() { |
| 97 | array_basics(); |