torch.autograd.gradcheck
function gradcheck(fn: (...inputs: Tensor[]) => Tensor, inputs: Tensor[], options: GradcheckOptions = {}): Promise<boolean>Check gradients of a function by comparing analytical and numerical gradients.
For each input tensor, computes:
- Analytical gradient via autograd (backward pass)
- Numerical gradient via finite differences
And verifies they match within tolerance.
Parameters
inputsTensor[]- Input tensors (should have requires_grad=true)
optionsGradcheckOptionsoptional- Gradient checking options
Returns
Promise<boolean>– Promise resolving to true if gradients match, false otherwiseExamples
import { gradcheck } from '@torchjsorg/torch.js';
// Define function to check
const fn = (inputs: Tensor[]) => {
const [x, y] = inputs;
return x.mul(y).sum();
};
// Create inputs with requires_grad
const x = torch.randn(3, 4, { requires_grad: true });
const y = torch.randn(3, 4, { requires_grad: true });
// Check gradients
const passed = await gradcheck(fn, [x, y]);
console.log(passed ? 'Gradients OK!' : 'Gradient mismatch!');