sourcework/xetal-libraries/libs/Equations/src/Equations.xtl
1⍝│ Equations: sums as printed in papers, written in one macro call (e:e_q<, e:t_ex<, e:b_oth< in Equations.xtlm);
2⍝│ here the valid 2-D cross-correlation of a CNN layer, as array code, which checks the macro.
3⍝│ Import it with an alias of your choice: "e:" u_se< "Equations".
4⍝│ Put libs/Equations/src on XETAL_PATH ("just path"); the reference is libs/Equations/docs.
5⍝│ Names with l: are exported; those under h: are private to this file.
6
7⍝│ The valid 2-D cross-correlation of pictures X (channels, height,
8⍝│ width) with filters W (outputs, channels, kernel height, kernel
9⍝│ width), as CNN layers compute it: no kernel flip, stride 1, no
10⍝│ padding, so the result has shape outputs, H - Kh + 1, Wd - Kw + 1.
11⍝│ Y[o,h,w] is the sum over c, r, s of X[c,h+r,w+s] * W[o,c,r,s]. It is
12⍝│ written without the macros (the windows by rotation, then one inner
13⍝│ product with the filters), so it checks them independently.
14ˡc̲orrelate ← { X W →
15 (C, H, Wd) ← (1 s̲elect s̲hape X, 2 s̲elect s̲hape X, 3 s̲elect s̲hape X)
16 (O, Kh, Kw) ← (1 s̲elect s̲hape W, 3 s̲elect s̲hape W, 4 s̲elect s̲hape W)
17 Ho ← H − Kh − 1
18 Wo ← Wd − Kw − 1
19 P ← 2 3 1 4 5 t̲ranspose Wo t̲ake₅ Ho t̲ake₄ (o̲ffsets Kh) o̲-₃ (o̲ffsets Kw) o̲-₃ X
20 F ← (O c̲at C × Kh × Kw) r̲eshape W
21 (O c̲at Ho c̲at Wo) r̲eshape F '+ '× i̲nner ((C × Kh × Kw) c̲at Ho × Wo) r̲eshape P
22}