programlibs/Equations/demos/catalog.xtl

Thirteen equations as ML papers print them, each one call: its LaTeX (a line between $$ is drawn as math in the live demo), its value, and 1 when the value equals the same computation written as plain X_eTaL. Run with "just demo Equations catalog".

source · imports e: libs/Equations/src/Equations.xtl libs/Equations/src/Equations.xtlm

A matrix product: C[i,k] = sum_j A[i,j] B[j,k]

A : Float

value · line 9
A ← f̲loat 2 3 r̲eshape 1 2 3 4 5 6

B : Float

value · line 10
B ← f̲loat 3 2 r̲eshape 1 0 0 1 1 1

C : Float

value · line 11
(C, tex1) ←  ᵉb̲oth< C_{i,k} = \sum_{j=0}^{2} A_{i,j} \, B_{j,k}
ᵉb̲oth< 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
  }
  g4:grid ← 2 c̲at 2
  g5:g̲rid ← { k → g4:grid r̲eshape (o̲ffsets k s̲elect g4:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g4:grid) r̲eshape 0 }
  g4:grid r̲eshape (({ @ →
  g6:grid ← 2 c̲at 2 c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0))
  g7:g̲rid ← { k → g6:grid r̲eshape (o̲ffsets k s̲elect g6:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g6:grid) r̲eshape 0 }
  '+ r̲/₃ g6:grid r̲eshape ((
    ((g1:r̲ank (A, "A", 2, "A[i,j]")) g3:a̲t ((
      ((A, "A", 2, "j") g2:c̲heck (0 + g7:g̲rid 3)) + (2 s̲elect s̲hape A) × 
      ((A, "A", 1, "i") g2:c̲heck (0 + g7:g̲rid 1))))) × 
    ((g1:r̲ank (B, "B", 2, "B[j,k]")) g3:a̲t ((
      ((B, "B", 2, "k") g2:c̲heck (0 + g7:g̲rid 2)) + (2 s̲elect s̲hape B) × 
      ((B, "B", 1, "j") g2:c̲heck (0 + g7:g̲rid 3)))))))
} @))
} @), "C_{i,k} = \\sum_{j=0}^{2} A_{i,j} \\, B_{j,k}"))

tex1 : Char

value · line 11
(C, tex1) ←  ᵉb̲oth< C_{i,k} = \sum_{j=0}^{2} A_{i,j} \, B_{j,k}
ᵉb̲oth< 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
  }
  g4:grid ← 2 c̲at 2
  g5:g̲rid ← { k → g4:grid r̲eshape (o̲ffsets k s̲elect g4:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g4:grid) r̲eshape 0 }
  g4:grid r̲eshape (({ @ →
  g6:grid ← 2 c̲at 2 c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0))
  g7:g̲rid ← { k → g6:grid r̲eshape (o̲ffsets k s̲elect g6:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g6:grid) r̲eshape 0 }
  '+ r̲/₃ g6:grid r̲eshape ((
    ((g1:r̲ank (A, "A", 2, "A[i,j]")) g3:a̲t ((
      ((A, "A", 2, "j") g2:c̲heck (0 + g7:g̲rid 3)) + (2 s̲elect s̲hape A) × 
      ((A, "A", 1, "i") g2:c̲heck (0 + g7:g̲rid 1))))) × 
    ((g1:r̲ank (B, "B", 2, "B[j,k]")) g3:a̲t ((
      ((B, "B", 2, "k") g2:c̲heck (0 + g7:g̲rid 2)) + (2 s̲elect s̲hape B) × 
      ((B, "B", 1, "j") g2:c̲heck (0 + g7:g̲rid 3)))))))
} @))
} @), "C_{i,k} = \\sum_{j=0}^{2} A_{i,j} \\, B_{j,k}"))

A dense layer with its bias: z[i] = sum_j W[i,j] x[j] + b[i]

W : Float

value · line 17
W ← f̲loat 2 3 r̲eshape 1 2 3 4 5 6

x : Float

value · line 18
x ← f̲loat 1 1 1

b : Float

value · line 19
b ← f̲loat 10 20

z : Float

value · line 20
(z, tex2) ←  ᵉb̲oth< z_{i} = \left(\sum_{j=0}^{2} W_{i,j} \, x_{j}\right) + b_{i}
ᵉb̲oth< expands to
((({ @ →
  g10:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g9: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 ")"
  }
  g8: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
  }
  g11:grid ← 2
  g12:g̲rid ← { k → g11:grid r̲eshape (o̲ffsets k s̲elect g11:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g11:grid) r̲eshape 0 }
  g11:grid r̲eshape ((({ @ →
  g13:grid ← 2 c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0))
  g14:g̲rid ← { k → g13:grid r̲eshape (o̲ffsets k s̲elect g13:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g13:grid) r̲eshape 0 }
  '+ r̲/₂ g13:grid r̲eshape ((
    ((g8:r̲ank (W, "W", 2, "W[i,j]")) g10:a̲t ((
      ((W, "W", 2, "j") g9:c̲heck (0 + g14:g̲rid 2)) + (2 s̲elect s̲hape W) × 
      ((W, "W", 1, "i") g9:c̲heck (0 + g14:g̲rid 1))))) × 
    ((g8:r̲ank (x, "x", 1, "x[j]")) g10:a̲t (
      ((x, "x", 1, "j") g9:c̲heck (0 + g14:g̲rid 2))))))
} @) + 
    ((g8:r̲ank (b, "b", 1, "b[i]")) g10:a̲t (
      ((b, "b", 1, "i") g9:c̲heck (0 + g12:g̲rid 1))))))
} @), "z_{i} = \\left(\\sum_{j=0}^{2} W_{i,j} \\, x_{j}\\right) + b_{i}"))

tex2 : Char

value · line 20
(z, tex2) ←  ᵉb̲oth< z_{i} = \left(\sum_{j=0}^{2} W_{i,j} \, x_{j}\right) + b_{i}
ᵉb̲oth< expands to
((({ @ →
  g10:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g9: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 ")"
  }
  g8: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
  }
  g11:grid ← 2
  g12:g̲rid ← { k → g11:grid r̲eshape (o̲ffsets k s̲elect g11:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g11:grid) r̲eshape 0 }
  g11:grid r̲eshape ((({ @ →
  g13:grid ← 2 c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0))
  g14:g̲rid ← { k → g13:grid r̲eshape (o̲ffsets k s̲elect g13:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g13:grid) r̲eshape 0 }
  '+ r̲/₂ g13:grid r̲eshape ((
    ((g8:r̲ank (W, "W", 2, "W[i,j]")) g10:a̲t ((
      ((W, "W", 2, "j") g9:c̲heck (0 + g14:g̲rid 2)) + (2 s̲elect s̲hape W) × 
      ((W, "W", 1, "i") g9:c̲heck (0 + g14:g̲rid 1))))) × 
    ((g8:r̲ank (x, "x", 1, "x[j]")) g10:a̲t (
      ((x, "x", 1, "j") g9:c̲heck (0 + g14:g̲rid 2))))))
} @) + 
    ((g8:r̲ank (b, "b", 1, "b[i]")) g10:a̲t (
      ((b, "b", 1, "i") g9:c̲heck (0 + g12:g̲rid 1))))))
} @), "z_{i} = \\left(\\sum_{j=0}^{2} W_{i,j} \\, x_{j}\\right) + b_{i}"))

Attention scores and output: S = Q K^T / sqrt(d), O = S V

Q : Float

value · line 26
Q ← f̲loat 2 4 r̲eshape 1 0 0 0 0 1 0 0

K : Float

value · line 27
K ← f̲loat 2 4 r̲eshape 2 0 0 0 0 2 0 0

V : Float

value · line 28
V ← f̲loat 2 3 r̲eshape 1 2 3 4 5 6

S : Float

value · line 29
(S, tex3) ←  ᵉb̲oth< S_{i,j} = \frac{\sum_{d=0}^{3} Q_{i,d} \, K_{j,d}}{\sqrt{4}}
ᵉb̲oth< expands to
((({ @ →
  g17:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g16: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 ")"
  }
  g15: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
  }
  g18:grid ← 2 c̲at 2
  g19:g̲rid ← { k → g18:grid r̲eshape (o̲ffsets k s̲elect g18:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g18:grid) r̲eshape 0 }
  g18:grid r̲eshape ((({ @ →
  g20:grid ← 2 c̲at 2 c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..3" } (1 + 3 − 0))
  g21:g̲rid ← { k → g20:grid r̲eshape (o̲ffsets k s̲elect g20:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g20:grid) r̲eshape 0 }
  '+ r̲/₃ g20:grid r̲eshape ((
    ((g15:r̲ank (Q, "Q", 2, "Q[i,d]")) g17:a̲t ((
      ((Q, "Q", 2, "d") g16:c̲heck (0 + g21:g̲rid 3)) + (2 s̲elect s̲hape Q) × 
      ((Q, "Q", 1, "i") g16:c̲heck (0 + g21:g̲rid 1))))) × 
    ((g15:r̲ank (K, "K", 2, "K[j,d]")) g17:a̲t ((
      ((K, "K", 2, "d") g16:c̲heck (0 + g21:g̲rid 3)) + (2 s̲elect s̲hape K) × 
      ((K, "K", 1, "j") g16:c̲heck (0 + g21:g̲rid 2)))))))
} @) ÷ (4 ^ 0.5)))
} @), "S_{i,j} = \\frac{\\sum_{d=0}^{3} Q_{i,d} \\, K_{j,d}}{\\sqrt{4}}"))

tex3 : Char

value · line 29
(S, tex3) ←  ᵉb̲oth< S_{i,j} = \frac{\sum_{d=0}^{3} Q_{i,d} \, K_{j,d}}{\sqrt{4}}
ᵉb̲oth< expands to
((({ @ →
  g17:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g16: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 ")"
  }
  g15: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
  }
  g18:grid ← 2 c̲at 2
  g19:g̲rid ← { k → g18:grid r̲eshape (o̲ffsets k s̲elect g18:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g18:grid) r̲eshape 0 }
  g18:grid r̲eshape ((({ @ →
  g20:grid ← 2 c̲at 2 c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..3" } (1 + 3 − 0))
  g21:g̲rid ← { k → g20:grid r̲eshape (o̲ffsets k s̲elect g20:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g20:grid) r̲eshape 0 }
  '+ r̲/₃ g20:grid r̲eshape ((
    ((g15:r̲ank (Q, "Q", 2, "Q[i,d]")) g17:a̲t ((
      ((Q, "Q", 2, "d") g16:c̲heck (0 + g21:g̲rid 3)) + (2 s̲elect s̲hape Q) × 
      ((Q, "Q", 1, "i") g16:c̲heck (0 + g21:g̲rid 1))))) × 
    ((g15:r̲ank (K, "K", 2, "K[j,d]")) g17:a̲t ((
      ((K, "K", 2, "d") g16:c̲heck (0 + g21:g̲rid 3)) + (2 s̲elect s̲hape K) × 
      ((K, "K", 1, "j") g16:c̲heck (0 + g21:g̲rid 2)))))))
} @) ÷ (4 ^ 0.5)))
} @), "S_{i,j} = \\frac{\\sum_{d=0}^{3} Q_{i,d} \\, K_{j,d}}{\\sqrt{4}}"))

O : Float

value · line 33
(O, tex4) ←  ᵉb̲oth< O_{i,k} = \sum_{j=0}^{1} S_{i,j} \, V_{j,k}
ᵉb̲oth< expands to
((({ @ →
  g24:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g23: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 ")"
  }
  g22: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
  }
  g25:grid ← 2 c̲at 3
  g26:g̲rid ← { k → g25:grid r̲eshape (o̲ffsets k s̲elect g25:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g25:grid) r̲eshape 0 }
  g25:grid r̲eshape (({ @ →
  g27:grid ← 2 c̲at 3 c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..1" } (1 + 1 − 0))
  g28:g̲rid ← { k → g27:grid r̲eshape (o̲ffsets k s̲elect g27:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g27:grid) r̲eshape 0 }
  '+ r̲/₃ g27:grid r̲eshape ((
    ((g22:r̲ank (S, "S", 2, "S[i,j]")) g24:a̲t ((
      ((S, "S", 2, "j") g23:c̲heck (0 + g28:g̲rid 3)) + (2 s̲elect s̲hape S) × 
      ((S, "S", 1, "i") g23:c̲heck (0 + g28:g̲rid 1))))) × 
    ((g22:r̲ank (V, "V", 2, "V[j,k]")) g24:a̲t ((
      ((V, "V", 2, "k") g23:c̲heck (0 + g28:g̲rid 2)) + (2 s̲elect s̲hape V) × 
      ((V, "V", 1, "j") g23:c̲heck (0 + g28:g̲rid 3)))))))
} @))
} @), "O_{i,k} = \\sum_{j=0}^{1} S_{i,j} \\, V_{j,k}"))

tex4 : Char

value · line 33
(O, tex4) ←  ᵉb̲oth< O_{i,k} = \sum_{j=0}^{1} S_{i,j} \, V_{j,k}
ᵉb̲oth< expands to
((({ @ →
  g24:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g23: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 ")"
  }
  g22: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
  }
  g25:grid ← 2 c̲at 3
  g26:g̲rid ← { k → g25:grid r̲eshape (o̲ffsets k s̲elect g25:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g25:grid) r̲eshape 0 }
  g25:grid r̲eshape (({ @ →
  g27:grid ← 2 c̲at 3 c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..1" } (1 + 1 − 0))
  g28:g̲rid ← { k → g27:grid r̲eshape (o̲ffsets k s̲elect g27:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g27:grid) r̲eshape 0 }
  '+ r̲/₃ g27:grid r̲eshape ((
    ((g22:r̲ank (S, "S", 2, "S[i,j]")) g24:a̲t ((
      ((S, "S", 2, "j") g23:c̲heck (0 + g28:g̲rid 3)) + (2 s̲elect s̲hape S) × 
      ((S, "S", 1, "i") g23:c̲heck (0 + g28:g̲rid 1))))) × 
    ((g22:r̲ank (V, "V", 2, "V[j,k]")) g24:a̲t ((
      ((V, "V", 2, "k") g23:c̲heck (0 + g28:g̲rid 2)) + (2 s̲elect s̲hape V) × 
      ((V, "V", 1, "j") g23:c̲heck (0 + g28:g̲rid 3)))))))
} @))
} @), "O_{i,k} = \\sum_{j=0}^{1} S_{i,j} \\, V_{j,k}"))

Softmax: p[i] = exp(z[i]) / sum_j exp(z[j])

logits : Float

value · line 39
logits ← f̲loat 1 2 3

p : Float

value · line 40
(p, tex5) ←  ᵉb̲oth< p_{i} = \frac{\exp(logits_{i})}{\sum_{j=0}^{2} \exp(logits_{j})}
ᵉb̲oth< expands to
((({ @ →
  g31:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g30: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 ")"
  }
  g29: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
  }
  g32:grid ← 3
  g33:g̲rid ← { k → g32:grid r̲eshape (o̲ffsets k s̲elect g32:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g32:grid) r̲eshape 0 }
  g32:grid r̲eshape (((e̲xp 
    ((g29:r̲ank (logits, "logits", 1, "logits[i]")) g31:a̲t (
      ((logits, "logits", 1, "i") g30:c̲heck (0 + g33:g̲rid 1))))) ÷ ({ @ →
  g34:grid ← 3 c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0))
  g35:g̲rid ← { k → g34:grid r̲eshape (o̲ffsets k s̲elect g34:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g34:grid) r̲eshape 0 }
  '+ r̲/₂ g34:grid r̲eshape ((e̲xp 
    ((g29:r̲ank (logits, "logits", 1, "logits[j]")) g31:a̲t (
      ((logits, "logits", 1, "j") g30:c̲heck (0 + g35:g̲rid 2))))))
} @)))
} @), "p_{i} = \\frac{\\exp(logits_{i})}{\\sum_{j=0}^{2} \\exp(logits_{j})}"))

tex5 : Char

value · line 40
(p, tex5) ←  ᵉb̲oth< p_{i} = \frac{\exp(logits_{i})}{\sum_{j=0}^{2} \exp(logits_{j})}
ᵉb̲oth< expands to
((({ @ →
  g31:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g30: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 ")"
  }
  g29: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
  }
  g32:grid ← 3
  g33:g̲rid ← { k → g32:grid r̲eshape (o̲ffsets k s̲elect g32:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g32:grid) r̲eshape 0 }
  g32:grid r̲eshape (((e̲xp 
    ((g29:r̲ank (logits, "logits", 1, "logits[i]")) g31:a̲t (
      ((logits, "logits", 1, "i") g30:c̲heck (0 + g33:g̲rid 1))))) ÷ ({ @ →
  g34:grid ← 3 c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0))
  g35:g̲rid ← { k → g34:grid r̲eshape (o̲ffsets k s̲elect g34:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g34:grid) r̲eshape 0 }
  '+ r̲/₂ g34:grid r̲eshape ((e̲xp 
    ((g29:r̲ank (logits, "logits", 1, "logits[j]")) g31:a̲t (
      ((logits, "logits", 1, "j") g30:c̲heck (0 + g35:g̲rid 2))))))
} @)))
} @), "p_{i} = \\frac{\\exp(logits_{i})}{\\sum_{j=0}^{2} \\exp(logits_{j})}"))

Cross-entropy: L = -sum_i y[i] log(p[i])

y : Float

value · line 46
y ← f̲loat 0 0 1

ce : Float

value · line 47
(ce, tex6) ←  ᵉb̲oth< L = -\left(\sum_{i=0}^{2} y_{i} \, \log(p_{i})\right)
ᵉb̲oth< expands to
((({ @ →
  g38:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g37: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 ")"
  }
  g36: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
  }
  ((0 − ({ @ →
  g41:grid ← ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0))
  g42:g̲rid ← { k → g41:grid r̲eshape (o̲ffsets k s̲elect g41:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g41:grid) r̲eshape 0 }
  '+ r̲/₁ g41:grid r̲eshape ((
    ((g36:r̲ank (y, "y", 1, "y[i]")) g38:a̲t (
      ((y, "y", 1, "i") g37:c̲heck (0 + g42:g̲rid 1)))) × (l̲og 
    ((g36:r̲ank (p, "p", 1, "p[i]")) g38:a̲t (
      ((p, "p", 1, "i") g37:c̲heck (0 + g42:g̲rid 1)))))))
} @)))
} @), "L = -\\left(\\sum_{i=0}^{2} y_{i} \\, \\log(p_{i})\\right)"))

tex6 : Char

value · line 47
(ce, tex6) ←  ᵉb̲oth< L = -\left(\sum_{i=0}^{2} y_{i} \, \log(p_{i})\right)
ᵉb̲oth< expands to
((({ @ →
  g38:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g37: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 ")"
  }
  g36: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
  }
  ((0 − ({ @ →
  g41:grid ← ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0))
  g42:g̲rid ← { k → g41:grid r̲eshape (o̲ffsets k s̲elect g41:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g41:grid) r̲eshape 0 }
  '+ r̲/₁ g41:grid r̲eshape ((
    ((g36:r̲ank (y, "y", 1, "y[i]")) g38:a̲t (
      ((y, "y", 1, "i") g37:c̲heck (0 + g42:g̲rid 1)))) × (l̲og 
    ((g36:r̲ank (p, "p", 1, "p[i]")) g38:a̲t (
      ((p, "p", 1, "i") g37:c̲heck (0 + g42:g̲rid 1)))))))
} @)))
} @), "L = -\\left(\\sum_{i=0}^{2} y_{i} \\, \\log(p_{i})\\right)"))

Mean squared error: L = (1/n) sum_i (y[i] - p[i])^2

yy : Float

value · line 53
yy ← f̲loat 1 2 3 4

pp : Float

value · line 54
pp ← 1.5 2 2 4

mse : Float

value · line 55
(mse, tex7) ←  ᵉb̲oth< L = \frac{\sum_{i=0}^{3} \left(yy_{i} - pp_{i}\right)^{2}}{4}
ᵉb̲oth< expands to
((({ @ →
  g45:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g44: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 ")"
  }
  g43: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
  }
  ((({ @ →
  g48:grid ← ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..3" } (1 + 3 − 0))
  g49:g̲rid ← { k → g48:grid r̲eshape (o̲ffsets k s̲elect g48:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g48:grid) r̲eshape 0 }
  '+ r̲/₁ g48:grid r̲eshape ((((
    ((g43:r̲ank (yy, "yy", 1, "yy[i]")) g45:a̲t (
      ((yy, "yy", 1, "i") g44:c̲heck (0 + g49:g̲rid 1)))) − 
    ((g43:r̲ank (pp, "pp", 1, "pp[i]")) g45:a̲t (
      ((pp, "pp", 1, "i") g44:c̲heck (0 + g49:g̲rid 1)))))) ^ 2))
} @) ÷ 4))
} @), "L = \\frac{\\sum_{i=0}^{3} \\left(yy_{i} - pp_{i}\\right)^{2}}{4}"))

tex7 : Char

value · line 55
(mse, tex7) ←  ᵉb̲oth< L = \frac{\sum_{i=0}^{3} \left(yy_{i} - pp_{i}\right)^{2}}{4}
ᵉb̲oth< expands to
((({ @ →
  g45:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g44: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 ")"
  }
  g43: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
  }
  ((({ @ →
  g48:grid ← ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..3" } (1 + 3 − 0))
  g49:g̲rid ← { k → g48:grid r̲eshape (o̲ffsets k s̲elect g48:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g48:grid) r̲eshape 0 }
  '+ r̲/₁ g48:grid r̲eshape ((((
    ((g43:r̲ank (yy, "yy", 1, "yy[i]")) g45:a̲t (
      ((yy, "yy", 1, "i") g44:c̲heck (0 + g49:g̲rid 1)))) − 
    ((g43:r̲ank (pp, "pp", 1, "pp[i]")) g45:a̲t (
      ((pp, "pp", 1, "i") g44:c̲heck (0 + g49:g̲rid 1)))))) ^ 2))
} @) ÷ 4))
} @), "L = \\frac{\\sum_{i=0}^{3} \\left(yy_{i} - pp_{i}\\right)^{2}}{4}"))

Layer norm's mean and variance: mu = (1/n) sum_i x[i], var = (1/n) sum_i (x[i] - mu)^2

h : Float

value · line 61
h ← f̲loat 1 2 3 4
Used in: mu, var

mu : Float

value · line 62
(mu, tex8) ←  ᵉb̲oth< \mu = \frac{\sum_{i=0}^{3} h_{i}}{4}
ᵉb̲oth< expands to
((({ @ →
  g52:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g51: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 ")"
  }
  g50: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
  }
  ((({ @ →
  g55:grid ← ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..3" } (1 + 3 − 0))
  g56:g̲rid ← { k → g55:grid r̲eshape (o̲ffsets k s̲elect g55:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g55:grid) r̲eshape 0 }
  '+ r̲/₁ g55:grid r̲eshape (
    ((g50:r̲ank (h, "h", 1, "h[i]")) g52:a̲t (
      ((h, "h", 1, "i") g51:c̲heck (0 + g56:g̲rid 1)))))
} @) ÷ 4))
} @), "\\mu = \\frac{\\sum_{i=0}^{3} h_{i}}{4}"))

tex8 : Char

value · line 62
(mu, tex8) ←  ᵉb̲oth< \mu = \frac{\sum_{i=0}^{3} h_{i}}{4}
ᵉb̲oth< expands to
((({ @ →
  g52:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g51: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 ")"
  }
  g50: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
  }
  ((({ @ →
  g55:grid ← ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..3" } (1 + 3 − 0))
  g56:g̲rid ← { k → g55:grid r̲eshape (o̲ffsets k s̲elect g55:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g55:grid) r̲eshape 0 }
  '+ r̲/₁ g55:grid r̲eshape (
    ((g50:r̲ank (h, "h", 1, "h[i]")) g52:a̲t (
      ((h, "h", 1, "i") g51:c̲heck (0 + g56:g̲rid 1)))))
} @) ÷ 4))
} @), "\\mu = \\frac{\\sum_{i=0}^{3} h_{i}}{4}"))

var : Float

value · line 64
(var, tex9) ←  ᵉb̲oth< var = \frac{\sum_{i=0}^{3} \left(h_{i} - \mu\right)^{2}}{4}
ᵉb̲oth< expands to
((({ @ →
  g59:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g58: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 ")"
  }
  g57: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
  }
  ((({ @ →
  g62:grid ← ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..3" } (1 + 3 − 0))
  g63:g̲rid ← { k → g62:grid r̲eshape (o̲ffsets k s̲elect g62:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g62:grid) r̲eshape 0 }
  '+ r̲/₁ g62:grid r̲eshape ((((
    ((g57:r̲ank (h, "h", 1, "h[i]")) g59:a̲t (
      ((h, "h", 1, "i") g58:c̲heck (0 + g63:g̲rid 1)))) − mu)) ^ 2))
} @) ÷ 4))
} @), "var = \\frac{\\sum_{i=0}^{3} \\left(h_{i} - \\mu\\right)^{2}}{4}"))

tex9 : Char

value · line 64
(var, tex9) ←  ᵉb̲oth< var = \frac{\sum_{i=0}^{3} \left(h_{i} - \mu\right)^{2}}{4}
ᵉb̲oth< expands to
((({ @ →
  g59:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g58: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 ")"
  }
  g57: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
  }
  ((({ @ →
  g62:grid ← ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..3" } (1 + 3 − 0))
  g63:g̲rid ← { k → g62:grid r̲eshape (o̲ffsets k s̲elect g62:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g62:grid) r̲eshape 0 }
  '+ r̲/₁ g62:grid r̲eshape ((((
    ((g57:r̲ank (h, "h", 1, "h[i]")) g59:a̲t (
      ((h, "h", 1, "i") g58:c̲heck (0 + g63:g̲rid 1)))) − mu)) ^ 2))
} @) ÷ 4))
} @), "var = \\frac{\\sum_{i=0}^{3} \\left(h_{i} - \\mu\\right)^{2}}{4}"))

Cosine similarity: cos = sum_i a[i] b[i] / (sqrt(sum_i a[i]^2) sqrt(sum_i b[i]^2))

a : Float

value · line 70
a ← f̲loat 1 0 1

bb : Float

value · line 71
bb ← f̲loat 1 1 0

cosine : Float

value · line 72
(cosine, tex10) ←  ᵉb̲oth< c = \frac{\sum_{i=0}^{2} a_{i} \, bb_{i}}{\sqrt{\sum_{i=0}^{2} a_{i}^{2}} \, \sqrt{\sum_{i=0}^{2} bb_{i}^{2}}}
ᵉb̲oth< expands to
((({ @ →
  g66:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g65: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 ")"
  }
  g64: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
  }
  ((({ @ →
  g69:grid ← ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0))
  g70:g̲rid ← { k → g69:grid r̲eshape (o̲ffsets k s̲elect g69:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g69:grid) r̲eshape 0 }
  '+ r̲/₁ g69:grid r̲eshape ((
    ((g64:r̲ank (a, "a", 1, "a[i]")) g66:a̲t (
      ((a, "a", 1, "i") g65:c̲heck (0 + g70:g̲rid 1)))) × 
    ((g64:r̲ank (bb, "bb", 1, "bb[i]")) g66:a̲t (
      ((bb, "bb", 1, "i") g65:c̲heck (0 + g70:g̲rid 1))))))
} @) ÷ (((({ @ →
  g71:grid ← ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0))
  g72:g̲rid ← { k → g71:grid r̲eshape (o̲ffsets k s̲elect g71:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g71:grid) r̲eshape 0 }
  '+ r̲/₁ g71:grid r̲eshape ((
    ((g64:r̲ank (a, "a", 1, "a[i]")) g66:a̲t (
      ((a, "a", 1, "i") g65:c̲heck (0 + g72:g̲rid 1)))) ^ 2))
} @) ^ 0.5) × (({ @ →
  g73:grid ← ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0))
  g74:g̲rid ← { k → g73:grid r̲eshape (o̲ffsets k s̲elect g73:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g73:grid) r̲eshape 0 }
  '+ r̲/₁ g73:grid r̲eshape ((
    ((g64:r̲ank (bb, "bb", 1, "bb[i]")) g66:a̲t (
      ((bb, "bb", 1, "i") g65:c̲heck (0 + g74:g̲rid 1)))) ^ 2))
} @) ^ 0.5)))))
} @), "c = \\frac{\\sum_{i=0}^{2} a_{i} \\, bb_{i}}{\\sqrt{\\sum_{i=0}^{2} a_{i}^{2}} \\, \\sqrt{\\sum_{i=0}^{2} bb_{i}^{2}}}"))

tex10 : Char

value · line 72
(cosine, tex10) ←  ᵉb̲oth< c = \frac{\sum_{i=0}^{2} a_{i} \, bb_{i}}{\sqrt{\sum_{i=0}^{2} a_{i}^{2}} \, \sqrt{\sum_{i=0}^{2} bb_{i}^{2}}}
ᵉb̲oth< expands to
((({ @ →
  g66:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g65: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 ")"
  }
  g64: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
  }
  ((({ @ →
  g69:grid ← ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0))
  g70:g̲rid ← { k → g69:grid r̲eshape (o̲ffsets k s̲elect g69:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g69:grid) r̲eshape 0 }
  '+ r̲/₁ g69:grid r̲eshape ((
    ((g64:r̲ank (a, "a", 1, "a[i]")) g66:a̲t (
      ((a, "a", 1, "i") g65:c̲heck (0 + g70:g̲rid 1)))) × 
    ((g64:r̲ank (bb, "bb", 1, "bb[i]")) g66:a̲t (
      ((bb, "bb", 1, "i") g65:c̲heck (0 + g70:g̲rid 1))))))
} @) ÷ (((({ @ →
  g71:grid ← ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0))
  g72:g̲rid ← { k → g71:grid r̲eshape (o̲ffsets k s̲elect g71:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g71:grid) r̲eshape 0 }
  '+ r̲/₁ g71:grid r̲eshape ((
    ((g64:r̲ank (a, "a", 1, "a[i]")) g66:a̲t (
      ((a, "a", 1, "i") g65:c̲heck (0 + g72:g̲rid 1)))) ^ 2))
} @) ^ 0.5) × (({ @ →
  g73:grid ← ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0))
  g74:g̲rid ← { k → g73:grid r̲eshape (o̲ffsets k s̲elect g73:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g73:grid) r̲eshape 0 }
  '+ r̲/₁ g73:grid r̲eshape ((
    ((g64:r̲ank (bb, "bb", 1, "bb[i]")) g66:a̲t (
      ((bb, "bb", 1, "i") g65:c̲heck (0 + g74:g̲rid 1)))) ^ 2))
} @) ^ 0.5)))))
} @), "c = \\frac{\\sum_{i=0}^{2} a_{i} \\, bb_{i}}{\\sqrt{\\sum_{i=0}^{2} a_{i}^{2}} \\, \\sqrt{\\sum_{i=0}^{2} bb_{i}^{2}}}"))

An L2 penalty: R = lambda sum_i w[i]^2

weights : Float

value · line 78
weights ← f̲loat 1 -2 3

l2 : Float

value · line 79
(l2, tex11) ←  ᵉb̲oth< R = 0.5 \, \left(\sum_{i=0}^{2} weights_{i}^{2}\right)
ᵉb̲oth< expands to
((({ @ →
  g77:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g76: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 ")"
  }
  g75: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
  }
  ((0.5 × ({ @ →
  g80:grid ← ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0))
  g81:g̲rid ← { k → g80:grid r̲eshape (o̲ffsets k s̲elect g80:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g80:grid) r̲eshape 0 }
  '+ r̲/₁ g80:grid r̲eshape ((
    ((g75:r̲ank (weights, "weights", 1, "weights[i]")) g77:a̲t (
      ((weights, "weights", 1, "i") g76:c̲heck (0 + g81:g̲rid 1)))) ^ 2))
} @)))
} @), "R = 0.5 \\, \\left(\\sum_{i=0}^{2} weights_{i}^{2}\\right)"))

tex11 : Char

value · line 79
(l2, tex11) ←  ᵉb̲oth< R = 0.5 \, \left(\sum_{i=0}^{2} weights_{i}^{2}\right)
ᵉb̲oth< expands to
((({ @ →
  g77:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g76: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 ")"
  }
  g75: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
  }
  ((0.5 × ({ @ →
  g80:grid ← ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0))
  g81:g̲rid ← { k → g80:grid r̲eshape (o̲ffsets k s̲elect g80:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g80:grid) r̲eshape 0 }
  '+ r̲/₁ g80:grid r̲eshape ((
    ((g75:r̲ank (weights, "weights", 1, "weights[i]")) g77:a̲t (
      ((weights, "weights", 1, "i") g76:c̲heck (0 + g81:g̲rid 1)))) ^ 2))
} @)))
} @), "R = 0.5 \\, \\left(\\sum_{i=0}^{2} weights_{i}^{2}\\right)"))

Max pooling, 2 by 2 windows: P[o,h,w] = max_r max_s X[o,2h+r,2w+s]

X : Int

value · line 85
X ← 1 4 4 r̲eshape r̲ange 16
Used in: P

P : Int

value · line 86
(P, tex12) ←  ᵉb̲oth< P_{o,h,w} = \max_{r=0}^{1} \max_{s=0}^{1} X_{o,2\,h+r,2\,w+s}
ᵉb̲oth< expands to
((({ @ →
  g84:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g83: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 ")"
  }
  g82: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
  }
  g85:grid ← 1 c̲at 2 c̲at 2
  g86:g̲rid ← { k → g85:grid r̲eshape (o̲ffsets k s̲elect g85:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g85:grid) r̲eshape 0 }
  g85:grid r̲eshape (({ @ →
  g87:grid ← 1 c̲at 2 c̲at 2 c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "max's range runs low..high: 0..1" } (1 + 1 − 0)) c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "max's range runs low..high: 0..1" } (1 + 1 − 0))
  g88:g̲rid ← { k → g87:grid r̲eshape (o̲ffsets k s̲elect g87:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g87:grid) r̲eshape 0 }
  'm̲ax r̲/₄ 'm̲ax r̲/₅ g87:grid r̲eshape (
    ((g82:r̲ank (X, "X", 3, "X[o,2*h+r,2*w+s]")) g84:a̲t ((
      ((X, "X", 3, "2*w+s") g83:c̲heck ((2 × (0 + g88:g̲rid 3)) + (0 + g88:g̲rid 5))) + (3 s̲elect s̲hape X) × (
      ((X, "X", 2, "2*h+r") g83:c̲heck ((2 × (0 + g88:g̲rid 2)) + (0 + g88:g̲rid 4))) + (2 s̲elect s̲hape X) × 
      ((X, "X", 1, "o") g83:c̲heck (0 + g88:g̲rid 1)))))))
} @))
} @), "P_{o,h,w} = \\max_{r=0}^{1} \\max_{s=0}^{1} X_{o,2\\,h+r,2\\,w+s}"))

tex12 : Char

value · line 86
(P, tex12) ←  ᵉb̲oth< P_{o,h,w} = \max_{r=0}^{1} \max_{s=0}^{1} X_{o,2\,h+r,2\,w+s}
ᵉb̲oth< expands to
((({ @ →
  g84:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g83: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 ")"
  }
  g82: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
  }
  g85:grid ← 1 c̲at 2 c̲at 2
  g86:g̲rid ← { k → g85:grid r̲eshape (o̲ffsets k s̲elect g85:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g85:grid) r̲eshape 0 }
  g85:grid r̲eshape (({ @ →
  g87:grid ← 1 c̲at 2 c̲at 2 c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "max's range runs low..high: 0..1" } (1 + 1 − 0)) c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "max's range runs low..high: 0..1" } (1 + 1 − 0))
  g88:g̲rid ← { k → g87:grid r̲eshape (o̲ffsets k s̲elect g87:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g87:grid) r̲eshape 0 }
  'm̲ax r̲/₄ 'm̲ax r̲/₅ g87:grid r̲eshape (
    ((g82:r̲ank (X, "X", 3, "X[o,2*h+r,2*w+s]")) g84:a̲t ((
      ((X, "X", 3, "2*w+s") g83:c̲heck ((2 × (0 + g88:g̲rid 3)) + (0 + g88:g̲rid 5))) + (3 s̲elect s̲hape X) × (
      ((X, "X", 2, "2*h+r") g83:c̲heck ((2 × (0 + g88:g̲rid 2)) + (0 + g88:g̲rid 4))) + (2 s̲elect s̲hape X) × 
      ((X, "X", 1, "o") g83:c̲heck (0 + g88:g̲rid 1)))))))
} @))
} @), "P_{o,h,w} = \\max_{r=0}^{1} \\max_{s=0}^{1} X_{o,2\\,h+r,2\\,w+s}"))

A 1-D convolution: y[n] = sum_m x[n-m] h[m], valid mode (the layer's cross-correlation is the kernel unreversed)

xs : Int

value · line 92
xs ← 1 2 3 4

hk : Int

value · line 93
hk ← 1 2 3

M : Int

value · line 94
(M, L) ← (3, 4)
Used in: M, yc

L : Int

value · line 94
(M, L) ← (3, 4)
Used in: M, yc

yc : Int

value · line 95
(yc, tex13) ←  ᵉb̲oth< y_{n} = \sum_{m=0}^{M - 1} xs_{n+M-1-m} \, hk_{m}
ᵉb̲oth< expands to
((({ @ →
  g91:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g90: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 ")"
  }
  g89: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
  }
  g92:grid ← ((L − M) + 1)
  g93:g̲rid ← { k → g92:grid r̲eshape (o̲ffsets k s̲elect g92:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g92:grid) r̲eshape 0 }
  g92:grid r̲eshape (({ @ →
  g94:grid ← ((L − M) + 1) c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..M-1" } (1 + (M − 1) − 0))
  g95:g̲rid ← { k → g94:grid r̲eshape (o̲ffsets k s̲elect g94:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g94:grid) r̲eshape 0 }
  '+ r̲/₂ g94:grid r̲eshape ((
    ((g89:r̲ank (xs, "xs", 1, "xs[n+M-1-m]")) g91:a̲t (
      ((xs, "xs", 1, "n+M-1-m") g90:c̲heck ((((0 + g95:g̲rid 1) + M) − 1) − (0 + g95:g̲rid 2))))) × 
    ((g89:r̲ank (hk, "hk", 1, "hk[m]")) g91:a̲t (
      ((hk, "hk", 1, "m") g90:c̲heck (0 + g95:g̲rid 2))))))
} @))
} @), "y_{n} = \\sum_{m=0}^{M - 1} xs_{n+M-1-m} \\, hk_{m}"))

tex13 : Char

value · line 95
(yc, tex13) ←  ᵉb̲oth< y_{n} = \sum_{m=0}^{M - 1} xs_{n+M-1-m} \, hk_{m}
ᵉb̲oth< expands to
((({ @ →
  g91:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g90: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 ")"
  }
  g89: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
  }
  g92:grid ← ((L − M) + 1)
  g93:g̲rid ← { k → g92:grid r̲eshape (o̲ffsets k s̲elect g92:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g92:grid) r̲eshape 0 }
  g92:grid r̲eshape (({ @ →
  g94:grid ← ((L − M) + 1) c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..M-1" } (1 + (M − 1) − 0))
  g95:g̲rid ← { k → g94:grid r̲eshape (o̲ffsets k s̲elect g94:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g94:grid) r̲eshape 0 }
  '+ r̲/₂ g94:grid r̲eshape ((
    ((g89:r̲ank (xs, "xs", 1, "xs[n+M-1-m]")) g91:a̲t (
      ((xs, "xs", 1, "n+M-1-m") g90:c̲heck ((((0 + g95:g̲rid 1) + M) − 1) − (0 + g95:g̲rid 2))))) × 
    ((g89:r̲ank (hk, "hk", 1, "hk[m]")) g91:a̲t (
      ((hk, "hk", 1, "m") g90:c̲heck (0 + g95:g̲rid 2))))))
} @))
} @), "y_{n} = \\sum_{m=0}^{M - 1} xs_{n+M-1-m} \\, hk_{m}"))

GELU: x Phi(x), Phi(x) = 1/2 + integral_0^x exp(-u^2/2) / sqrt(2 pi) du (Hendrycks and Gimpel, 2016)

pi : Float

value · line 102
pi ← 3.141592653589793

ᵘg̲elu : Float -> Float

function · line 103
ᵘg̲elu ← { x → x ×  ᵉe̲q< \Phi = \frac{1}{2} + \left(\int_{0}^{x} \frac{\exp(-\frac{u^{2}}{2})}{\sqrt{2 \, \pi}} \, du\right) }
ᵉe̲q< expands to
(({ @ →
  g109:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g108: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 ")"
  }
  g107: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
  }
  (((1 ÷ 2) + ({ @ →
  g115:a ← f̲loat (0)
  g114:b ← f̲loat (x)
  g113:h ← (g114:b − g115:a) ÷ 32.0
  g112:w ← g113:h × (1.0 c̲at (31 r̲eshape 4.0 2.0) c̲at 1.0) ÷ 3.0
  g116:grid ← (33)
  g117:g̲rid ← { k → g116:grid r̲eshape (o̲ffsets k s̲elect g116:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g116:grid) r̲eshape 0 }
  '+ r̲/₁ g116:grid r̲eshape ((1 + g117:g̲rid 1) s̲elect g112:w) × (((e̲xp (0 − ((((g115:a + g113:h × f̲loat g117:g̲rid 1) ^ 2)) ÷ 2))) ÷ ((2 × pi) ^ 0.5)))
} @)))
} @))
Used in: gelu

tex14 : Char

value · line 104
tex14 ← "Phi[] | " ᵉt̲ex< "1 / 2 + (integral u:=0..x | exp(-(u^2) / 2) / sqrt(2 * pi))"
ᵉt̲ex< expands to
("\\Phi = \\frac{1}{2} + \\left(\\int_{0}^{x} \\frac{\\exp(-\\frac{u^{2}}{2})}{\\sqrt{2 \\, \\pi}} \\, du\\right)")

gelu : Float

value · line 106
gelu ← 'ᵘg̲elu e̲ach -2.0 -1.0 0.0 1.0 2.0

ᵘt̲anh : Num a => a -> Float

function · line 109

The tanh approximation the frameworks compute agrees to 5e-4 (its largest error over -4..4 is 0.000473).

ᵘt̲anh ← { z → ((e̲xp z) − e̲xp n̲eg z) ÷ (e̲xp z) + e̲xp n̲eg z }
Used in: ᵘa̲pprox

ᵘa̲pprox : Float -> Float

function · line 110
ᵘa̲pprox ← { x → 0.5 × x × 1.0 + ᵘt̲anh ((2.0 ÷ pi) ^ 0.5) × x + 0.044715 × x ^ 3 }

KL divergence of two Gaussians: KL(p || q) = integral p(x) log(p(x) / q(x)) dx, against its closed form

m1 : Float

value · line 114
(m1, s1, m2, s2) ← (0.0, 1.0, 1.0, 2.0)
Used in: m1, kl, closed

s1 : Float

value · line 114
(m1, s1, m2, s2) ← (0.0, 1.0, 1.0, 2.0)
Used in: m1, kl, closed

m2 : Float

value · line 114
(m1, s1, m2, s2) ← (0.0, 1.0, 1.0, 2.0)
Used in: m1, kl, closed

s2 : Float

value · line 114
(m1, s1, m2, s2) ← (0.0, 1.0, 1.0, 2.0)
Used in: m1, kl, closed

kl : Float

value · line 115
(kl, tex15) ←  ᵉb̲oth< KL = \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
ᵉb̲oth< expands to
((({ @ →
  g120:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g119: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 ")"
  }
  g118: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
  }
  (({ @ →
  g126:a ← f̲loat ((0 − 10))
  g125:b ← f̲loat (10)
  g124:h ← (g125:b − g126:a) ÷ 400.0
  g123:w ← g124:h × (1.0 c̲at (399 r̲eshape 4.0 2.0) c̲at 1.0) ÷ 3.0
  g127:grid ← (401)
  g128:g̲rid ← { k → g127:grid r̲eshape (o̲ffsets k s̲elect g127:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g127:grid) r̲eshape 0 }
  '+ r̲/₁ g127:grid r̲eshape ((1 + g128:g̲rid 1) s̲elect g123:w) × (((((e̲xp (0 − ((((((g126:a + g124:h × f̲loat g128:g̲rid 1) − m1)) ^ 2)) ÷ ((2 × (s1 ^ 2)))))) ÷ ((s1 × ((2 × pi) ^ 0.5))))) × (l̲og ((((e̲xp (0 − ((((((g126:a + g124:h × f̲loat g128:g̲rid 1) − m1)) ^ 2)) ÷ ((2 × (s1 ^ 2)))))) ÷ ((s1 × ((2 × pi) ^ 0.5))))) ÷ (((e̲xp (0 − ((((((g126:a + g124:h × f̲loat g128:g̲rid 1) − m2)) ^ 2)) ÷ ((2 × (s2 ^ 2)))))) ÷ ((s2 × ((2 × pi) ^ 0.5)))))))))
} @))
} @), "KL = \\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"))

tex15 : Char

value · line 115
(kl, tex15) ←  ᵉb̲oth< KL = \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
ᵉb̲oth< expands to
((({ @ →
  g120:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g119: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 ")"
  }
  g118: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
  }
  (({ @ →
  g126:a ← f̲loat ((0 − 10))
  g125:b ← f̲loat (10)
  g124:h ← (g125:b − g126:a) ÷ 400.0
  g123:w ← g124:h × (1.0 c̲at (399 r̲eshape 4.0 2.0) c̲at 1.0) ÷ 3.0
  g127:grid ← (401)
  g128:g̲rid ← { k → g127:grid r̲eshape (o̲ffsets k s̲elect g127:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g127:grid) r̲eshape 0 }
  '+ r̲/₁ g127:grid r̲eshape ((1 + g128:g̲rid 1) s̲elect g123:w) × (((((e̲xp (0 − ((((((g126:a + g124:h × f̲loat g128:g̲rid 1) − m1)) ^ 2)) ÷ ((2 × (s1 ^ 2)))))) ÷ ((s1 × ((2 × pi) ^ 0.5))))) × (l̲og ((((e̲xp (0 − ((((((g126:a + g124:h × f̲loat g128:g̲rid 1) − m1)) ^ 2)) ÷ ((2 × (s1 ^ 2)))))) ÷ ((s1 × ((2 × pi) ^ 0.5))))) ÷ (((e̲xp (0 − ((((((g126:a + g124:h × f̲loat g128:g̲rid 1) − m2)) ^ 2)) ÷ ((2 × (s2 ^ 2)))))) ÷ ((s2 × ((2 × pi) ^ 0.5)))))))))
} @))
} @), "KL = \\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"))

closed : Float

value · line 118
closed ← (l̲og s2 ÷ s1) + ((s1² + (m1 − m2)²) ÷ 2.0 × s2²) − 0.5