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 1524–1604 of 1604. Source text is displayed literally.

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 1584 1585# --- N7: zeros of odd networks (own generator)1586rg12 = random.Random(20261124)1587hist = {}1588ok_odd = True1589for _ in range(200):1590    units = [(rg12.gauss(0, 2), rg12.gauss(0, 2), rg12.gauss(0, 1)) for _ in range(6)]1591    fo = lambda t: sum(c_ * math.tanh(a_ * math.cos(t) + b_ * math.sin(t)) for a_, b_, c_ in units)1592    M_ = 200001593    vals_ = [fo(2 * PI * (i + 0.5) / M_) for i in range(M_)]1594    z_ = sum(1 for i in range(M_) if vals_[i - 1] * vals_[i] < 0)1595    hist[z_] = hist.get(z_, 0) + 11596    ok_odd = ok_odd and z_ % 4 == 21597# Each tanh is bounded by 1 in absolute value, so this is a global lower bound.1598biased_lower_bound = 10.0 - sum(abs(weight) for _, _, weight in units)1599check('N7 the sampled sign crossings of the scalar odd networks number 2 modulo 4',1600      ok_odd and biased_lower_bound > 0,1601      '200 random networks with six tanh units: sign crossings on the unit circle %s; with output bias 10 the last output is at least %.4f' % (sorted(hist.items()), biased_lower_bound))1602 1603footer()1604