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

821            step = max(abs(du), abs(dv))822            sc = min(1.0, 0.2 / step) if step > 0 else 1.0823            u, v = u - sc * du, v - sc * dv824        best = min(best, vnorm(g(u, v)))825        if best < 1e-11:826            break827    return best828 829 830res2 = [find_zero_sphere(cubic_odd()) for _ in range(40)]831check('O2 Borsuk-Ulam on the sphere: every continuous code e: S^2 -> R^2 identifies a pair of antipodal points', max(res2) < 1e-9,832      '40 random cubic encoders; largest residual |e(x) - e(-x)| at the point found %.1e' % max(res2))833 834 835def bottleneck_radius(e, M=3000, bins=100):836    ts = [2 * PI * i / M for i in range(M)]837    cs = [e(t) for t in ts]838    lo, hi = min(cs), max(cs)839    groups = {}840    for t, c in zip(ts, cs):841        groups.setdefault(min(bins - 1, int((c - lo) / (hi - lo) * bins)), []).append(t)842    best = 0.0843    for b, g in groups.items():844        g = sorted(g)845        gap = max([g[i + 1] - g[i] for i in range(len(g) - 1)] + [g[0] + 2 * PI - g[-1]])846        arc = 2 * PI - gap847        best = max(best, 1.0 if arc >= PI else math.sin(arc / 2))848    return best849 850 851rad = [bottleneck_radius(trig_poly()) for _ in range(100)]852tight = max(abs(math.sin(2 * PI * i / 1000)) for i in range(1000))853check('O3 a one-dimensional bottleneck for the unit circle has worst-case reconstruction error at least 1, and the code cos(theta) attains 1',854      min(rad) > 0.995 and close(tight, 1.0, 1e-9), 'smallest radius of a fibre of the code over 100 random encoders: %.4f; error of the projection code: %.4f' % (min(rad), tight))855 856 857def qmul(a, b):858    return (a[0] * b[0] - a[1] * b[1] - a[2] * b[2] - a[3] * b[3], a[0] * b[1] + a[1] * b[0] + a[2] * b[3] - a[3] * b[2],859            a[0] * b[2] - a[1] * b[3] + a[2] * b[0] + a[3] * b[1], a[0] * b[3] + a[1] * b[2] - a[2] * b[1] + a[3] * b[0])860 861 862def qrotf(q):863    w, x, y, z = q864    return [[1 - 2 * (y * y + z * z), 2 * (x * y - w * z), 2 * (x * z + w * y)],865            [2 * (x * y + w * z), 1 - 2 * (x * x + z * z), 2 * (y * z - w * x)],866            [2 * (x * z - w * y), 2 * (y * z + w * x), 1 - 2 * (x * x + y * y)]]867 868 869def axis_q(u, th):870    n = vnorm(u)871    return (math.cos(th / 2),) + tuple(math.sin(th / 2) * a / n for a in u)872 873 874def canon(q):875    s = 1 if (q[0] > 1e-12 or (abs(q[0]) <= 1e-12 and next((a for a in q[1:] if abs(a) > 1e-12), 1) > 0)) else -1876    return tuple(s * a for a in q)877 878 879def euler(R):880    return (math.atan2(R[1][0], R[0][0]), math.asin(max(-1.0, min(1.0, -R[2][0]))), math.atan2(R[2][1], R[2][2]))881 882 883def six(R):884    return [R[0][0], R[1][0], R[2][0], R[0][1], R[1][1], R[2][1]]885 886 887def gram_schmidt(s):888    a1, a2 = s[:3], s[3:]889    n1 = vnorm(a1)890    b1 = [x / n1 for x in a1]891    d = sum(x * y for x, y in zip(b1, a2))892    c = [y - d * x for x, y in zip(b1, a2)]893    n2 = vnorm(c)894    b2 = [x / n2 for x in c]895    b3 = [b1[1] * b2[2] - b1[2] * b2[1], b1[2] * b2[0] - b1[0] * b2[2], b1[0] * b2[1] - b1[1] * b2[0]]896    return [[b1[i], b2[i], b3[i]] for i in range(3)]897 898 899def fro(A, B):900    return math.sqrt(sum((a - b) ** 2 for r, q in zip(A, B) for a, b in zip(r, q)))901 902 903Nst = 2000904u0 = [0.3, -0.5, 0.8]905jq = je = jm = j6 = 0.0906prevR = prevq = preve = None907for i in range(Nst + 1):908    th = 2 * PI * i / Nst909    R = qrotf(axis_q(u0, th))910    Ry = qrotf(axis_q([0, 1, 0], th))911    q, e = canon(axis_q(u0, th)), euler(Ry)912    if prevR is not None:913        jm = max(jm, fro(R, prevR))914        jq = max(jq, vnorm([a - b for a, b in zip(q, prevq)]))915        je = max(je, max(abs(a - b) for a, b in zip(e, preve)))916        j6 = max(j6, vnorm([a - b for a, b in zip(six(R), six(prevR))]))917    prevR, prevq, preve = R, q, e918check('O4 along a full turn the rotation matrix moves by small steps, the canonical quaternion jumps by 2 and the Euler angles by pi; the six-dimensional code does not jump',919      jm < 0.01 and close(jq, 2.0, 1e-3) and close(je, PI, 1e-2) and j6 <= jm + 1e-12,920      'largest step over 2000 steps: matrix %.4f, quaternion %.4f, Euler angles %.4f, 6D code %.4f' % (jm, jq, je, j6))