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 964–1063 of 1244. Source text is displayed literally.
964 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)) * 21041st1 = math.log(0.01) / math.log(1 - mu_disc(1))1042st8 = math.log(0.01) / math.log(1 - mu_disc(8))1043st16 = math.log(0.01) / math.log(1 - mu_disc(16))1044check('K2 kernel gradient descent learns mode k at rate mu_k: after 200 steps mode 1 is fitted and 3.8 per cent of mode 8 remains',1045 r1_ < 1e-12 and close(r8_, (1 - mu_disc(8)) ** 200, 1e-9) and 0.03 < r8_ < 0.06,1046 'residual amplitudes %.0e (k=1), %.4f (k=8); steps to 1 per cent: %.1f, %.0f, %.0f for k = 1, 8, 16' % (r1_, r8_, st1, st8, st16))1047 1048n = 311049AD = [[v * 32 * 32 for v in row] for row in lap1(n, 'D')]1050ld, _ = jacobi_eig(AD)1051kap = ld[-1] / ld[0]1052AA = mm(tr(AD), AD)1053l2, _ = jacobi_eig(AA)1054kap2 = l2[-1] / l2[0]1055 1056 1057def its(k_):1058 return math.log(1e-6) / math.log((k_ - 1) / (k_ + 1))1059 1060 1061check('K3 the strong-form (collocation) least-squares problem has the square of the condition number of the energy (Ritz) problem',1062 close(kap, (math.sin(31 * PI / 64) / math.sin(PI / 64)) ** 2, 1e-8) and close(kap2, kap * kap, 1e-6),1063 '31 unknowns: condition %.1f (Ritz) and %.0f (strong form); optimal-step gradient iterations for 1e-6: %.0f and %.0f' % (kap, kap2, its(kap), its(kap2)))