gradCheck()
Function in IR
Compare the derivative grad builds for fn with a central difference on the CPU oracle at f64, at each point of opts.at, for every component of every parameter of opts.wrt and every component of the result.
import { gradCheck } from 'typeshade'
The signature, the description and the examples come from the compiler's own source at commit af54ddad5.
Syntax
function gradCheck(m: ModuleDecl, fn: string, opts: GradCheckOptions): GradCheckResultParameters
mModuleDeclthe module holding
fn.fnstringthe name of the function.
optsGradCheckOptionsthe parameters, the points, the mode, the step and the tolerance.
Return value
whether every partial derivative agrees, how many were compared and the worst.
Exceptions
Description
Compare the derivative grad builds for fn with a central difference on the CPU
oracle at f64, at each point of opts.at, for every component of every parameter of
opts.wrt and every component of the result. It builds the derivative in the mode
opts.mode names, so it holds forward mode and reverse mode alike.
Examples
Example
import { compile, gradCheck } from 'typeshade'
const { module } = compile(`"use typeshade"export function f(x: f32, k: f32): f32 { return sin(k * x) * k}`)gradCheck(module, 'f', { wrt: ['x', 'k'], at: [[0.5, 2]], mode: 'reverse' }).ok // trueIn the guide
See also
Source
src/core/passes/grad-check.ts, line 102, at commit af54ddad5