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 441–540 of 1244. Source text is displayed literally.
441 442def evenness(z):443 return norm([a - b for a, b in zip(z, reversed(z))]) / norm(z)444 445 446ws = [0.2, -0.5, 1.0, -0.5, 0.2]447wa = [0.2, -0.5, 1.0, 0.7, -0.3]448d_restr = max(norm(axpy(-1, conv_pad(even_ext(x), w, 'circ')[:n], conv_pad(x, w, 'reflect'))) for w in (ws, wa))449ev_s = evenness(conv_pad(even_ext(x), ws, 'circ'))450ev_a = evenness(conv_pad(even_ext(x), wa, 'circ'))451 452 453def two_layer_defect(w1, w2):454 a = conv_pad(conv_pad(x, w1, 'reflect'), w2, 'reflect')455 b = conv_pad(conv_pad(even_ext(x), w1, 'circ'), w2, 'circ')[:n]456 return norm(axpy(-1, a, b)) / norm(b)457 458 459c_s, c_a = two_layer_defect(ws, wa), two_layer_defect(wa, ws)460check('N3 half-sample reflect padding agrees with circular convolution on the doubled signal followed by restriction to the original positions',461 d_restr < 1e-12 and ev_s < 1e-12 and ev_a > 0.1 and c_s < 1e-12 and c_a > 0.01,462 'restriction %.0e; output odd part: symmetric kernel %.0e, asymmetric %.3f; two-layer defect %.0e vs %.3f' % (d_restr, ev_s, ev_a, c_s, c_a))463 464 465def odd_ext(x):466 return [0.0] + list(x) + [0.0] + [-a for a in reversed(x)]467 468 469def zero_vs_odd(w):470 a = conv_pad(x, w, 'zero')471 b = conv_pad(odd_ext(x), w, 'circ')[1:n + 1]472 return norm(axpy(-1, a, b)) / norm(a)473 474 475z1, z2 = zero_vs_odd([0.3, 1.0, 0.3]), zero_vs_odd(ws)476check('N4 a three-point symmetric stencil agrees with the Dirichlet image construction, while the tested five-point stencil differs',477 z1 < 1e-12 and z2 > 0.01, 'relative difference: radius 1 %.0e, radius 2 %.3f' % (z1, z2))478 479 480def shift(x):481 return [0.0] + list(x[:-1])482 483 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):