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 1461–1560 of 1604. Source text is displayed literally.
1461 break1462 # leave through the other 01-edge1463 other = edge[0] if lab(edge[0]) != lab(third) else edge[1]1464 new_edge = (other, third)1465 nxt = [c for c in cells_of_edge(*new_edge) if c not in tri]1466 if not nxt:1467 break1468 edge, third = new_edge, nxt[0]1469 return best1470 1471 1472walks = {n: door_walk(n) for n in (16, 32, 64, 128)}1473check('N3 finding a complete Sperner cell by walking through doors takes a number of steps proportional to the side of the grid, not to the number of cells',1474 all(walks[n] is not None for n in walks) and walks[128] < 6 * 128 and 1.5 < walks[128] / walks[64] < 2.6,1475 'steps %s for n = 16, 32, 64, 128; cells %s' % ([walks[n] for n in sorted(walks)], [n * n for n in sorted(walks)]))1476 1477 1478def circle_fixed(d, eps, g, n=6000):1479 h = lambda t: (d - 1) * t + eps * g(t)1480 tot = cnt = 01481 for m in range(-abs(d) - 6, abs(d) + 7):1482 prev = h(0.0) - 2 * PI * m1483 for i in range(1, n + 1):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: