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

741    th = math.tanh(A_ * y[0] + B_)742    return [C_ * th, C_ * A_ * (1 - th * th)]743 744 745r = 0.0746for x in (-1.5, -0.4, 0.3, 1.2):747    y, ld = flow(vf, [x, 0.0], 0.0, 1.0, 1000)748    yp = flow(vf, [x + 1e-4, 0.0], 0.0, 1.0, 1000)[0]749    ym = flow(vf, [x - 1e-4, 0.0], 0.0, 1.0, 1000)[0]750    r = max(r, abs(math.exp(ld) - (yp - ym) / 2e-4))751check('F1 along a characteristic the log-density changes at rate -div v (instantaneous change of variables)', r < 1e-6,752      'max difference between exp(int div v) and the derivative of the flow map %.1e' % r)753 754m_, s_ = 2.0, 0.5755 756 757def vfm(t, y):758    return [m_ + (t * s_ * s_ - (1 - t)) / ((1 - t) ** 2 + t * t * s_ * s_) * (y[0] - t * m_)]759 760 761r = max(abs(flow(vfm, [x], 0.0, 1.0, 2000)[0] - (m_ + s_ * x)) for x in (-2.0, -1.0, 0.0, 0.7, 2.0))762check('F2 flow matching between Gaussians with independent coupling integrates to the monotone transport map x -> m + s x',763      r < 1e-7, 'max error %.1e' % r)764 765 766def p_fm(x, t):767    return 0.5 * (gauss(x, t, (1 - t) ** 2) + gauss(x, -t, (1 - t) ** 2))768 769 770def v_fm(x, t):771    return (math.tanh(t * x / (1 - t) ** 2) - x) / (1 - t)772 773 774r = max(abs(d1(lambda t: p_fm(x, t), 0.5) + d1(lambda y: p_fm(y, 0.5) * v_fm(y, 0.5), x)) for x in (-1.0, -0.3, 0.2, 0.9))775sl = d1(lambda y: v_fm(y, 0.9), 0.0, 1e-6)776tstar = bisect(lambda t: d1(lambda y: v_fm(y, t), 0.0, 1e-5), 0.2, 0.6)777check('F3 the marginal velocity for two-point data satisfies the continuity equation; the midpoint turns from attracting to repelling at t = (3 - sqrt 5)/2',778      r < 1e-6 and close(sl, 890, 1e-5) and close(tstar, (3 - math.sqrt(5)) / 2, 1e-7),779      'continuity residual %.1e; slope at the midpoint changes sign at t = %.6f and is %.1f at t = 0.9' % (r, tstar, sl))780 781sec('5. Graph networks: diffusion, waves and sheaves')782 783 784def graph_lap(n, edges, signs=None):785    L = [[0.0] * n for _ in range(n)]786    for idx, (u_, v_) in enumerate(edges):787        s = 1.0 if signs is None else signs[idx]788        L[u_][u_] += 1789        L[v_][v_] += 1790        L[u_][v_] -= s791        L[v_][u_] -= s792    return L793 794 795def rand_graph(n, extra):796    edges = [(random.randrange(i), i) for i in range(1, n)]797    while len(edges) < n - 1 + extra:798        a_, b_ = sorted(random.sample(range(n), 2))799        if (a_, b_) not in edges:800            edges.append((a_, b_))801    return edges802 803 804def center(x):805    m = sum(x) / len(x)806    return [a - m for a in x]807 808 809n = 12810E1 = rand_graph(n, 8)811L = graph_lap(n, E1)812lam, _ = jacobi_eig(L)813tau = 1 / lam[-1]814rho = 1 - tau * lam[1]815X = [random.gauss(0, 1) for _ in range(n)]816e0 = dot(X, mv(L, X))817layers99 = None818prev = None819for k in range(1, 201):820    X = axpy(-tau, mv(L, X), X)821    if layers99 is None and dot(X, mv(L, X)) < 0.01 * e0:822        layers99 = k823    if k == 199:824        prev = norm(center(X))825ratio = norm(center(X)) / prev826pred99 = math.log(0.01) / (2 * math.log(rho))827check('G1 linear message passing contracts the non-constant part by 1 - tau*lambda_2 per layer (oversmoothing rate)',828      close(ratio, rho, 1e-4) and layers99 <= math.ceil(pred99),829      'lambda_2 = %.4f, lambda_max = %.4f; measured factor %.5f, predicted %.5f; 99%% of Dirichlet energy lost after %d layers (bound %.1f)' % (lam[1], lam[-1], ratio, rho, layers99, pred99))830 831n = 32832lp = 4 * math.sin(PI / 64) ** 2833Q5 = [(i, i ^ (1 << b)) for i in range(32) for b in range(5) if i < i ^ (1 << b)]834lq, _ = jacobi_eig(graph_lap(32, Q5))835K32 = [(i, j) for i in range(32) for j in range(i + 1, 32)]836x = [random.gauss(0, 1) for _ in range(32)]837y = axpy(-1 / 32, mv(graph_lap(32, K32), x), x)838lpath, _ = jacobi_eig(lap1(32, 'N'))839check('G2 the depth scale 1/(tau*lambda_2) of oversmoothing on 32 nodes: path 415 layers, hypercube 5, complete graph 1',840      close(lpath[1], lp, 1e-9) and close(1 / (0.25 * lp), 415.4, 1e-3) and close(lq[1], 2, 1e-9) and close(lq[-1], 10, 1e-9) and norm(center(y)) < 1e-12,