Independent scholarship · Open accessThe monthly digest

Indexed text attachment

verify-pde-learning.py

Source for Partial Differential Equations in Machine Learning and AI: Naturality, Well-Posedness, and Spectral Rates

View paper · Download original file

Lines 864–963 of 1244. Source text is displayed literally.

864keep = (sum(dirs[400:]) / 100) / (sum(dirs[:100]) / 100)865_, _, norms = wave_run(L, 4.4 / lam[-1], 200, x0)866gr = norms[-1] / norms[-2]867check('G3 a second-order (wave) layer conserves a discrete energy and does not smooth; past the CFL bound it grows by 1.8633 per layer',868      drift < 1e-9 and 0.5 < keep < 2 and close(gr, (2.4 + math.sqrt(2.4 ** 2 - 4)) / 2, 1e-6),869      'invariant drift %.1e over 500 layers; late/early mean Dirichlet energy %.3f; growth factor %.4f at tau^2*lambda_max = 4.4' % (drift, keep, gr))870 871n = 10872E2 = rand_graph(n, 6)873lab = [random.choice([-1.0, 1.0]) for _ in range(n)]874lab[0], lab[1] = 1.0, -1.0875sg = [lab[u_] * lab[v_] for u_, v_ in E2]876LF = graph_lap(n, E2, sg)877L0 = graph_lap(n, E2)878lf, _ = jacobi_eig(LF)879l0, _ = jacobi_eig(L0)880X0 = [random.gauss(0, 1) for _ in range(n)]881Xs, Xo = list(X0), list(X0)882for _ in range(3000):883    Xs = axpy(-1 / lf[-1], mv(LF, Xs), Xs)884    Xo = axpy(-1 / l0[-1], mv(L0, Xo), Xo)885proj = dot(lab, X0) / n886sep = max(abs(Xs[i] - proj * lab[i]) for i in range(n))887flat = norm(center(Xo))888sgf = list(sg)889sgf[-1] = -sgf[-1]890lff, _ = jacobi_eig(graph_lap(n, E2, sgf))891C7 = [(i, (i + 1) % 7) for i in range(7)]892lc, _ = jacobi_eig(graph_lap(7, C7, [1.0] * 6 + [-1.0]))893check('G4 sheaf diffusion converges to the global sections: a balanced sign sheaf separates the two classes, a frustrated one has none',894      lf[0] < 1e-10 and lf[1] > 1e-3 and sep < 1e-8 and flat < 1e-8 and lff[0] > 1e-3 and close(lc[0], 4 * math.sin(PI / 14) ** 2, 1e-9),895      'balanced: kernel dimension 1, limit = labelling times %.4f (error %.0e), ordinary diffusion limit constant (%.0e); one flipped edge: smallest eigenvalue %.4f; frustrated 7-cycle %.5f' % (proj, sep, flat, lff[0], lc[0]))896 897E3 = [(0, 1), (0, 2), (1, 2), (1, 3), (2, 3), (2, 4), (3, 4)]898nb = 0899okb = True900for bits in itertools.product([1, -1], repeat=7):901    labl = {0: 1}902    cons = True903    for _ in range(5):904        for (u_, v_), s in zip(E3, bits):905            if u_ in labl and v_ not in labl:906                labl[v_] = labl[u_] * s907            elif v_ in labl and u_ not in labl:908                labl[u_] = labl[v_] * s909    cons = all(labl[u_] * labl[v_] == s for (u_, v_), s in zip(E3, bits))910    Lr = [[Fr(0)] * 5 for _ in range(5)]911    for (u_, v_), s in zip(E3, bits):912        Lr[u_][u_] += 1913        Lr[v_][v_] += 1914        Lr[u_][v_] -= s915        Lr[v_][u_] -= s916    sing = rank_exact(Lr) < 5917    okb = okb and (sing == cons)918    nb += cons919check('G5 on a graph with 5 nodes and 7 edges, 16 = 2^(V-1) of the 128 sign sheaves have a global section: the others are the non-zero classes of H^1(G; Z/2)',920      nb == 16 and okb, '%d balanced; singular Laplacian exactly for these; 128/16 = 8 = 2^(E-V+1) classes' % nb)921 922sec('6. Residual networks as difference schemes')923n = 6924B = [[random.gauss(0, 1) for _ in range(n)] for _ in range(n)]925A = [[sum(B[k][i] * B[k][j] for k in range(n)) + (0.1 if i == j else 0) for j in range(n)] for i in range(n)]926la, _ = jacobi_eig(A)927 928 929def euler_growth(h, steps=400):930    x = [random.gauss(0, 1) for _ in range(n)]931    p = None932    for k in range(steps):933        if k == steps - 1:934            p = norm(x)935        x = axpy(-h, mv(A, x), x)936        s = norm(x)937        if s > 1e100 or s < 1e-100:938            x = [a / s for a in x]939            if k == steps - 1:940                p = p / s941    return norm(x) / p942 943 944g_lo, g_hi = euler_growth(1.9 / la[-1]), euler_growth(2.1 / la[-1])945check('R1 a residual layer x - h*A*x with A positive definite is stable exactly for h <= 2/lambda_max',946      g_lo < 1 and close(g_hi, 1.1, 1e-6), 'per-layer factor %.4f at h = 1.9/lambda_max and %.4f at 2.1/lambda_max' % (g_lo, g_hi))947 948 949def euler_rot(h, N):950    q, p = 1.0, 0.0951    for _ in range(N):952        q, p = q - h * p, p + h * q953    return math.hypot(q, p)954 955 956e1, e2 = euler_rot(0.1, 100), euler_rot(0.01, 1000)957check('R2 forward Euler on an antisymmetric layer is unstable for every step: the norm grows by (1+h^2)^(N/2)',958      close(e1, 1.01 ** 50, 1e-12) and close(e2, 1.0001 ** 500, 1e-12) and e1 > e2 > 1,959      'growth over time 10: %.4f at h = 0.1, %.4f at h = 0.01 (e^{hT/2} = %.4f)' % (e1, e2, math.exp(0.05)))960 961 962def leap_mod(hw, N=400):963    q, p = 1.0, 0.3