programlibs/Equations/demos/integrals.xtl
Two continuous equations of machine learning, written as the papers print them. GELU, the activation of BERT and GPT, is x Phi(x) (Hendrycks and Gimpel, 2016), Phi the Gaussian CDF: 1/2 plus the integral from 0 to x of the density exp(-u^2/2) / sqrt(2 pi), so no improper bound is needed. The tanh form the frameworks compute is an approximation; the integral says by how much. Then the KL divergence of two Gaussians, the integral of p log(p / q) over a truncation (the tails beyond -10..10 are below 1e-20), beside its closed form. Run with "just demo Equations integrals".
ᵘg̲elu : Float -> Float
GELU at one x: x (1/2 + the integral of the density from 0 to x), simpson on 32 steps.
ᵘg̲elu ← { x → x × 0.5 + ᵉe̲q< L = \int_{0}^{x} \frac{\exp(-\frac{u^{2}}{2})}{\sqrt{2 \, \pi}} \, du }
ᵉe̲q< expands to
(({ @ → g3:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g2:c̲heck ← { (A, name, k, from) e → n ← k s̲elect s̲hape A bad ← w̲here n̲ot r̲avel (0 ≤ e) ∧ e < n 0 = t̲ally bad ? e "index" ⎕S̲IGNAL name c̲at " axis " c̲at (f̲ormat k) c̲at ": index " c̲at (f̲ormat (f̲irst bad) s̲elect r̲avel e) c̲at " is outside 0.." c̲at (f̲ormat n − 1) c̲at " (from " c̲at from c̲at ")" } g1:r̲ank ← { (A, name, q, ref) → (t̲ally s̲hape A) = q ? A "rank" ⎕S̲IGNAL ref c̲at " indexes " c̲at (f̲ormat q) c̲at " axes, but " c̲at name c̲at " has rank " c̲at f̲ormat t̲ally s̲hape A } (({ @ → g9:a ← f̲loat (0) g8:b ← f̲loat (x) g7:h ← (g8:b − g9:a) ÷ 32.0 g6:w ← g7:h × (1.0 c̲at (31 r̲eshape 4.0 2.0) c̲at 1.0) ÷ 3.0 g10:grid ← (33) g11:g̲rid ← { k → g10:grid r̲eshape (o̲ffsets k s̲elect g10:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g10:grid) r̲eshape 0 } '+ r̲/₁ g10:grid r̲eshape ((1 + g11:g̲rid 1) s̲elect g6:w) × (((e̲xp (0 − ((((g9:a + g7:h × f̲loat g11:g̲rid 1) ^ 2)) ÷ 2))) ÷ ((2 × pi) ^ 0.5))) } @)) } @))
ᵘt̲anh : Num a => a -> Float
The tanh approximation: x/2 (1 + tanh(sqrt(2/pi) (x + 0.044715 x^3))), tanh by exp.
ᵘt̲anh ← { z → ((e̲xp z) − e̲xp n̲eg z) ÷ (e̲xp z) + e̲xp n̲eg z }
ᵘa̲pprox : Float -> Float
ᵘa̲pprox ← { x → 0.5 × x × 1.0 + ᵘt̲anh ((2.0 ÷ pi) ^ 0.5) × x + 0.044715 × x ^ 3 }
xs : Float
xs ← -3.0 -2.0 -1.0 -0.5 0.0 0.5 1.0 2.0 3.0
m1 : Float
KL(N(0, 1) || N(1, 2)) over -10..10 by simpson on 400 steps, and the closed form log(s2 / s1) + (s1^2 + (m1 - m2)^2) / (2 s2^2) - 1/2.
(m1, s1, m2, s2) ← (0.0, 1.0, 1.0, 2.0)
s1 : Float
KL(N(0, 1) || N(1, 2)) over -10..10 by simpson on 400 steps, and the closed form log(s2 / s1) + (s1^2 + (m1 - m2)^2) / (2 s2^2) - 1/2.
(m1, s1, m2, s2) ← (0.0, 1.0, 1.0, 2.0)
m2 : Float
KL(N(0, 1) || N(1, 2)) over -10..10 by simpson on 400 steps, and the closed form log(s2 / s1) + (s1^2 + (m1 - m2)^2) / (2 s2^2) - 1/2.
(m1, s1, m2, s2) ← (0.0, 1.0, 1.0, 2.0)
s2 : Float
KL(N(0, 1) || N(1, 2)) over -10..10 by simpson on 400 steps, and the closed form log(s2 / s1) + (s1^2 + (m1 - m2)^2) / (2 s2^2) - 1/2.
(m1, s1, m2, s2) ← (0.0, 1.0, 1.0, 2.0)
kl : Float
kl ← ᵉe̲q< L = \int_{-10}^{10} \left(\frac{\exp(-\frac{\left(x - m1\right)^{2}}{2 \, s1^{2}})}{s1 \, \sqrt{2 \, \pi}}\right) \, \log(\frac{\frac{\exp(-\frac{\left(x - m1\right)^{2}}{2 \, s1^{2}})}{s1 \, \sqrt{2 \, \pi}}}{\frac{\exp(-\frac{\left(x - m2\right)^{2}}{2 \, s2^{2}})}{s2 \, \sqrt{2 \, \pi}}}) \, dx
ᵉe̲q< expands to
(({ @ → g14:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g13:c̲heck ← { (A, name, k, from) e → n ← k s̲elect s̲hape A bad ← w̲here n̲ot r̲avel (0 ≤ e) ∧ e < n 0 = t̲ally bad ? e "index" ⎕S̲IGNAL name c̲at " axis " c̲at (f̲ormat k) c̲at ": index " c̲at (f̲ormat (f̲irst bad) s̲elect r̲avel e) c̲at " is outside 0.." c̲at (f̲ormat n − 1) c̲at " (from " c̲at from c̲at ")" } g12:r̲ank ← { (A, name, q, ref) → (t̲ally s̲hape A) = q ? A "rank" ⎕S̲IGNAL ref c̲at " indexes " c̲at (f̲ormat q) c̲at " axes, but " c̲at name c̲at " has rank " c̲at f̲ormat t̲ally s̲hape A } (({ @ → g20:a ← f̲loat ((0 − 10)) g19:b ← f̲loat (10) g18:h ← (g19:b − g20:a) ÷ 400.0 g17:w ← g18:h × (1.0 c̲at (399 r̲eshape 4.0 2.0) c̲at 1.0) ÷ 3.0 g21:grid ← (401) g22:g̲rid ← { k → g21:grid r̲eshape (o̲ffsets k s̲elect g21:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g21:grid) r̲eshape 0 } '+ r̲/₁ g21:grid r̲eshape ((1 + g22:g̲rid 1) s̲elect g17:w) × (((((e̲xp (0 − ((((((g20:a + g18:h × f̲loat g22:g̲rid 1) − m1)) ^ 2)) ÷ ((2 × (s1 ^ 2)))))) ÷ ((s1 × ((2 × pi) ^ 0.5))))) × (l̲og ((((e̲xp (0 − ((((((g20:a + g18:h × f̲loat g22:g̲rid 1) − m1)) ^ 2)) ÷ ((2 × (s1 ^ 2)))))) ÷ ((s1 × ((2 × pi) ^ 0.5))))) ÷ (((e̲xp (0 − ((((((g20:a + g18:h × f̲loat g22:g̲rid 1) − m2)) ^ 2)) ÷ ((2 × (s2 ^ 2)))))) ÷ ((s2 × ((2 × pi) ^ 0.5))))))))) } @)) } @))