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

724    return simpson(f, -9, 9, 6000)725 726 727def esm(s):728    return simpson(lambda y: p2(y) * (s(y) - s_true(y)) ** 2, -10, 10, 6000)729 730 731J2 = simpson(lambda y: p2(y) * s_true(y) ** 2, -10, 10, 6000)732gaps = [dsm(s) - esm(s) for s in (lambda y: -math.tanh(y), lambda y: -y / 2 + math.sin(y))]733check('S8 denoising score matching differs from explicit score matching by the constant 1/t - J, so they have the same minimizer',734      close(gaps[0], gaps[1], 1e-8) and close(gaps[0], 1 / t - J2, 1e-8), 'gap %.6f for two test functions; 1/t - J = %.6f' % (gaps[0], 1 / t - J2))735 736sec('4. Characteristics: continuous normalizing flows and flow matching')737A_, B_, C_ = 1.3, 0.2, 0.8738 739 740def vf(t, y):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: