sourcelibs/Equations/demos/integrals.xtl

1⍝!/usr/bin/env xetal 2⍝│ Two continuous equations of machine learning, written as the papers 3⍝│ print them. GELU, the activation of BERT and GPT, is x Phi(x) 4⍝│ (Hendrycks and Gimpel, 2016), Phi the Gaussian CDF: 1/2 plus the 5⍝│ integral from 0 to x of the density exp(-u^2/2) / sqrt(2 pi), so no 6⍝│ improper bound is needed. The tanh form the frameworks compute is an 7⍝│ approximation; the integral says by how much. Then the KL divergence 8⍝│ of two Gaussians, the integral of p log(p / q) over a truncation 9⍝│ (the tails beyond -10..10 are below 1e-20), beside its closed form. 10⍝│ Run with "just demo Equations integrals". 11"e:" u̲se< "Equations" 12pi ← 3.141592653589793 13⍝│ GELU at one x: x (1/2 + the integral of the density from 0 to x), simpson on 32 steps. 14ᵘ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)))
} @))
} @))
15⍝│ The tanh approximation: x/2 (1 + tanh(sqrt(2/pi) (x + 0.044715 x^3))), tanh by exp. 16ᵘt̲anh ← { z → ((e̲xp z) − e̲xp n̲eg z) ÷ (e̲xp z) + e̲xp n̲eg z } 17ᵘa̲pprox ← { x → 0.5 × x × 1.0 + ᵘt̲anh ((2.0 ÷ pi) ^ 0.5) × x + 0.044715 × x ^ 3 } 18xs ← -3.0 -2.0 -1.0 -0.5 0.0 0.5 1.0 2.0 3.0 19g ← 'ᵘg̲elu e̲ach xs 20(f̲loat f̲loor 0.5 + 10000.0 × g) ÷ 10000.0 21⍝│ The approximation's error at those points, and its largest over -4..4. 22(f̲loat f̲loor 0.5 + 1000000.0 × g − ᵘa̲pprox xs) ÷ 1000000.0 23fine ← -4.0 + 0.05 × f̲loat o̲ffsets 161 24(f̲loat f̲loor 0.5 + 1000000.0 × 'm̲ax r̲/ a̲bs ('ᵘg̲elu e̲ach fine) − ᵘa̲pprox fine) ÷ 1000000.0 25⍝│ What the paper writes. 26"$$" c̲at ("Phi[] | " ᵉt̲ex< "1 / 2 + (integral u:=0..x | exp(-(u^2) / 2) / sqrt(2 * pi))") c̲at "$$"
ᵉt̲ex< expands to
("\\Phi = \\frac{1}{2} + \\left(\\int_{0}^{x} \\frac{\\exp(-\\frac{u^{2}}{2})}{\\sqrt{2 \\, \\pi}} \\, du\\right)")
27⍝│ KL(N(0, 1) || N(1, 2)) over -10..10 by simpson on 400 steps, and the 28⍝│ closed form log(s2 / s1) + (s1^2 + (m1 - m2)^2) / (2 s2^2) - 1/2. 29(m1, s1, m2, s2) ← (0.0, 1.0, 1.0, 2.0) 30kl ← ᵉ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)))))))))
} @))
} @))
31closed ← (l̲og s2 ÷ s1) + ((s1² + (m1 − m2)²) ÷ 2.0 × s2²) − 0.5 32(f̲loat f̲loor 0.5 + 1000000.0 × kl c̲at closed) ÷ 1000000.0 33"$$" c̲at ("KL[] | " ᵉt̲ex< "integral x:=-10..10 | P[x] * log(P[x] / Q[x])") c̲at "$$"
ᵉt̲ex< expands to
("KL = \\int_{-10}^{10} P_{x} \\, \\log(\\frac{P_{x}}{Q_{x}}) \\, dx")