sourcedemos/pipelines/rosetta.xtl

1⍝!/usr/bin/env xetal 2⍝│ One CNN layer four ways, a Rosetta Stone: as a paper prints it (an 3⍝│ equation, read by X_eTaL-libraries' Equations), as a pipeline (Flow), 4⍝│ and as plain X_eTaL (this repository's Conv). The pipeline's 5⍝│ expansion, the fourth way, is what xetal expand rosetta.xtl shows. 6⍝│ All give the same feature maps. 7 8"e:" u̲se< "Equations" 9"flow:" u̲se< "Flow" 10"cv:" u̲se< "Conv" 11"nn:" u̲se< "NN" 12"act:" u̲se< "Activations" 13"k:" u̲se< "Check" 14 15⍝━━ Data 16 17⍝│ One 5 x 5 picture (one channel: a bright 2 x 2 square on a dim 18⍝│ field), two 3 x 3 filters (a vertical edge, a center-surround) and 19⍝│ their biases. 20m ← 0.0 1.0 1.0 0.0 0.0 21P ← 0.1 + 0.8 × m '× t̲able m 22F ← 2 3 3 r̲eshape 1.0 0.0 -1.0 2.0 0.0 -2.0 1.0 0.0 -1.0 0.0 1.0 0.0 1.0 -4.0 1.0 0.0 1.0 0.0 23B ← 0.5 -0.25 24O ← 2 25Ho ← 3 26Wo ← 3 27 28⍝━━ As a paper prints it 29 30⍝│ Y[o,h,w] = max(sum over r, s of P[h+r, w+s] F[o,r,s], plus B[o], 0): 31⍝│ its value, then its LaTeX, from the same reading of the same text. 32eq ← ᵉe̲q< Y_{o,h,w} = \max(\left(\sum_{r=0}^{2} \sum_{s=0}^{2} P_{h+r,w+s} \, F_{o,r,s}\right) + B_{o}, 0)
ᵉ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
  }
  g4:grid ← O c̲at Ho c̲at Wo
  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 ← O c̲at Ho c̲at Wo c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0)) 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̲/₄ '+ r̲/₅ g6:grid r̲eshape ((
    ((g1:r̲ank (P, "P", 2, "P[h+r,w+s]")) g3:a̲t ((
      ((P, "P", 2, "w+s") g2:c̲heck ((0 + g7:g̲rid 3) + (0 + g7:g̲rid 5))) + (2 s̲elect s̲hape P) × 
      ((P, "P", 1, "h+r") g2:c̲heck ((0 + g7:g̲rid 2) + (0 + g7:g̲rid 4)))))) × 
    ((g1:r̲ank (F, "F", 3, "F[o,r,s]")) g3:a̲t ((
      ((F, "F", 3, "s") g2:c̲heck (0 + g7:g̲rid 5)) + (3 s̲elect s̲hape F) × (
      ((F, "F", 2, "r") g2:c̲heck (0 + g7:g̲rid 4)) + (2 s̲elect s̲hape F) × 
      ((F, "F", 1, "o") g2:c̲heck (0 + g7:g̲rid 1))))))))
} @) + 
    ((g1:r̲ank (B, "B", 1, "B[o]")) g3:a̲t (
      ((B, "B", 1, "o") g2:c̲heck (0 + g5:g̲rid 1))))) m̲ax 0))
} @))
33"Y[o,h,w] | O Ho Wo" ᵉt̲ex< "max((sum r:=0..2, sum s:=0..2 | P[h+r,w+s] * F[o,r,s]) + B[o], 0)"
ᵉt̲ex< expands to
"Y_{o,h,w} = \\max(\\left(\\sum_{r=0}^{2} \\sum_{s=0}^{2} P_{h+r,w+s} \\, F_{o,r,s}\\right) + B_{o}, 0)"
34 35⍝│ The same as a pipeline of equations, a stage each: the convolution 36⍝│ with its bias, then the activation; drawn as math a row a stage. 37eqpipe ← @ ᶠˡᵒʷp̲ipe< c#"
ᶠˡᵒʷp̲ipe< expands to
({ @ →
  p ← P
  c ← "C[o,h,w] | O Ho Wo" ᵉe̲q< "(sum r:=0..2, sum s:=0..2 | p[h+r,w+s] * F[o,r,s]) + B[o]"
  _ ← { @ →
    ((s̲hape c) m̲atch r̲avel O c̲at Ho c̲at Wo) ? @
    "shape" ⎕S̲IGNAL "stage 2 (c): shape " c̲at (f̲ormat s̲hape c) c̲at ", expected " c̲at f̲ormat r̲avel O c̲at Ho c̲at Wo
  } @
  g8:stage ← "Y[o,h,w] | O Ho Wo" ᵉe̲q< "max(c[o,h,w], 0)"
  g8:stage
} @)
ᵉe̲q< expands to
(({ @ →
  g11:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g10: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 ")"
  }
  g9: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
  }
  g12:grid ← O c̲at Ho c̲at Wo
  g13:g̲rid ← { k → g12:grid r̲eshape (o̲ffsets k s̲elect g12:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g12:grid) r̲eshape 0 }
  g12:grid r̲eshape ((({ @ →
  g14:grid ← O c̲at Ho c̲at Wo c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0)) c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0))
  g15:g̲rid ← { k → g14:grid r̲eshape (o̲ffsets k s̲elect g14:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g14:grid) r̲eshape 0 }
  '+ r̲/₄ '+ r̲/₅ g14:grid r̲eshape ((
    ((g9:r̲ank (p, "p", 2, "p[h+r,w+s]")) g11:a̲t ((
      ((p, "p", 2, "w+s") g10:c̲heck ((0 + g15:g̲rid 3) + (0 + g15:g̲rid 5))) + (2 s̲elect s̲hape p) × 
      ((p, "p", 1, "h+r") g10:c̲heck ((0 + g15:g̲rid 2) + (0 + g15:g̲rid 4)))))) × 
    ((g9:r̲ank (F, "F", 3, "F[o,r,s]")) g11:a̲t ((
      ((F, "F", 3, "s") g10:c̲heck (0 + g15:g̲rid 5)) + (3 s̲elect s̲hape F) × (
      ((F, "F", 2, "r") g10:c̲heck (0 + g15:g̲rid 4)) + (2 s̲elect s̲hape F) × 
      ((F, "F", 1, "o") g10:c̲heck (0 + g15:g̲rid 1))))))))
} @) + 
    ((g9:r̲ank (B, "B", 1, "B[o]")) g11:a̲t (
      ((B, "B", 1, "o") g10:c̲heck (0 + g13:g̲rid 1))))))
} @))
ᵉe̲q< expands to
(({ @ →
  g18:a̲t ← { A i → (1 + i) s̲elect r̲avel A }
  g17: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 ")"
  }
  g16: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
  }
  g19:grid ← O c̲at Ho c̲at Wo
  g20:g̲rid ← { k → g19:grid r̲eshape (o̲ffsets k s̲elect g19:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g19:grid) r̲eshape 0 }
  g19:grid r̲eshape ((
    ((g16:r̲ank (c, "c", 3, "c[o,h,w]")) g18:a̲t ((
      ((c, "c", 3, "w") g17:c̲heck (0 + g20:g̲rid 3)) + (3 s̲elect s̲hape c) × (
      ((c, "c", 2, "h") g17:c̲heck (0 + g20:g̲rid 2)) + (2 s̲elect s̲hape c) × 
      ((c, "c", 1, "o") g17:c̲heck (0 + g20:g̲rid 1)))))) m̲ax 0))
} @))
38 P ▹p 39▷ "C[o,h,w] | O Ho Wo" ᵉe̲q< "(sum r:=0..2, sum s:=0..2 | p[h+r,w+s] * F[o,r,s]) + B[o]" ▹c [O Ho Wo] 40▷ "Y[o,h,w] | O Ho Wo" ᵉe̲q< "max(c[o,h,w], 0)""# 41drawn ← @ ᶠˡᵒʷm̲ath< c#"
ᶠˡᵒʷm̲ath< expands to
((r#"\begin{aligned} "# c̲at r#"& "# c̲at r#"P"# c̲at r#" \qquad \triangleright \, p \\ "# c̲at r#"\longrightarrow \; & "# c̲at ("C[o,h,w] | O Ho Wo" ᵉt̲ex< "(sum r:=0..2, sum s:=0..2 | p[h+r,w+s] * F[o,r,s]) + B[o]") c̲at r#" \qquad \triangleright \, c: O \times Ho \times Wo \\ "# c̲at r#"\longrightarrow \; & "# c̲at ("Y[o,h,w] | O Ho Wo" ᵉt̲ex< "max(c[o,h,w], 0)") c̲at r#""# c̲at r#" \end{aligned}"#))
ᵉt̲ex< expands to
("C_{o,h,w} = \\left(\\sum_{r=0}^{2} \\sum_{s=0}^{2} p_{h+r,w+s} \\, F_{o,r,s}\\right) + B_{o}")
ᵉt̲ex< expands to
("Y_{o,h,w} = \\max(c_{o,h,w}, 0)")
42 P ▹p 43▷ "C[o,h,w] | O Ho Wo" ᵉe̲q< "(sum r:=0..2, sum s:=0..2 | p[h+r,w+s] * F[o,r,s]) + B[o]" ▹c [O Ho Wo] 44▷ "Y[o,h,w] | O Ho Wo" ᵉe̲q< "max(c[o,h,w], 0)""# 45drawn 46 47⍝━━ As a pipeline 48 49⍝│ The windows of the picture, one column each; every filter times every 50⍝│ window (one matrix product); its bias along the filters' axis; the 51⍝│ maps shaped O x Ho x Wo; then the activation. 52pipe ← @ ᶠˡᵒʷp̲ipe< c"
ᶠˡᵒʷp̲ipe< expands to
({ @ →
  g21:stage ← 1 5 5 r̲eshape P
  g22:stage ← ᶜᵛw̲indows g21:stage
  g23:stage ← { @ →
    it ← g22:stage
    ((O c̲at 9) r̲eshape F) '+ '× i̲nner it
  } @
  g24:stage ← { @ →
    it ← g23:stage
    (1, B) ⁿⁿa̲ddAxis it
  } @
  Y ← { @ →
    it ← g24:stage
    (O c̲at Ho c̲at Wo) r̲eshape it
  } @
  _ ← { @ →
    ((s̲hape Y) m̲atch r̲avel O c̲at Ho c̲at Wo) ? @
    "shape" ⎕S̲IGNAL "stage 5 (Y): shape " c̲at (f̲ormat s̲hape Y) c̲at ", expected " c̲at f̲ormat r̲avel O c̲at Ho c̲at Wo
  } @
  g25:stage ← ᵃᶜᵗr̲elu Y
  g25:stage
} @)
53 1 5 5 r̲eshape P 54▷ ᶜᵛw̲indows 55▷ ((O c̲at 9) r̲eshape F) '+ '× i̲nner it 56▷ (1, B) ⁿⁿa̲ddAxis it 57▷ (O c̲at Ho c̲at Wo) r̲eshape it ▹Y [O Ho Wo] 58▷ ᵃᶜᵗr̲elu Y" 59 60⍝━━ Plain X_eTaL 61 62⍝│ Conv's layer for a batch of one picture, as the repository's CNN 63⍝│ demos use it. 64plain ← ᵃᶜᵗr̲elu (O c̲at Ho c̲at Wo) r̲eshape ((O c̲at 9) r̲eshape F, B) ᶜᵛc̲onv 1 5 5 r̲eshape P 65 66⍝━━ The same 67 68r0 ← "the equation and the pipeline of equations" ᵏt̲est eq ᵏn̲ear eqpipe 69r1 ← "the equation and the pipeline" ᵏt̲est eq ᵏn̲ear pipe 70r2 ← "the pipeline and plain X_eTaL" ᵏt̲est pipe ᵏn̲ear plain 71ᵏr̲eport r0 ᵏa̲nd r1 ᵏa̲nd r2 72 73⍝│ The two feature maps (after the activation): the vertical edge at 74⍝│ the square's left side, the center-surround on its corners. 75plain