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 1024–1123 of 1244. Source text is displayed literally.
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)))1064 1065AP = [[v / (hh * hh) for v in row] for row in lap1(N, 'P')]1066 1067 1068def rate_pinn(k):1069 v = [math.cos(k * i * hh) for i in range(N)]1070 w = mv(Kmat, mv(AP, mv(AP, v)))1071 return dot(v, w) / dot(v, v)1072 1073 1074rp1, rp16 = rate_pinn(1), rate_pinn(16)1075check('K4 composing the kernel with the strong-form loss reverses the bias: mode rates k^4/(1+k^2) grow with k',1076 close(rp1, mu_disc(1) * (4 / hh ** 2 * math.sin(hh / 2) ** 2) ** 2, 1e-9) and rp16 / rp1 > 100 and mu_disc(1) / mu_disc(16) > 100,1077 'regression rates %.4f and %.5f at k = 1, 16 (ratio 1/%.0f); strong-form rates %.4f and %.1f (ratio %.0f)' % (mu_disc(1), mu_disc(16), mu_disc(1) / mu_disc(16), rp1, rp16, rp16 / rp1))1078 1079xd = [0.5, 1.7, 3.0, 4.4, 5.6]1080ydat = [random.gauss(0, 1) for _ in xd]1081cc = solve([[Gper(a_ - b_) for b_ in xd] for a_ in xd], ydat)1082 1083 1084def uk(x):1085 return sum(c_ * Gper(x - b_) for c_, b_ in zip(cc, xd))1086 1087 1088r_int = max(abs(uk(a_) - y_) for a_, y_ in zip(xd, ydat))1089r_ode = max(abs(uk(x) - d2(uk, x)) for x in (0.1, 1.1, 2.3, 3.7, 5.0, 6.0))1090dl = 1e-51091r_jump = max(abs((uk(a_ + dl) - 2 * uk(a_) + uk(a_ - dl)) / dl + c_) for a_, c_ in zip(xd, cc))1092check('K5 the kernel interpolant solves u - u\'\' = sum c_i delta_{x_i}: the representer theorem is a fundamental solution',1093 r_int < 1e-12 and r_ode < 1e-6 and r_jump < 1e-3, 'interpolation %.0e; equation residual off the data %.1e; jump of u\' plus c_i %.1e' % (r_int, r_ode, r_jump))1094 1095sec('8. Langevin dynamics and stochastic gradient descent')1096 1097 1098def fp_gap(V, lo, hi, n, beta, k=2):1099 h = (hi - lo) / n1100 xs = [lo + i * h for i in range(n + 1)]1101 Vs = [V(x) for x in xs]1102 D = 1 / beta1103 d = []1104 for i in range(n + 1):1105 s = 0.01106 if i < n:1107 s += math.exp(-beta * (Vs[i + 1] - Vs[i]) / 2)1108 if i > 0:1109 s += math.exp(-beta * (Vs[i - 1] - Vs[i]) / 2)1110 d.append(D / h ** 2 * s)1111 e = [-D / h ** 2] * n1112 psi = [math.exp(-beta * v / 2) for v in Vs]1113 res = 0.01114 for i in range(n + 1):1115 s = d[i] * psi[i] + (e[i - 1] * psi[i - 1] if i > 0 else 0) + (e[i] * psi[i + 1] if i < n else 0)1116 res = max(res, abs(s) / (D / h ** 2 * max(psi)))1117 return tri_eigs(d, e, k), res1118 1119 1120(ev1, rs1), (ev4, rs4) = fp_gap(lambda x: x * x / 2, -6, 6, 240, 1.0), fp_gap(lambda x: x * x / 2, -4, 4, 320, 4.0)1121check('L1 the Gibbs density is stationary for the Fokker-Planck operator, and for a quadratic loss the spectral gap is the curvature at every temperature',1122 rs1 < 1e-12 and rs4 < 1e-12 and abs(ev1[0]) < 1e-8 and close(ev1[1], 1, 5e-3) and close(ev4[1], 1, 5e-3),1123 'gap %.4f at beta = 1 and %.4f at beta = 4' % (ev1[1], ev4[1]))