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

361    cs = [1]362    for k in range(1, n + 1):363        AM = [[sum(A[i][l] * M[l][j] for l in range(n)) for j in range(n)] for i in range(n)]364        c = -sum(AM[i][i] for i in range(n)) // k365        cs.append(c)366        M = [[AM[i][j] + (c if i == j else 0) for j in range(n)] for i in range(n)]367    return tuple(cs)368 369 370# ######################## CHECKS FOR THE PAPER ########################371def rk4t(F, y, t, h):372    k1 = F(t, y)373    k2 = F(t + 0.5 * h, [a + 0.5 * h * b for a, b in zip(y, k1)])374    k3 = F(t + 0.5 * h, [a + 0.5 * h * b for a, b in zip(y, k2)])375    k4 = F(t + h, [a + h * b for a, b in zip(y, k3)])376    return [a + h * (p + 2 * q + 2 * r + s) / 6 for a, p, q, r, s in zip(y, k1, k2, k3, k4)]377 378 379def flow(F, y, t0, t1, steps):380    h = (t1 - t0) / steps381    t = t0382    for _ in range(steps):383        y = rk4t(F, y, t, h)384        t += h385    return y386 387 388def norm(x):389    return math.sqrt(sum(a * a for a in x))390 391 392def gauss(x, m, v):393    return math.exp(-(x - m) ** 2 / (2 * v)) / math.sqrt(2 * PI * v)394 395 396def Phi(z):397    return 0.5 * (1 + math.erf(z / math.sqrt(2)))398 399 400sec('1. Padding is a boundary condition')401 402 403def conv_pad(x, w, mode):404    n = len(x)405    r = len(w) // 2406 407    def get(i):408        if 0 <= i < n:409            return x[i]410        if mode == 'zero':411            return 0.0412        if mode == 'circ':413            return x[i % n]414        i = i % (2 * n)415        return x[i] if i < n else x[2 * n - 1 - i]416    return [sum(w[j + r] * get(i + j) for j in range(-r, r + 1)) for i in range(n)]417 418 419n = 12420x = [random.gauss(0, 1) for _ in range(n)]421st = [-1.0, 2.0, -1.0]422e = max(norm(axpy(-1, conv_pad(x, st, m), mv(lap1(n, b), x))) for m, b in (('reflect', 'N'), ('zero', 'D'), ('circ', 'P')))423check('N1 the stencil (-1,2,-1) with reflect, zero and circular padding is the Neumann, Dirichlet and periodic Laplacian',424      e < 1e-12, 'max difference %.1e' % e)425 426res = 0.0427for k in range(n):428    v = [math.cos(PI * k * (j + 0.5) / n) for j in range(n)]429    res = max(res, norm(axpy(-4 * math.sin(PI * k / (2 * n)) ** 2, v, mv(lap1(n, 'N'), v))))430    v = [math.sin(PI * (k + 1) * (j + 1) / (n + 1)) for j in range(n)]431    res = max(res, norm(axpy(-4 * math.sin(PI * (k + 1) / (2 * (n + 1))) ** 2, v, mv(lap1(n, 'D'), v))))432    v = [math.cos(2 * PI * k * j / n) for j in range(n)]433    res = max(res, norm(axpy(-4 * math.sin(PI * k / n) ** 2, v, mv(lap1(n, 'P'), v))))434check('N2 the three paddings are diagonalized by the DCT-II, DST-I and DFT bases with eigenvalues 4 sin^2',435      res < 1e-12, 'max residual %.1e over 36 vectors' % res)436 437 438def even_ext(x):439    return list(x) + list(reversed(x))440 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',