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 924–1023 of 1244. Source text is displayed literally.
924B = [[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.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))