Notebooks · examples/nn_symmetry
examples/ nn_symmetry.viv
examples/nn_symmetry.viv · 1 code cellOpen in the workbenchDownload .vivLook inside this program
- Atlas
The program’s map: every function and which calls which.
There’s no recorded run of this program yet.
Shift+Enter in a cell runs it and moves to the next. Running a cell runs every cell above it too, as one program.
[1]lines 1–446
# NN-guided symmetry discovery: the AI-Feynman outer loop, in Viv. # # The mystery is f(x, y) = exp(-(x-y)^2), sampled on a grid. In two variables # its shortest RPN spelling needs 8 tokens -- beyond the L <= 6 search budget, # so brute force drowns (phase 1). A small MLP trained on the same samples # (phase 2) becomes a queryable stand-in for f: shift BOTH inputs by the same # amount -- a point the dataset never contained -- and the prediction doesn't # move (phase 3). That invariance means f depends only on u = x - y, so the # search re-runs in one variable (phase 4), where the answer costs 5 tokens # and the SAME budget finds it exactly. # # Output protocol (labeled `show` lines): ph marks the phase; ch_* champion # records as in symbolic_regression.viv; ep/tr_loss the NN trace; pr_* the # probe deviations; n_valid/n_seen search stats. # # Alphabet (codes): 0 x 1 one 2 + 3 - 4 * 5 / 6 sin 7 exp # 8 sqrt 9 neg 10 y (phase-1 only) MAXLEN = 6 GRID = 15 NPTS = 225 xs1 = zeros(256) xs2 = zeros(256) ys = zeros(256) syy = zeros(1) def gen_data() { for i to GRID { for j to GRID { x = 0.0 - 1.2 + 2.4 * i / (GRID - 1.0) y = 0.0 - 1.2 + 2.4 * j / (GRID - 1.0) k = i * GRID + j xs1[k] = x xs2[k] = y d = x - y ys[k] = exp(0.0 - d * d) } } s = 0.0 for k to NPTS { s = s + ys[k] * ys[k] } syy[0] = s } # ── the RPN machinery (two-variable variant of symbolic_regression.viv) ──── def arity(c) { if c <= 1.0 { return 0.0 } if c == 10.0 { return 0.0 } if c <= 5.0 { return 2.0 } return 1.0 } code = zeros(8) stk = zeros(8) bad = zeros(1) def eval_rpn(L, x, y) { sp = 0.0 for k to L { c = code[k]