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 1041–1140 of 1604. Source text is displayed literally.
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 / SLOPE1101 1102 1103def narrow_net(rg, depth=4):1104 layers = []1105 while len(layers) < depth:1106 A = [[rg.gauss(0, 1), rg.gauss(0, 1)], [rg.gauss(0, 1), rg.gauss(0, 1)]]1107 dt = A[0][0] * A[1][1] - A[0][1] * A[1][0]1108 if abs(dt) > 0.3:1109 layers.append((A, [rg.gauss(0, 0.5), rg.gauss(0, 0.5)], dt))1110 w = [rg.gauss(0, 1), rg.gauss(0, 1)]1111 return layers, w, rg.gauss(0, 0.5)1112 1113 1114def narrow_fwd(net, x):1115 layers, w, c = net1116 for A, b, _ in layers:1117 x = (lrelu(A[0][0] * x[0] + A[0][1] * x[1] + b[0]), lrelu(A[1][0] * x[0] + A[1][1] * x[1] + b[1]))1118 return w[0] * x[0] + w[1] * x[1] + c1119 1120 1121def narrow_inv(net, y):1122 layers = net[0]1123 for A, b, dt in reversed(layers):1124 z = (lrelu_inv(y[0]) - b[0], lrelu_inv(y[1]) - b[1])1125 y = ((A[1][1] * z[0] - A[0][1] * z[1]) / dt, (-A[1][0] * z[0] + A[0][0] * z[1]) / dt)1126 return y1127 1128 1129rg10 = random.Random(20261121)1130ok_conn = ok_unb = ok_circ = True1131for _ in range(30):1132 net = narrow_net(rg10)1133 layers, w, c = net1134 nw = math.hypot(*w)1135 nrm = (w[0] / nw, w[1] / nw)1136 tng = (-nrm[1], nrm[0])1137 base = (-c / nw * nrm[0], -c / nw * nrm[1])1138 for sgn in (1, -1):1139 # two random points of the half-plane on one side of the last layer; the straight path between them, pulled back1140 ys = []