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

1001    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 1021def sq_param(x):1022    m = max(abs(x[0]), abs(x[1]))1023    a, b = x[0] / m, x[1] / m1024    if a == 1.0 and b > -1.0:1025        return 1.0 + b1026    if b == 1.0:1027        return 3.0 - a1028    if a == -1.0:1029        return 5.0 - b1030    if b == -1.0:1031        return 7.0 + a1032    # not exactly on a side after division: choose the nearest side1033    cands = [(abs(a - 1), 1.0 + b), (abs(b - 1), 3.0 - a), (abs(a + 1), 5.0 - b), (abs(b + 1), 7.0 + a)]1034    return min(cands)[1]1035 1036 1037def fold2(x):1038    """a continuous piecewise linear map of the plane, linear on eight cones, of degree 2 on the boundary of the square"""1039    m = max(abs(x[0]), abs(x[1]))1040    p = sq_point(2.0 * sq_param(x))1041    return (m * p[0], m * p[1])1042 1043 1044def deep_curve(L, per_piece=4):1045    npc = 4 * 2 ** L1046    pts_ = []1047    for j in range(npc * per_piece):1048        x = sq_point(8.0 * j / (npc * per_piece))1049        for _ in range(L):1050            x = fold2(x)1051        pts_.append(x)1052    return pts_1053 1054 1055def curve_winding(pts_):1056    tot = 0.01057    for i in range(len(pts_)):1058        a, b = complex(*pts_[i]), complex(*pts_[(i + 1) % len(pts_)])1059        tot += cmath.phase(b / a)1060    return round(tot / (2 * PI))1061 1062 1063def count_pieces(pts_):1064    """number of maximal straight pieces of a closed polygon given by its points in order"""1065    n = len(pts_)1066    c = 01067    for i in range(n):1068        p, q, r_ = pts_[i - 1], pts_[i], pts_[(i + 1) % n]1069        cr = (q[0] - p[0]) * (r_[1] - q[1]) - (q[1] - p[1]) * (r_[0] - q[0])1070        if abs(cr) > 1e-9:1071            c += 11072    return c1073 1074 1075rg9 = random.Random(20261120)1076lin_ok = True1077for _ in range(200):1078    # linear on each cone: additive on pairs of points of the same cone, and positively homogeneous1079    th = rg9.uniform(0, 2 * PI)1080    k8 = int(((th + PI / 4) % (2 * PI)) / (PI / 4))1081    a0 = -PI / 4 + k8 * PI / 41082    t1, t2 = a0 + rg9.uniform(0.02, PI / 4 - 0.02), a0 + rg9.uniform(0.02, PI / 4 - 0.02)1083    r1, r2 = rg9.uniform(0.3, 2), rg9.uniform(0.3, 2)1084    x1, x2 = (r1 * math.cos(t1), r1 * math.sin(t1)), (r2 * math.cos(t2), r2 * math.sin(t2))1085    f1, f2, f12 = fold2(x1), fold2(x2), fold2((x1[0] + x2[0], x1[1] + x2[1]))1086    lin_ok = lin_ok and abs(f12[0] - f1[0] - f2[0]) < 1e-9 and abs(f12[1] - f1[1] - f2[1]) < 1e-91087deep = []1088for L in range(1, 7):1089    cv = deep_curve(L)1090    deep.append((L, curve_winding(cv), count_pieces(cv)))1091check('O9 depth multiplies degree: L compositions of one piecewise linear map with eight pieces wind 2^L times, with 4*2^L pieces, against the bound of more than 2^(L+1)',1092      lin_ok and all(w == 2 ** L and pc == 4 * 2 ** L and pc > 2 * w for L, w, pc in deep),1093      'map linear on each of eight cones (200 random pairs); (depth, winding, pieces): %s' % deep)1094 1095# --- O10: narrow networks1096SLOPE = 0.21097def lrelu(v):1098    return v if v > 0 else SLOPE * v1099def lrelu_inv(v):1100    return v if v > 0 else v / SLOPE