#!/usr/bin/env -S jennifer run# SPDX-License-Identifier: MIT-0# pragma-jennifer-version: >=0.27.0# One neuron, out = tanh(x * w + b), on the tape engine ("Every number is a# node", "The tape is already sorted"). Prints the tape in the order it was# built, runs backward, and checks each gradient against the chain rule.include"autograd.inc.j";
use io;
use math;
def x asintinitleaf(2.0);
def w asintinitleaf(-3.0);
def b asintinitleaf(1.0);
def xw asintinitmul($x, $w);
def z asintinitadd($xw, $b);
def out asintinittanhOp($z);
io.printf("the tape, in the order the nodes were appended:\n");
def idx asintinit0;
while ($idx < len($tape)) {
io.printf(" #%d %v data=%f|prec=4\n", $idx, $tape[$idx].op, $tape[$idx].data);
$idx = $idx + 1;
}
backward($out);
# d tanh(z) / dz = 1 - tanh(z)^2; the rest is the chain rule by hand.def dz asfloatinit1.0 - valueOf($out) * valueOf($out);
io.printf("\nbackward walked it from #%d down to #0:\n", $out);
io.printf(" dout/dx = %f|prec=6 (by hand %f|prec=6)\n", gradOf($x), valueOf($w) * $dz);
io.printf(" dout/dw = %f|prec=6 (by hand %f|prec=6)\n", gradOf($w), valueOf($x) * $dz);
io.printf(" dout/db = %f|prec=6 (by hand %f|prec=6)\n", gradOf($b), $dz);