Indexed text attachment
verify-pde-learning.py
Source for Partial Differential Equations in Machine Learning and AI: Naturality, Well-Posedness, and Spectral Rates
View paper · Download original file
Lines 1121–1220 of 1244. Source text is displayed literally.
1121check('L1 the Gibbs density is stationary for the Fokker-Planck operator, and for a quadratic loss the spectral gap is the curvature at every temperature',1122 rs1 < 1e-12 and rs4 < 1e-12 and abs(ev1[0]) < 1e-8 and close(ev1[1], 1, 5e-3) and close(ev4[1], 1, 5e-3),1123 'gap %.4f at beta = 1 and %.4f at beta = 4' % (ev1[1], ev4[1]))1124 1125kr = {}1126for beta in (4.0, 6.0, 8.0, 10.0):1127 ev, _ = fp_gap(lambda x: (x * x - 1) ** 2, -2.2, 2.2, 880, beta)1128 kr[beta] = (ev[1], math.sqrt(32) / PI * math.exp(-beta))1129rt = {b_: kr[b_][0] / kr[b_][1] for b_ in kr}1130check('L2 in a double well the gap is the Kramers rate (sqrt(32)/pi) e^{-beta*barrier}, exponentially small in the inverse temperature',1131 all(0.7 < v < 1.05 for v in rt.values()) and abs(rt[10.0] - 1) < abs(rt[4.0] - 1) and abs(rt[10.0] - 1) < 0.12,1132 'gap / Kramers = %s at beta = 4, 6, 8, 10; gap at beta = 10 is %.2e' % (', '.join('%.3f' % rt[b_] for b_ in sorted(rt)), kr[10.0][0]))1133 1134eta, lam_, sig = 0.1, 2.0, 1.51135v = 0.01136for _ in range(2000):1137 v = (1 - eta * lam_) ** 2 * v + eta ** 2 * sig ** 21138check('L3 constant-step SGD on a quadratic has stationary variance eta*sigma^2/(lambda(2-eta*lambda)): a temperature eta*sigma^2/2 to first order',1139 close(v, eta * sig ** 2 / (lam_ * (2 - eta * lam_)), 1e-12), 'variance %.5f; first-order value %.5f' % (v, eta * sig ** 2 / (2 * lam_)))1140 1141sec('9. Control: the Bellman operator and viscosity solutions')1142nS, nA, gam = 6, 3, 0.91143Pm = []1144for a_ in range(nA):1145 rows = []1146 for s in range(nS):1147 w = [random.random() for _ in range(nS)]1148 t_ = sum(w)1149 rows.append([q / t_ for q in w])1150 Pm.append(rows)1151cost = [[random.random() for _ in range(nS)] for _ in range(nA)]1152 1153 1154def bell(V):1155 return [min(cost[a_][s] + gam * dot(Pm[a_][s], V) for a_ in range(nA)) for s in range(nS)]1156 1157 1158def sup(x):1159 return max(abs(a_) for a_ in x)1160 1161 1162okc = okm = True1163worst = 0.01164for _ in range(300):1165 V = [random.gauss(0, 3) for _ in range(nS)]1166 Wv = [random.gauss(0, 3) for _ in range(nS)]1167 q = sup(axpy(-1, bell(V), bell(Wv))) / sup(axpy(-1, V, Wv))1168 worst = max(worst, q)1169 okc = okc and q <= gam + 1e-121170 Up = [a_ + abs(random.gauss(0, 1)) for a_ in V]1171 okm = okm and all(p_ <= q_ + 1e-12 for p_, q_ in zip(bell(V), bell(Up)))1172Vs = [0.0] * nS1173for _ in range(2000):1174 Vs = bell(Vs)1175V = [0.0] * nS1176e0 = sup(axpy(-1, V, Vs))1177okr = True1178for k in range(1, 60):1179 V = bell(V)1180 okr = okr and sup(axpy(-1, V, Vs)) <= gam ** k * e0 + 1e-121181check('Q1 the Bellman operator is monotone and a sup-norm contraction of factor gamma, so value iteration converges geometrically',1182 okc and okm and okr and sup(axpy(-1, bell(Vs), Vs)) < 1e-12, 'largest observed contraction ratio %.4f (gamma = %.1f) over 300 pairs' % (worst, gam))1183 1184n = 201185h = 1.0 / n1186u = [0.0] + [10.0] * (n - 1) + [0.0]1187for _ in range(200):1188 for i in range(1, n):1189 u[i] = min(u[i - 1], u[i + 1]) + h1190dist = [min(i * h, 1 - i * h) for i in range(n + 1)]1191ev_ = max(abs(a_ - b_) for a_, b_ in zip(u, dist))1192saw = [0.0, h, 2 * h, h, 0.0, h, 2 * h, 3 * h, 4 * h, 5 * h, 6 * h, 7 * h, 8 * h, 7 * h, 6 * h, 5 * h, 4 * h, 3 * h, 2 * h, h, 0.0]1193res_saw = max(abs(saw[i] - (min(saw[i - 1], saw[i + 1]) + h)) for i in range(1, n))1194neg = [-d for d in dist]1195res_neg = max(abs(neg[i] - (min(neg[i - 1], neg[i + 1]) + h)) for i in range(1, n))1196check('Q2 the monotone scheme for |u\'| = 1 converges to the distance function and rejects the other almost-everywhere solutions',1197 ev_ < 1e-12 and res_saw > h and res_neg > h, 'error %.0e; residual of a sawtooth %.2f and of minus the distance %.2f (grid step %.2f)' % (ev_, res_saw, res_neg, h))1198 1199sec('10. How many numbers a solution operator needs')1200n = 631201lamk = [4 * 64 * 64 * math.sin(k * PI / 128) ** 2 for k in range(1, n + 1)]1202ell = [1 / v for v in lamk]1203par = [math.exp(-0.01 * v) for v in lamk]1204rank_e = sum(1 for v in ell if v / ell[0] > 1e-6)1205rank_p = sum(1 for v in par if v / par[0] > 1e-6)1206cont_p = max(k for k in range(1, 100) if math.exp(-0.01 * PI * PI * (k * k - 1)) > 1e-6)1207n2 = 151208A2 = [[v * 16 * 16 for v in row] for row in lap1(n2, 'D')]1209l15, v15 = jacobi_eig(A2)1210tt = 0.371211U = [[0.0] * (2 * n2) for _ in range(2 * n2)]1212for k in range(n2):1213 w_ = math.sqrt(l15[k])1214 cs, sn = math.cos(w_ * tt), math.sin(w_ * tt)1215 for i in range(n2):1216 for j in range(n2):1217 pij = v15[k][i] * v15[k][j]1218 U[i][j] += cs * pij1219 U[i][n2 + j] += sn / w_ * pij1220 U[n2 + i][j] += -w_ * sn * pij