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 941–1040 of 1244. Source text is displayed literally.

941    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.3964    I0 = p * p + q * q + hw * p * q965    drift = 0.0966    pn = None967    for k in range(N):968        if k == N - 1:969            pn = math.hypot(q, p)970        q = q + hw * p971        p = p - hw * q972        drift = max(drift, abs(p * p + q * q + hw * p * q - I0))973        s = math.hypot(q, p)974        if s > 1e100:975            q, p, pn = q / s, p / s, (pn / s if k == N - 1 else pn)976    return drift, math.hypot(q, p) / pn977 978 979d19, _ = leap_mod(1.9)980_, g21 = leap_mod(2.1)981q_edge, p_edge = 1, 0982for step in range(1, 41):983    q_edge += 2 * p_edge984    p_edge -= 2 * q_edge985check('R3 leapfrog has a positive invariant below h*omega = 2, linear growth at 2, and a growing eigenmode above 2',986      d19 < 1e-10 and (q_edge, p_edge) == (-79, 80)987      and close(g21, (2.41 + math.sqrt(2.41 ** 2 - 4)) / 2, 1e-6),988      'invariant drift %.1e at h*omega = 1.9; (q,p) at 2 after 40 steps (%d,%d); growth %.4f per layer at 2.1' % (d19, q_edge, p_edge, g21))989 990 991def cond2(M):992    a_, b_, c_, d_ = M[0][0], M[0][1], M[1][0], M[1][1]993    s = a_ * a_ + b_ * b_ + c_ * c_ + d_ * d_994    det = abs(a_ * d_ - b_ * c_)995    smax = math.sqrt((s + math.sqrt(max(s * s - 4 * det * det, 0))) / 2)996    return smax * smax / det997 998 999h = 0.51000M1 = [[1.0, h], [-h, 1 - h * h]]1001conds = {}1002P = [[1.0, 0.0], [0.0, 1.0]]1003for k in range(1, 801):1004    P = mm(M1, P)1005    if k in (50, 200, 800):1006        conds[k] = cond2(P)1007par = (0.95 / 0.5) ** 501008check('R4 backpropagation through N symplectic layers has condition number at most (1+h/2)/(1-h/2); through a parabolic stack it grows geometrically',1009      all(v <= 1.25 / 0.75 + 1e-9 for v in conds.values()) and par > 1e13,1010      'leapfrog, h = 0.5: %s for N = 50, 200, 800 (bound %.4f); Euler on eigenvalues 1 and 10, h = 0.05, N = 50: %.2e' % (', '.join('%.4f' % conds[k] for k in sorted(conds)), 1.25 / 0.75, par))1011 1012sec('7. Kernels, spectral bias and conditioning')1013N = 641014hh = 2 * PI / N1015 1016 1017def Gper(x):1018    x = (x + PI) % (2 * PI) - PI1019    return math.cosh(abs(x) - PI) / (2 * math.sinh(PI))1020 1021 1022def mu_disc(k):1023    return (PI / N) * math.sinh(2 * PI / N) / (math.cosh(2 * PI / N) - math.cos(2 * PI * k / N))1024 1025 1026Kmat = [[hh * Gper((i - j) * hh) for j in range(N)] for i in range(N)]1027r = 0.01028for k in (0, 1, 5, 10, 16):1029    v = [math.cos(k * i * hh) for i in range(N)]1030    r = max(r, norm(axpy(-mu_disc(k), v, mv(Kmat, v))))1031check('K1 the sampled H^1 kernel has eigenvalues sum_m 1/(1+(k+mN)^2): the continuum 1/(1+k^2) plus its aliases (Poisson summation)',1032      r < 1e-12 and close(mu_disc(1), 0.5, 2e-3) and 1.02 < mu_disc(10) * 101 < 1.10,1033      'residual %.0e; discrete/continuum eigenvalue %.4f at k=1, %.4f at k=10, %.4f at k=16' % (r, mu_disc(1) * 2, mu_disc(10) * 101, mu_disc(16) * 257))1034 1035yv = [math.cos(i * hh) + math.cos(8 * i * hh) for i in range(N)]1036f = [0.0] * N1037for _ in range(200):1038    f = axpy(-1.0, mv(Kmat, axpy(-1, yv, f)), f)1039res = axpy(-1, f, yv)1040r1_, r8_ = abs(coef(res, 1)) * 2, abs(coef(res, 8)) * 2