programdemos/cnn-digits/cnn-digits.xtl
A tiny convolutional network reads a handwritten digit: 28 x 28 -> 8 filters of 3 x 3 -> ReLU -> 2 x 2 max-pooling -> dense -> softmax. The convolution is the picture's nine shifted copies times the filters: one matrix product. Trained offline on MNIST (97.82% of the 10,000 test digits right); just cnn-train writes data/.
The network, read from data/
k9 : Float
k9 ← 9 t̲ake₂ fb ⍝ 8 filters, 3 x 3 each as 9
Used in: ᵘc̲onv, demos/cnn-digits/cnn-digits.xtl:47
The core: every stage one array expression
ᵘc̲onv : Float -> Float
ᵘc̲onv ← { x → v ← 1 d̲rop₄ -1 d̲rop₄ 1 d̲rop₃ -1 d̲rop₃ -1 0 1 o̲-₂ -1 0 1 o̲-₂ x 8 26 26 r̲eshape (k9 '+ '× i̲nner 9 676 r̲eshape v) + bc 'l̲eft t̲able o̲ffsets 676 }
Used in: ᵘc̲lassify, c
ᵘc̲lassify : Float -> Float
ᵘc̲lassify ← { x → f̲irst ⁿⁿs̲oftmax (1 1352 r̲eshape r̲avel ᵘp̲ool ⁿⁿr̲elu ᵘc̲onv x) ⁿⁿd̲ense wb }
Drawing: values as shades, maps side by side
ᵘs̲hade : Float -> Char
ᵘs̲hade ← { m → (1 + f̲loor 0 m̲ax 7 m̲in 8.0 × m) s̲elect " .:-=+*#" }
Used in: ᵘm̲aps, demos/cnn-digits/cnn-digits.xtl:45
ᵘm̲aps : Float -> Char
Each of n maps scaled from its own smallest to largest value, then shaded.
ᵘm̲aps ← { m → n ← 1 t̲ake s̲hape m e ← '× r̲/ 1 d̲rop s̲hape m f ← (n c̲at e) r̲eshape m lo ← (n c̲at e) r̲eshape (n r̲eshape e) r̲eplicate 'm̲in r̲/₂ f hi ← (n c̲at e) r̲eshape (n r̲eshape e) r̲eplicate 'm̲ax r̲/₂ f ᵘs̲hade (s̲hape m) r̲eshape (f − lo) ÷ 0.000001 m̲ax hi − lo }
ᵘw̲ide : a -> a
ᵘw̲ide ← { c → ((-1 t̲ake s̲hape c) r̲eshape 2) r̲eplicate₂ c }
ᵘs̲ide : Char -> Char
ᵘs̲ide ← { c → g ← c c̲at₃ ((2 t̲ake s̲hape c) c̲at 2) r̲eshape " " ((1 s̲elect 1 d̲rop s̲hape g) c̲at '× r̲/ 1 0 1 r̲eplicate s̲hape g) r̲eshape 2 1 3 t̲ranspose g }
ᵘb̲ars : Float -> Char
ᵘb̲ars ← { p → (1 + (f̲loor 0.5 + 50.0 × p) '> t̲able o̲ffsets 50) s̲elect " #" }
Used in: demos/cnn-digits/cnn-digits.xtl:59
ᵘr̲ead : Int -> Int
The ten test digits (0 to 9): what the network reads, how sure (%).
ᵘr̲ead ← { i → (ⁿⁿa̲rgmax ᵘc̲lassify i s̲elect samples) − 1 }
Used in: demos/cnn-digits/cnn-digits.xtl:64
ᵘs̲ure : Int -> Int
ᵘs̲ure ← { i → f̲loor 0.5 + 100.0 × 'm̲ax r̲/ ᵘc̲lassify i s̲elect samples }
Used in: demos/cnn-digits/cnn-digits.xtl:65