Independent scholarship · Open accessThe monthly digest

Indexed text attachment

verify-topology-learning.py

Source for Algebraic and Differential Topology in Machine Learning and AI: Obstructions, Invariants, and Counts

View paper · Download original file

Lines 1484–1583 of 1604. Source text is displayed literally.

1484            cur = h(2 * PI * i / n) - 2 * PI * m1485            if prev == 0 or prev * cur < 0:1486                cnt += 11487                tot += -1 if cur > prev else 11488            prev = cur1489    return tot, cnt1490 1491 1492okcf = True1493info = []1494for d in (-2, 0, 1, 2, 3):1495    for eps in (0.3, 2.0):1496        g = trig_poly(4)1497        s, c = circle_fixed(d, eps, g)1498        okcf = okcf and s == 1 - d and c >= abs(1 - d) and (c - abs(1 - d)) % 2 == 01499        if eps == 2.0:1500            info.append('d=%d: %d points, signed %d' % (d, c, s))1501check('N4 fixed points of a circle map of degree d, counted with index, number 1 - d; a perturbed ring attractor (d = 1) has an even number, half of them stable',1502      okcf, '; '.join(info))1503 1504 1505def torus_zeros(eps):1506    def v(t):1507        return [math.sin(t[0]) + eps * math.sin(t[1] + 0.4), math.sin(t[1]) + eps * math.cos(t[0] - t[1])]1508    found = []1509    for i in range(16):1510        for j in range(16):1511            t = [2 * PI * (i + 0.37) / 16, 2 * PI * (j + 0.61) / 16]1512            for _ in range(50):1513                f0 = v(t)1514                h = 1e-61515                a = [(p - q) / (2 * h) for p, q in zip(v([t[0] + h, t[1]]), v([t[0] - h, t[1]]))]1516                b = [(p - q) / (2 * h) for p, q in zip(v([t[0], t[1] + h]), v([t[0], t[1] - h]))]1517                det = a[0] * b[1] - a[1] * b[0]1518                if abs(det) < 1e-12:1519                    break1520                t = [t[0] - (f0[0] * b[1] - f0[1] * b[0]) / det, t[1] - (a[0] * f0[1] - a[1] * f0[0]) / det]1521            if vnorm(v(t)) < 1e-10:1522                t = [x % (2 * PI) for x in t]1523                if not any(min(abs(t[0] - s[0]), 2 * PI - abs(t[0] - s[0])) + min(abs(t[1] - s[1]), 2 * PI - abs(t[1] - s[1])) < 1e-5 for s, _ in found):1524                    h = 1e-61525                    a = [(p - q) / (2 * h) for p, q in zip(v([t[0] + h, t[1]]), v([t[0] - h, t[1]]))]1526                    b = [(p - q) / (2 * h) for p, q in zip(v([t[0], t[1] + h]), v([t[0], t[1] - h]))]1527                    found.append((t, 1 if a[0] * b[1] - a[1] * b[0] > 0 else -1))1528    return found1529 1530 1531tz = {eps: torus_zeros(eps) for eps in (0.0, 0.3, 0.6)}1532check('N5 the zeros of a drift field on a torus attractor have total index 0: in three examples a source, a sink and two saddles',1533      all(sum(s for _, s in z) == 0 and len(z) >= 4 for z in tz.values()), 'zeros found for three perturbations: %s; index sums %s' % ([len(z) for z in tz.values()], [sum(s for _, s in z) for z in tz.values()]))1534 1535W1 = [[1.3, -0.4], [0.5, 1.1], [-0.8, 0.9], [0.2, -1.2]]1536B1 = [0.1, -0.2, 0.3, 0.05]1537W2 = [[0.9, 0.3, -0.5, 0.2], [-0.2, 0.8, 0.6, -0.7]]1538 1539 1540def net_curve(t, shift):1541    hdn = [math.tanh(W1[i][0] * math.cos(t) + W1[i][1] * math.sin(t) + B1[i]) for i in range(4)]1542    return complex(sum(W2[0][i] * hdn[i] for i in range(4)) + shift, sum(W2[1][i] * hdn[i] for i in range(4)))1543 1544 1545def net_winding(shift, n=4000):1546    tot = 0.01547    prev = net_curve(0.0, shift)1548    for i in range(1, n + 1):1549        cur = net_curve(2 * PI * i / n, shift)1550        tot += cmath.phase(cur / prev)1551        prev = cur1552    return round(tot / (2 * PI))1553 1554 1555w0 = net_winding(0.0)1556lo, hi = 0.0, 5.01557assert net_winding(hi) == 01558for _ in range(50):1559    mid_ = (lo + hi) / 21560    if net_winding(mid_) == w0:1561        lo = mid_1562    else:1563        hi = mid_1564bstar = (lo + hi) / 21565mn = min(abs(net_curve(2 * PI * i / 20000, bstar)) for i in range(20000))1566 1567 1568def signed_preimages(shift, alpha, n=4000):1569    tot = 01570    prev = cmath.phase(net_curve(0.0, shift) * cmath.exp(-1j * alpha))1571    for i in range(1, n + 1):1572        cur = cmath.phase(net_curve(2 * PI * i / n, shift) * cmath.exp(-1j * alpha))1573        if prev * cur < 0 and abs(prev - cur) < PI:1574            tot += 1 if cur > prev else -11575        prev = cur1576    return tot1577 1578 1579sp = [signed_preimages(0.0, a) for a in (0.3, 2.0, -1.7)]1580check('N6 the degree of a normalized network output is the signed number of preimages of any direction, and it changes only when the output passes through zero',1581      abs(w0) == 1 and all(s == w0 for s in sp) and net_winding(bstar - 1e-3) == w0 and net_winding(bstar + 1e-3) == 0 and mn < 1e-3,1582      'degree %d; signed preimages of three directions %s; degree drops to 0 at bias %.4f, where the smallest output norm is %.1e' % (w0, sp, bstar, mn))1583