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

1164for _ in range(300):1165    V = [random.gauss(0, 3) for _ in range(nS)]1166    Wv = [random.gauss(0, 3) for _ in range(nS)]1167    q = sup(axpy(-1, bell(V), bell(Wv))) / sup(axpy(-1, V, Wv))1168    worst = max(worst, q)1169    okc = okc and q <= gam + 1e-121170    Up = [a_ + abs(random.gauss(0, 1)) for a_ in V]1171    okm = okm and all(p_ <= q_ + 1e-12 for p_, q_ in zip(bell(V), bell(Up)))1172Vs = [0.0] * nS1173for _ in range(2000):1174    Vs = bell(Vs)1175V = [0.0] * nS1176e0 = sup(axpy(-1, V, Vs))1177okr = True1178for k in range(1, 60):1179    V = bell(V)1180    okr = okr and sup(axpy(-1, V, Vs)) <= gam ** k * e0 + 1e-121181check('Q1 the Bellman operator is monotone and a sup-norm contraction of factor gamma, so value iteration converges geometrically',1182      okc and okm and okr and sup(axpy(-1, bell(Vs), Vs)) < 1e-12, 'largest observed contraction ratio %.4f (gamma = %.1f) over 300 pairs' % (worst, gam))1183 1184n = 201185h = 1.0 / n1186u = [0.0] + [10.0] * (n - 1) + [0.0]1187for _ in range(200):1188    for i in range(1, n):1189        u[i] = min(u[i - 1], u[i + 1]) + h1190dist = [min(i * h, 1 - i * h) for i in range(n + 1)]1191ev_ = max(abs(a_ - b_) for a_, b_ in zip(u, dist))1192saw = [0.0, h, 2 * h, h, 0.0, h, 2 * h, 3 * h, 4 * h, 5 * h, 6 * h, 7 * h, 8 * h, 7 * h, 6 * h, 5 * h, 4 * h, 3 * h, 2 * h, h, 0.0]1193res_saw = max(abs(saw[i] - (min(saw[i - 1], saw[i + 1]) + h)) for i in range(1, n))1194neg = [-d for d in dist]1195res_neg = max(abs(neg[i] - (min(neg[i - 1], neg[i + 1]) + h)) for i in range(1, n))1196check('Q2 the monotone scheme for |u\'| = 1 converges to the distance function and rejects the other almost-everywhere solutions',1197      ev_ < 1e-12 and res_saw > h and res_neg > h, 'error %.0e; residual of a sawtooth %.2f and of minus the distance %.2f (grid step %.2f)' % (ev_, res_saw, res_neg, h))1198 1199sec('10. How many numbers a solution operator needs')1200n = 631201lamk = [4 * 64 * 64 * math.sin(k * PI / 128) ** 2 for k in range(1, n + 1)]1202ell = [1 / v for v in lamk]1203par = [math.exp(-0.01 * v) for v in lamk]1204rank_e = sum(1 for v in ell if v / ell[0] > 1e-6)1205rank_p = sum(1 for v in par if v / par[0] > 1e-6)1206cont_p = max(k for k in range(1, 100) if math.exp(-0.01 * PI * PI * (k * k - 1)) > 1e-6)1207n2 = 151208A2 = [[v * 16 * 16 for v in row] for row in lap1(n2, 'D')]1209l15, v15 = jacobi_eig(A2)1210tt = 0.371211U = [[0.0] * (2 * n2) for _ in range(2 * n2)]1212for k in range(n2):1213    w_ = math.sqrt(l15[k])1214    cs, sn = math.cos(w_ * tt), math.sin(w_ * tt)1215    for i in range(n2):1216        for j in range(n2):1217            pij = v15[k][i] * v15[k][j]1218            U[i][j] += cs * pij1219            U[i][n2 + j] += sn / w_ * pij1220            U[n2 + i][j] += -w_ * sn * pij1221            U[n2 + i][n2 + j] += cs * pij1222Em = [[0.0] * (2 * n2) for _ in range(2 * n2)]1223for i in range(n2):1224    for j in range(n2):1225        Em[i][j] = A2[i][j]1226    Em[n2 + i][n2 + i] = 1.01227UEU = mm(tr(U), mm(Em, U))1228iso = max(abs(UEU[i][j] - Em[i][j]) for i in range(2 * n2) for j in range(2 * n2)) / l15[-1]1229check('O1 numerical rank at relative accuracy 1e-6: elliptic inverse 1000 modes (algebraic), heat flow at t = 0.01 eleven modes, wave flow all of them',1230      rank_e == 63 and cont_p == 11 and rank_p in (11, 12) and close(ell[0], 1 / PI ** 2, 1e-3) and iso < 1e-10,1231      'singular values 1/(pi k)^2 exceed 1e-6 of the first for k < 1000; heat: %d in the continuum, %d on a 63-point grid; wave propagator is an isometry of the energy (defect %.0e)' % (cont_p, rank_p, iso))1232 1233m = 601234Cm = [[(1 - max((i + 0.5) / m, (j + 0.5) / m)) / m for j in range(m)] for i in range(m)]1235lc_, _ = jacobi_eig(Cm)1236top = sorted(lc_, reverse=True)[:3]1237exact3 = [1 / ((k - 0.5) ** 2 * PI ** 2) for k in (1, 2, 3)]1238tail10 = 0.5 - sum(1 / ((k - 0.5) ** 2 * PI ** 2) for k in range(1, 11))1239check('O2 translates of a step have covariance eigenvalues 1/((k-1/2)pi)^2: ten modes leave 14 per cent error, the n-width decays like n^(-1/2)',1240      all(close(a_, b_, 2e-3) for a_, b_ in zip(top, exact3)) and close(math.sqrt(tail10 / 0.5), 0.1424, 5e-3),1241      'eigenvalues %s (exact %s); relative error after 10 modes %.4f, after 100 modes %.4f' % (', '.join('%.5f' % v for v in top), ', '.join('%.5f' % v for v in exact3), math.sqrt(tail10 / 0.5), math.sqrt((0.5 - sum(1 / ((k - 0.5) ** 2 * PI ** 2) for k in range(1, 101))) / 0.5)))1242 1243footer()1244