sourcelibs/NN/demos/xor.xtl
1⍝!/usr/bin/env xetal
2⍝# XOR by a two-layer network with hand-set weights: no straight line
3⍝# separates XOR's classes, but one hidden ReLU layer does. Every layer
4⍝# is one nn:d_ense on the whole batch of four points at once.
5⍝# Run with "just demo-lib NN xor".
6
7ⁿⁿ⁼u̲se< "NN"
8⍝# The four points, one per row, and their classes (1: same, 2: differ).
9x ← 4 2 r̲eshape 0.0 0.0 0.0 1.0 1.0 0.0 1.0 1.0
10y ← 1 2 2 1
11⍝# Hidden layer: h1 = relu(a + b), h2 = relu(a + b - 1); the last row
12⍝# of each weight array is its bias.
13w1 ← 3 2 r̲eshape 1.0 1.0 1.0 1.0 0.0 -1.0
14h ← ⁿⁿr̲elu x ⁿⁿd̲ense w1
15h
16⍝# Output layer: class 2's score is 4 h1 - 8 h2 - 2, class 1's is 0.
17w2 ← 3 2 r̲eshape 0.0 4.0 0.0 -8.0 0.0 -2.0
18p ← ⁿⁿs̲oftmax h ⁿⁿd̲ense w2
19p
20⍝ The predicted classes, the accuracy and the loss.
21ⁿⁿa̲rgmax p
22y ⁿⁿa̲ccuracy p
23(2 ⁿⁿo̲neHot y) ⁿⁿc̲rossEntropy p