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 501–600 of 1244. Source text is displayed literally.
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)584 585 586def ub(x):587 return sum(c[k] * cmath.exp(1j * k * x) for k in c).real588 589 590exact = {k: sum(c[p] * c[k - p] for p in c if (k - p) in c) for k in range(-K, K + 1)}591 592 593def alias_err(N):594 xs = [2 * PI * j / N for j in range(N)]595 sq = [ub(t) ** 2 for t in xs]596 return max(abs(coef(sq, k) - exact[k]) for k in range(-K, K + 1))597 598 599e13, e12, e9 = alias_err(3 * K + 1), alias_err(3 * K), alias_err(2 * K + 1)600check('A2 squaring a signal of band K is alias-free in the band exactly when the grid has more than 3K points',