|
| 1 | +/- |
| 2 | +Copyright (c) 2026 TorchLean |
| 3 | +Released under MIT license as described in the file LICENSE. |
| 4 | +Authors: TorchLean Team |
| 5 | +-/ |
| 6 | + |
| 7 | +module |
| 8 | + |
| 9 | +public import NN |
| 10 | + |
| 11 | +/-! |
| 12 | +# Functional transcendentals + scalar-affine: autograd correctness |
| 13 | +
|
| 14 | +Positive / negative example for the `nn.functional.{exp, log, scale, shift, affine}` |
| 15 | +ops added for scientific forward models — e.g. the soil-moisture retrieval that |
| 16 | +combines SMAP (Soil Moisture Active Passive) and NISAR (NASA–ISRO Synthetic |
| 17 | +Aperture Radar) observations through the AVS (Attenuation–Volume–Surface) model, |
| 18 | +whose surface term is `exp(-2·b·NDVI)·c·|R|²`. |
| 19 | +
|
| 20 | +The point is that these ops are differentiated by the **autograd engine**, so a |
| 21 | +forward model written once yields its gradient with no hand-coded derivative. |
| 22 | +Each check differentiates a tiny function and compares the autograd gradient to |
| 23 | +the closed form: |
| 24 | +
|
| 25 | +* positive controls — the gradient matches the analytic value; |
| 26 | +* negative controls — a deliberately *wrong* analytic value (notably the |
| 27 | + wrong-sign gradient of `exp(-2x)`) does **not** match. That is exactly the |
| 28 | + defect class — a sign/factor error in a hand-coded Jacobian — that deriving the |
| 29 | + gradient by autograd eliminates. |
| 30 | +
|
| 31 | +`#eval checkAll` runs at build time and fails the build on any regression. |
| 32 | +-/ |
| 33 | + |
| 34 | +@[expose] public section |
| 35 | + |
| 36 | +namespace NN.Examples.Functional.Transcendentals |
| 37 | + |
| 38 | +open Spec |
| 39 | +open Tensor |
| 40 | +open NN.Tensor |
| 41 | +open NN.API |
| 42 | + |
| 43 | +/-! ## Functions under test (written once; gradients come from autograd) -/ |
| 44 | + |
| 45 | +/-- `f(x) = eˣ`. -/ |
| 46 | +def expFn : autograd.fn1.Fn Spec.Shape.scalar Spec.Shape.scalar := |
| 47 | + fun x => nn.functional.exp x |
| 48 | + |
| 49 | +/-- `f(x) = e^{-2x}` — the shape of the AVS canopy two-way transmittance as a |
| 50 | +function of the attenuation parameter. -/ |
| 51 | +def expNeg2Fn : autograd.fn1.Fn Spec.Shape.scalar Spec.Shape.scalar := |
| 52 | + fun x => do |
| 53 | + let u ← nn.functional.scale x (-Numbers.two) |
| 54 | + nn.functional.exp u |
| 55 | + |
| 56 | +/-- `f(x) = 3·x + 1` via the scalar-affine op. -/ |
| 57 | +def affineFn : autograd.fn1.Fn Spec.Shape.scalar Spec.Shape.scalar := |
| 58 | + fun x => nn.functional.affine x Numbers.three Numbers.one |
| 59 | + |
| 60 | +/-! ## Float checks -/ |
| 61 | + |
| 62 | +/-- Absolute-tolerance float compare. -/ |
| 63 | +def approx (a b : Float) (tol : Float := 1e-6) : Bool := (a - b).abs ≤ tol |
| 64 | + |
| 65 | +/-- Positive control: `name`'s autograd gradient ≈ expected; throws on mismatch. -/ |
| 66 | +def expectGrad (name : String) (got expected : Float) (tol : Float := 1e-6) : IO Unit := |
| 67 | + if approx got expected tol then |
| 68 | + IO.println s!"[PASS] {name}: grad = {got} ≈ {expected}" |
| 69 | + else |
| 70 | + throw <| IO.userError s!"[FAIL] {name}: grad = {got}, expected {expected}" |
| 71 | + |
| 72 | +/-- Negative control: the gradient must *not* equal `wrong`; throws if it does. -/ |
| 73 | +def expectNot (name : String) (got wrong : Float) (tol : Float := 1e-6) : IO Unit := |
| 74 | + if approx got wrong tol then |
| 75 | + throw <| IO.userError s!"[FAIL-NEG] {name}: grad = {got} wrongly matched {wrong}" |
| 76 | + else |
| 77 | + IO.println s!"[PASS-NEG] {name}: grad = {got} ≠ {wrong} (test discriminates)" |
| 78 | + |
| 79 | +/-- Differentiate a scalar→scalar `Fn` at a Float point, returning the gradient. -/ |
| 80 | +def gradAt (f : autograd.fn1.Fn Spec.Shape.scalar Spec.Shape.scalar) (x0 : Float) : |
| 81 | + IO Float := do |
| 82 | + let x : Spec.Tensor Float Spec.Shape.scalar := Spec.fill (x0 : Float) Spec.Shape.scalar |
| 83 | + let g ← autograd.fn1.grad (α := Float) f x |
| 84 | + pure (Spec.toScalarSpec g) |
| 85 | + |
| 86 | +def checkAll : IO Unit := do |
| 87 | + -- exp: d/dx eˣ = eˣ |
| 88 | + let ge ← gradAt expFn 0.5 |
| 89 | + expectGrad "exp" ge (Float.exp 0.5) |
| 90 | + expectNot "exp≠1" ge 1.0 -- a constant-1 gradient would be caught |
| 91 | + |
| 92 | + -- affine: d/dx (3x+1) = 3 |
| 93 | + let ga ← gradAt affineFn 0.5 |
| 94 | + expectGrad "affine(3x+1)" ga 3.0 |
| 95 | + expectNot "affine≠1" ga 1.0 -- the slope is 3, not 1 |
| 96 | + |
| 97 | + -- exp(-2x): d/dx e^{-2x} = -2·e^{-2x} |
| 98 | + let gn ← gradAt expNeg2Fn 0.5 |
| 99 | + expectGrad "exp(-2x)" gn ((-2.0) * Float.exp (-1.0)) |
| 100 | + -- THE AVS bug class: the wrong-SIGN analytic (+2·e^{-2x}) must NOT match. |
| 101 | + expectNot "exp(-2x) sign" gn (( 2.0) * Float.exp (-1.0)) |
| 102 | + |
| 103 | + IO.println "[transcendentals] all positive + negative controls passed ✓" |
| 104 | + |
| 105 | +end NN.Examples.Functional.Transcendentals |
| 106 | + |
| 107 | +/-- Compiled entry point. Autograd uses the native runtime, so this runs as a |
| 108 | +compiled `lean_exe` (`lake exe transcendentals_check`), not via `#eval` (the |
| 109 | +interpreter cannot load the native tape externs). -/ |
| 110 | +def main : IO Unit := NN.Examples.Functional.Transcendentals.checkAll |
0 commit comments