librarywork/xetal-libraries/libs/Equations/src/Equations.xtl

Equations: sums as printed in papers, written in one macro call (e:e_q<, e:t_ex<, e:b_oth< in Equations.xtlm); here the valid 2-D cross-correlation of a CNN layer, as array code, which checks the macro. Import it with an alias of your choice: "e:" u_se< "Equations". Put libs/Equations/src on XETAL_PATH ("just path"); the reference is libs/Equations/docs. Names with l: are exported; those under h: are private to this file.

source

ˡc̲orrelate : Num a => a -> a -> a

function · line 14

The valid 2-D cross-correlation of pictures X (channels, height, width) with filters W (outputs, channels, kernel height, kernel width), as CNN layers compute it: no kernel flip, stride 1, no padding, so the result has shape outputs, H - Kh + 1, Wd - Kw + 1. 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 written without the macros (the windows by rotation, then one inner product with the filters), so it checks them independently.

ˡc̲orrelate ← { X W →
  (C, H, Wd) ← (1 s̲elect s̲hape X, 2 s̲elect s̲hape X, 3 s̲elect s̲hape X)
  (O, Kh, Kw) ← (1 s̲elect s̲hape W, 3 s̲elect s̲hape W, 4 s̲elect s̲hape W)
  Ho ← H − Kh − 1
  Wo ← Wd − Kw − 1
  P ← 2 3 1 4 5 t̲ranspose Wo t̲ake₅ Ho t̲ake₄ (o̲ffsets Kh) o̲-₃ (o̲ffsets Kw) o̲-₃ X
  F ← (O c̲at C × Kh × Kw) r̲eshape W
  (O c̲at Ho c̲at Wo) r̲eshape F '+ '× i̲nner ((C × Kh × Kw) c̲at Ho × Wo) r̲eshape P
}