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

484def shift_c(x):485    return [x[-1]] + list(x[:-1])486 487 488n = 20489x = [random.gauss(0, 1) for _ in range(n)]490r = 2491com = axpy(-1, conv_pad(shift(x), wa, 'zero'), shift(conv_pad(x, wa, 'zero')))492supp = [i for i in range(n) if abs(com[i]) > 1e-12]493comc = norm(axpy(-1, conv_pad(shift_c(x), wa, 'circ'), shift_c(conv_pad(x, wa, 'circ'))))494check('N5 circular convolution commutes with translation; zero padding fails only within the kernel radius of the boundary',495      comc < 1e-12 and len(supp) > 0 and all(i == 0 or i >= n - r for i in supp), 'commutator supported on positions %s of 0..%d' % (supp, n - 1))496 497n = 32498one = [1.0] * n499MD = [[(1.0 if i == j else 0.0) - 0.25 * lap1(n, 'D')[i][j] for j in range(n)] for i in range(n)]500MN = [[(1.0 if i == j else 0.0) - 0.25 * lap1(n, 'N')[i][j] for j in range(n)] for i in range(n)]501yD, yN = one, [random.random() for _ in range(n)]502m0 = sum(yN)503mx0 = max(yN)504okmax = True505for _ in range(200):506    yD, yN2 = mv(MD, yD), mv(MN, yN)507    okmax = okmax and max(yN2) <= max(yN) + 1e-15 and min(yN2) >= min(yN) - 1e-15508    yN = yN2509surv = sum(yD) / n510pred = 0.0511for k in range(1, n + 1):512    ip = sum(math.sin(PI * k * j / (n + 1)) for j in range(1, n + 1))513    pred += (1 - math.sin(PI * k / (2 * (n + 1))) ** 2) ** 200 * ip * ip * 2 / (n + 1) / n514check('N6 a smoothing layer with reflect padding conserves mass and obeys the maximum principle; zero padding leaks mass',515      abs(sum(yN) - m0) < 1e-10 and okmax and close(surv, pred, 1e-10) and 0.3 < surv < 0.9,516      'after 200 layers of (1/4,1/2,1/4) on 32 points: reflect mass change %.0e; zero-padded mass left %.4f (spectral formula %.4f)' % (abs(sum(yN) - m0), surv, pred))517 518sec('2. Discretization: what commutes with resampling')519 520 521def dft(x):522    N = len(x)523    return [sum(x[j] * cmath.exp(-2j * PI * k * j / N) for j in range(N)) / N for k in range(N)]524 525 526def coef(x, k):527    N = len(x)528    return sum(x[j] * cmath.exp(-2j * PI * k * j / N) for j in range(N)) / N529 530 531K = 5532a = [random.gauss(0, 1) for _ in range(K + 1)]533b = [random.gauss(0, 1) for _ in range(K + 1)]534 535 536def u(x):537    return sum(a[k] * math.cos(k * x) + b[k] * math.sin(k * x) for k in range(1, K + 1))538 539 540def mult_exact(x):541    return sum((a[k] * math.cos(k * x) + b[k] * math.sin(k * x)) / (1 + k * k) for k in range(1, K + 1))542 543 544def lap_exact(x):545    return sum((a[k] * math.cos(k * x) + b[k] * math.sin(k * x)) * k * k for k in range(1, K + 1))546 547 548def apply_mult(N):549    xs = [2 * PI * j / N for j in range(N)]550    c = dft([u(t) for t in xs])551    out = []552    for j in range(N):553        s = 0554        for k in range(N):555            f = k if k <= N // 2 else k - N556            s += c[k] / (1 + f * f) * cmath.exp(2j * PI * k * j / N)557        out.append(s.real)558    return max(abs(out[j] - mult_exact(xs[j])) for j in range(N))559 560 561def apply_stencil(N):562    h = 2 * PI / N563    xs = [j * h for j in range(N)]564    us = [u(t) for t in xs]565    out = [(2 * us[j] - us[j - 1] - us[(j + 1) % N]) / (h * h) for j in range(N)]566    ex = [lap_exact(t) for t in xs]567    return norm(axpy(-1, out, ex)) / norm(ex)568 569 570em = max(apply_mult(16), apply_mult(64))571es16, es64 = apply_stencil(16), apply_stencil(64)572sy16 = (math.sin(5 * PI / 16) / (5 * PI / 16)) ** 2573sy64 = (math.sin(5 * PI / 64) / (5 * PI / 64)) ** 2574check('A1 a Fourier multiplier commutes with resampling of band-limited inputs; a stencil does so only to second order',575      em < 1e-12 and es16 > 5 * es64 > 0 and close(sy16, 0.7175, 1e-3) and close(sy64, 0.9801, 1e-3),576      'multiplier error %.0e on 16 and 64 points; stencil relative error %.3f and %.4f; its symbol at k=5 is %.4f and %.4f of k^2' % (em, es16, es64, sy16, sy64))577 578K = 4579c = {}580for k in range(1, K + 1):581    c[k] = complex(random.gauss(0, 1), random.gauss(0, 1))582    c[-k] = c[k].conjugate()583c[0] = complex(0.3, 0)