UNPKG

scalar-autograd

Version:

Scalar-based reverse-mode automatic differentiation in TypeScript.

25 lines (24 loc) 890 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Value_1 = require("./Value"); describe('Gradient flow control', () => { it('stops gradient at non-requiresGrad nodes', () => { const x = new Value_1.Value(2, 'x', true); const y = new Value_1.Value(3, 'y', false); const z = new Value_1.Value(4, 'z', true); const out = x.mul(y).add(z); out.backward(); expect(x.grad).toBe(3); expect(y.grad).toBe(0); expect(z.grad).toBe(1); }); it('handles detached computation graphs', () => { const x = new Value_1.Value(2, 'x', true); const y = x.mul(3); const z = new Value_1.Value(y.data, 'z', true); // detached const out = z.mul(4); out.backward(); expect(z.grad).toBe(4); expect(x.grad).toBe(0); // no gradient flows to x }); });