programdemos/pipelines/rosetta.xtl

One CNN layer four ways, a Rosetta Stone: as a paper prints it (an equation, read by X_eTaL-libraries' Equations), as a pipeline (Flow), and as plain X_eTaL (this repository's Conv). The pipeline's expansion, the fourth way, is what xetal expand rosetta.xtl shows. All give the same feature maps.

source · imports e: work/libs/Equations/src/Equations.xtl work/libs/Equations/src/Equations.xtlm; flow: libs/Flow/src/Flow.xtlm; cv: libs/Conv/src/Conv.xtl; nn: libs/NN/src/NN.xtl; act: libs/Activations/src/Activations.xtl; k: work/libs/Check/src/Check.xtl work/libs/Check/src/Check.xtlm

Data

m : Float

value · line 20

One 5 x 5 picture (one channel: a bright 2 x 2 square on a dim field), two 3 x 3 filters (a vertical edge, a center-surround) and their biases.

m ← 0.0 1.0 1.0 0.0 0.0
Used in: P

P : Float

value · line 21
P ← 0.1 + 0.8 × m '× t̲able m
Used in: eq, eqpipe, pipe, plain

F : Float

value · line 22
F ← 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
Used in: eq, eqpipe, pipe, plain

B : Float

value · line 23
B ← 0.5 -0.25
Used in: eq, eqpipe, pipe, plain

O : Int

value · line 24
O ← 2
Used in: eq, eqpipe, pipe, plain

Ho : Int

value · line 25
Ho ← 3
Used in: eq, eqpipe, pipe, plain

Wo : Int

value · line 26
Wo ← 3
Used in: eq, eqpipe, pipe, plain

As a paper prints it

eq : Float

value · line 32

Y[o,h,w] = max(sum over r, s of P[h+r, w+s] F[o,r,s], plus B[o], 0): its value, then its LaTeX, from the same reading of the same text.

eq ←  ᵉ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))
} @))
Used in: r0, r1

eqpipe : Float

value · line 37

The same as a pipeline of equations, a stage each: the convolution with its bias, then the activation; drawn as math a row a stage.

eqpipe ← @ ᶠˡᵒʷ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))
} @))
Used in: r0

drawn : Char

value · line 41
drawn ← @ ᶠˡᵒʷ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)")

As a pipeline

pipe : Float

value · line 52

The windows of the picture, one column each; every filter times every window (one matrix product); its bias along the filters' axis; the maps shaped O x Ho x Wo; then the activation.

pipe ← @ ᶠˡᵒʷ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
} @)
Used in: r1, r2

Plain X_eTaL

plain : Float

value · line 64

Conv's layer for a batch of one picture, as the repository's CNN demos use it.

plain ← ᵃᶜᵗ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

The same

r0 : Char

value · line 68
r0 ← "the equation and the pipeline of equations" ᵏt̲est eq ᵏn̲ear eqpipe

r1 : Char

value · line 69
r1 ← "the equation and the pipeline" ᵏt̲est eq ᵏn̲ear pipe

r2 : Char

value · line 70
r2 ← "the pipeline and plain X_eTaL" ᵏt̲est pipe ᵏn̲ear plain