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 1084–1183 of 1604. Source text is displayed literally.
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 = []1141 for _ in range(2):1142 un, ut = sgn * rg10.uniform(0.1, 30), rg10.uniform(-30, 30)1143 ys.append((base[0] + un * nrm[0] + ut * tng[0], base[1] + un * nrm[1] + ut * tng[1]))1144 for i in range(41):1145 t = i / 401146 x = narrow_inv(net, ((1 - t) * ys[0][0] + t * ys[1][0], (1 - t) * ys[0][1] + t * ys[1][1]))1147 ok_conn = ok_conn and sgn * narrow_fwd(net, x) > 01148 # a ray inside the half-plane, pulled back, leaves every bounded set1149 nr = [math.hypot(*narrow_inv(net, (base[0] + sgn * nrm[0] + s_ * tng[0], base[1] + sgn * nrm[1] + s_ * tng[1]))) for s_ in (1e2, 1e3, 1e4)]1150 ok_unb = ok_unb and nr[0] < nr[1] < nr[2] and nr[2] > 501151 r0 = max(1.0, math.hypot(*narrow_inv(net, base))) # a point of the decision boundary lies at this distance1152 for R_ in (10 * r0, 100 * r0, 1000 * r0):1153 sg = {narrow_fwd(net, (R_ * math.cos(2 * PI * i / 3600), R_ * math.sin(2 * PI * i / 3600))) > 0 for i in range(3600)}1154 ok_circ = ok_circ and sg == {True, False}1155tri = [(math.cos(PI / 2 + 2 * PI * k / 3), math.sin(PI / 2 + 2 * PI * k / 3)) for k in range(3)]1156def island(x):1157 return 1.0 - sum(max(0.0, a[0] * x[0] + a[1] * x[1]) for a in tri)1158isl_out = max(island((4 * math.cos(2 * PI * i / 720), 4 * math.sin(2 * PI * i / 720))) for i in range(720))1159check('O10 width: with two inputs, two units per layer and an injective activation both decision regions are connected and unbounded; three units can enclose a bounded region',1160 ok_conn and ok_unb and ok_circ and island((0.0, 0.0)) > 0 and isl_out < 0,1161 '30 random networks of depth 4: explicit paths inside each region, both classes on three circles of radius 10 to 1000 times the distance of the boundary; three-unit island: value %.1f at the centre, at most %.2f on the circle of radius 4' % (island((0.0, 0.0)), isl_out))1162 1163# --- O11: a global frame on the 3-sphere1164rg11 = random.Random(20261122)1165fr_err = 0.01166for _ in range(200):1167 q = [rg11.gauss(0, 1) for _ in range(4)]1168 n_ = vnorm(q)1169 q = tuple(a / n_ for a in q)1170 fr = [qmul(q, e_) for e_ in ((0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1))]1171 vs_ = [q] + fr1172 for i in range(4):1173 for j in range(4):1174 fr_err = max(fr_err, abs(sum(a * b for a, b in zip(vs_[i], vs_[j])) - (1.0 if i == j else 0.0)))1175check('O11 the 3-sphere has a global frame: at every unit quaternion q the vectors qi, qj, qk are orthonormal and tangent', fr_err < 1e-12,1176 '200 random points; largest deviation from orthonormality %.1e' % fr_err)1177 1178sec('2. Invariants')1179 1180 1181def hodge(K, k):1182 n = K.n(k)1183 L = [[0] * n for _ in range(n)]