sourcedemos/cnn-digits/batch.xtl

1⍝!/usr/bin/env xetal 2⍝│ The digit reader over a batch: the first 1,000 MNIST test digits as 3⍝│ one batch through the Conv library (the windows of every picture, 4⍝│ one product for the batch, the pooling), then the dense layer; how 5⍝│ many digits it reads right, the count the trainer's Rust forward 6⍝│ pass gives from the same pixels. The batch run for a GPU 7⍝│ (docs/gpu-asks.md); cnn-digits.xtl is the lesson, a picture at a time. 8 9"nn:" u̲se< "NN" 10"act:" u̲se< "Activations" 11"cv:" u̲se< "Conv" 12 13⍝━━ The network, read from data/ (as cnn-digits.xtl reads it) 14fb ← 8 10 r̲eshape n̲umbers ⎕N̲GET "data/filters.txt" 15wb ← 1353 10 r̲eshape n̲umbers ⎕N̲GET "data/dense.txt" 16 17⍝━━ The batch: the test digits, their pixels as whole percents, and their labels 18X ← (1000 28 28 r̲eshape n̲umbers ⎕N̲GET "data/test-digits.txt") ÷ 100.0 19labels ← f̲loor n̲umbers ⎕N̲GET "data/test-labels.txt" 20 21⍝━━ The reader 22⍝│ The digit, 0 to 9, the network reads in each picture of a batch X: 23⍝│ the filters over every window, ReLU, 2 x 2 pooling, the dense layer, 24⍝│ the likeliest output. One function of arrays, the part a GPU takes. 25ᵘr̲ead ← { X → 26 (ⁿⁿa̲rgmax (((t̲ally X) c̲at 1352) r̲eshape ᶜᵛp̲ool ᵃᶜᵗr̲elu (9 t̲ake₂ fb, r̲avel -1 t̲ake₂ fb) ᶜᵛc̲onv X) ⁿⁿd̲ense wb) − 1 27} 28⍝ -- end of the core ------------------------------------------ 29 30⍝━━ How many of the 1,000 it reads right, and that the Rust model counts the same 31right ← f̲loor '+ r̲/ f̲loat (ᵘr̲ead X) = labels 32right 33right = f̲loor n̲umbers ⎕N̲GET "data/test-right.txt"