sourcelibs/Net/demos/xor.xtl

1⍝!/usr/bin/env xetal 2⍝# XOR, the network written as one line. NN's own demo builds the same 3⍝# two layers by hand; here the spec "2 2 relu 2 softmax" is the 4⍝# network, and xetal expand shows the function the macro wrote. 5⍝# Run with "just demo-lib Net xor"; see it with "just expand-lib Net xor". 6 7ⁿⁿ⁼u̲se< "NN" 8ⁿᵉᵗ⁼u̲se< "Net" 9⍝# The four points and their classes (1: same, 2: differ). 10x ← 4 2 r̲eshape 0.0 0.0 0.0 1.0 1.0 0.0 1.0 1.0 11y ← 1 2 2 1 12⍝# The weights, set by hand: each layer's bias is its last row. 13w1 ← 3 2 r̲eshape 1.0 1.0 1.0 1.0 0.0 -1.0 14w2 ← 3 2 r̲eshape 0.0 4.0 0.0 -8.0 0.0 -2.0 15⍝ How many numbers this network has, counted when the program is 16⍝ compiled, and whether the weights have the shapes the spec says. 17"2 2 relu 2 softmax" ⁿᵉᵗp̲arams< @
ⁿᵉᵗp̲arams< expands to
12
18"2 2 relu 2 softmax" ⁿᵉᵗs̲hapes< "w1 w2"
ⁿᵉᵗs̲hapes< expands to
((s̲hape w1) m̲atch 3 2) ∧ ((s̲hape w2) m̲atch 3 2)
19⍝# The network: a function, written by the macro. 20ᵘx̲or ← "2 2 relu 2 softmax" ⁿᵉᵗn̲etwork< "w1 w2"
ⁿᵉᵗn̲etwork< expands to
({ x → ⁿⁿs̲oftmax (ⁿⁿr̲elu x ⁿⁿd̲ense w1) ⁿⁿd̲ense w2 })
21p ← ᵘx̲or x 22p 23ⁿⁿa̲rgmax p 24y ⁿⁿa̲ccuracy p