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 921–1020 of 1604. Source text is displayed literally.
921 922ok6 = True923lip = 0.0924for _ in range(200):925 q = [random.gauss(0, 1) for _ in range(4)]926 n = vnorm(q)927 R = qrotf(tuple(a / n for a in q))928 ok6 = ok6 and fro(gram_schmidt(six(R)), R) < 1e-12929 noise = [random.gauss(0, 1e-3) for _ in range(6)]930 R2 = gram_schmidt([a + b for a, b in zip(six(R), noise)])931 lip = max(lip, fro(R2, R) / vnorm(noise))932check('O5 the six-dimensional code (two columns, then Gram-Schmidt) is a continuous right inverse with a Lipschitz decoder', ok6 and lip < 3.0,933 '200 random rotations recovered to 1e-12; decoder moves by at most %.2f times a perturbation of the code' % lip)934 935 936def link_cycle(K, v):937 edges = [tuple(a for a in t if a != v) for t in K.S[2] if v in t]938 nxt = {}939 for a, b in edges:940 nxt.setdefault(a, []).append(b)941 nxt.setdefault(b, []).append(a)942 start = edges[0][0]943 cyc = [start]944 prev, cur = None, start945 while True:946 cand = [w for w in nxt[cur] if w != prev]947 if not cand:948 break949 w = cand[0]950 if w == start:951 break952 cyc.append(w)953 prev, cur = cur, w954 return cyc955 956 957def index_sum(K):958 h = {x[0]: random.random() for x in K.S[0]}959 tot = 0960 for x in K.S[0]:961 v = x[0]962 cyc = link_cycle(K, v)963 s = [h[w] > h[v] for w in cyc]964 changes = sum(1 for i in range(len(s)) if s[i] != s[(i + 1) % len(s)])965 tot += Fr(2 - changes, 2)966 return tot967 968 969surfs = [('sphere', simplex_boundary(2), 2), ('icosahedron', icosahedron(), 2), ('torus', torus7(), 0), ('projective plane', rp2(), 1), ('Klein bottle', klein(), 0)]970okidx = all(index_sum(K) == chi for _, K, chi in surfs for _ in range(100))971check('O6 hairy ball on meshes: the indices of the critical vertices of any generic function on a triangulated surface add up to the Euler characteristic',972 okidx, '100 random functions on each of five surfaces; sums 2, 2, 0, 1, 0')973 974okcp = True975for _ in range(50):976 k = 6977 xs = sorted(random.random() for _ in range(k))978 knots = [0.0] + xs + [1.0]979 ys = [random.gauss(0, 1) for _ in knots]980 slopes = [(ys[i + 1] - ys[i]) / (knots[i + 1] - knots[i]) for i in range(len(knots) - 1)]981 def net(x):982 return ys[0] + slopes[0] * x + sum((slopes[i] - slopes[i - 1]) * max(x - knots[i], 0.0) for i in range(1, len(slopes)))983 def pl(x):984 i = max(j for j in range(len(knots) - 1) if knots[j] <= x)985 return ys[i] + slopes[i] * (x - knots[i])986 okcp = okcp and all(abs(net(x) - pl(x)) < 1e-10 for x in [random.random() for _ in range(40)])987check('O7 a continuous piecewise linear function with k breakpoints is an affine function plus k rectifier units, exactly', okcp, '50 random functions with 6 breakpoints')988 989 990def polygon_winding(d, N):991 tot = 0.0992 for j in range(N):993 a = cmath.exp(2j * PI * d * j / N)994 b = cmath.exp(2j * PI * d * (j + 1) / N)995 tot += cmath.phase(b / a)996 return round(tot / (2 * PI))997 998 999def rep(d, N):1000 r = d % N1001 return r - N if r > N / 2 else r1002 1003 1004cases = [(3, 5), (3, 7), (3, 12), (5, 8), (2, 5), (4, 9), (-3, 7), (7, 10)]1005wind = {c: polygon_winding(*c) for c in cases}1006check('O8 a closed piecewise linear curve with N pieces through the points of z -> z^d winds d times only if N > 2|d|; otherwise it winds d reduced modulo N',1007 all(wind[c] == rep(*c) for c in cases) and wind[(3, 5)] == -2 and wind[(3, 7)] == 3, 'windings %s' % wind)1008 1009# --- O9: depth multiplies degree (own generator, so that later checks keep their random inputs)1010def sq_point(u):1011 u %= 8.01012 if u < 2:1013 return (1.0, u - 1.0)1014 if u < 4:1015 return (3.0 - u, 1.0)1016 if u < 6:1017 return (-1.0, 5.0 - u)1018 return (u - 7.0, -1.0)1019 1020