solution = sv.fiber_solver(**fiber_kwargs) V1 = np.concatenate(solution["Fiber trace"].points, axis=1)[:-1,::10] FX1 = solution["Fixed points"] z = solution["Fiber trace"].z_initial print("Status: %s\n"%solution["Fiber trace"].status) # Run in other direction (negate initial tangent) fiber_kwargs["z"] = -z solution = sv.fiber_solver(**fiber_kwargs) V2 = np.concatenate(solution["Fiber trace"].points, axis=1)[:-1,::10] FX2 = solution["Fixed points"] print("Status: %s\n"%solution["Fiber trace"].status) # Join fiber segments V = np.concatenate((np.fliplr(V1), V2), axis=1) FX = np.concatenate((FX1, FX2), axis=1) X_grid, Y_grid = np.mgrid[-10:10:60j,-10:10:60j] ax = pt.gcf().add_subplot(2,1,2) tv.plot_fiber(X_grid, Y_grid, V[:,::20], f, ax=ax, scale_XY=10, scale_V=20) pt.plot(FX[0],FX[1], 'ko') pt.show() # v = np.zeros((2,1)) # print(levy(v)) # print(f(v).T) # v = np.ones((2,1)) # print(levy(v)) # print(f(v).T)
x_lork = (b+1)/(-2*a)*np.ones(2) y_lork = np.array([-2, 2]) # Fixed points x_fx = ((b+1) + np.array([-1, 1])*np.sqrt((b+1)**2 + 4*a))/(-2*a) y_fx = b*x_fx # Grids for fiber and attractor X_fiber, Y_fiber = np.mgrid[-2:2:20j, -2:2:20j] X_a, Y_a = np.mgrid[-1:1:100j, -1:1:100j] # Compute attractor points V_a = np.array([X_a.flatten(), Y_a.flatten()]) for u in range(11): V_a = V_a + f(V_a) V_a = V_a[:,np.isfinite(V_a).all(axis=0)] V_a = V_a[:,(np.fabs(V_a) < 2).all(axis=0)] # Visualize fiber and attractor pt.figure(figsize=(3.5,3.5)) ax_fiber = pt.gca() ax_fiber.scatter(*V_a, marker='o', s=2, color=((0.3, 0.3, 0.3),)) # attractor tv.plot_fiber(X_fiber, Y_fiber, V[:,::10], f, ax=ax_fiber, scale_XY=10, scale_V=10) ax_fiber.plot(x_fx, y_fx, 'ko') # fixed points # ax_fiber.plot(x_lork, y_lork, 'r-') # low-rank Df points ax_fiber.set_xlabel("x") ax_fiber.set_ylabel("y",rotation=0) pt.yticks(np.linspace(-2,2,5)) pt.tight_layout() pt.show()
X_fiber, Y_fiber = np.mgrid[-2.5:3.5:40j, -1:4:40j] X_surface, Y_surface = np.mgrid[-2.5:3.5:100j, -1:4:100j] # Low rank Df curve x_lork = np.linspace(-2.5, 3.5, 40) y_lork = (2 + (12 - 8 * b) * x_lork**2) / (4 * b) # Compute rosenbrock R = (a - X_surface)**2 + b * (Y_surface - X_surface**2)**2 # Visualize fiber and surface ax_fiber = pt.subplot(2, 1, 2) tv.plot_fiber(X_fiber, Y_fiber, V, f, ax=ax_fiber, scale_XY=500, scale_V=25) ax_fiber.plot([1], [1], 'ko') # global optimum # ax_fiber.plot(x_lork, y_lork, 'r-') # low-rank Df points ax_surface = pt.gcf().add_subplot(2, 1, 1, projection="3d") ax_surface.plot_surface(X_surface, Y_surface, R, linewidth=0, antialiased=False, color='gray') ax_surface.view_init(azim=-98, elev=21) for ax in [ax_fiber, ax_surface]: ax.set_xlabel("x")
def sanity2D(): # Set up fiber arguments v = np.array([[2], [.5]]) fiber_kwargs = { "f": f, "ef": ef, "Df": Df, "compute_step_amount": compute_step_amount, "v": v, "c": f(v), "max_step_size": 1, "max_traverse_steps": 2000, "max_solve_iterations": 2**5, } # Run in one direction solution = sv.fiber_solver(**fiber_kwargs) V1 = np.concatenate(solution["Fiber trace"].points, axis=1)[:-1, ::10] z = solution["Fiber trace"].z_initial # Run in other direction (negate initial tangent) fiber_kwargs["z"] = -z solution = sv.fiber_solver(**fiber_kwargs) V2 = np.concatenate(solution["Fiber trace"].points, axis=1)[:-1, ::10] # Join fiber segments V = np.concatenate((np.fliplr(V1), V2), axis=1) # Grids for fiber and surface X_fiber, Y_fiber = np.mgrid[-2.5:3.5:40j, -1:4:40j] X_surface, Y_surface = np.mgrid[-2.5:3.5:100j, -1:4:100j] # Compute rosenbrock R_surface = R(np.array([X_surface.flatten(), Y_surface.flatten()])) R_surface = R_surface.reshape(X_surface.shape) # Visualize fiber and surface ax_fiber = pt.subplot(2, 1, 2) tv.plot_fiber(X_fiber, Y_fiber, V, f, ax=ax_fiber, scale_XY=500, scale_V=25) ax_fiber.plot([1], [1], 'ko') # global optimum ax_surface = pt.gcf().add_subplot(2, 1, 1, projection="3d") ax_surface.plot_surface(X_surface, Y_surface, R_surface, linewidth=0, antialiased=False, color='gray') ax_surface.view_init(azim=-98, elev=21) for ax in [ax_fiber, ax_surface]: ax.set_xlabel("x") ax.set_ylabel("y") if ax == ax_surface: ax.set_zlabel("R(x,y)", rotation=90) ax.set_zlim([0, 10000]) ax.view_init(elev=40, azim=-106) ax.set_xlim([-2.5, 3.5]) pt.show()
if __name__ == "__main__": # Set up 2D network N = 2 W = 1.25 * np.eye(N) + 0.1 * np.random.randn(N, N) logger = lu.Logger(sys.stdout) fxpts, solution = run_fiber_solver( W, # max_history=100, compute_step_amount=compute_step_amount_factory3(W), logger=logger, abs_alpha_min=True, within_fiber=True) # Extract steps along fiber and corresponding f(v)'s trace = solution["Fiber trace"] X = np.concatenate(trace.points, axis=1) X = np.concatenate((-np.fliplr(X), X), axis=1) V = X[:-1, :] # Plot fiber and fixed points X_grid, Y_grid = np.mgrid[-1.15:1.15:20j, -1.15:1.15:20j] plt.figure(figsize=(5, 4.5)) tv.plot_fiber(X_grid, Y_grid, V, f_factory(W), scale_XY=1, scale_V=1) plt.scatter(*fxpts, color='k', marker='o') plt.xlabel("v1") plt.ylabel("v2", rotation=0) plt.tight_layout() plt.show()
fxpts = fx.sanitize_points( V_fx, f, ef, Df, duplicates=lambda V, v: (np.fabs(V - v) < 10**-6).all(axis=0), ) print("Fxpts:") print(fxpts) print("Residuals:") print(ef(fxpts)) # Visualize fiber if 2d if N == 2: X_f, Y_f = np.mgrid[-2:2:15j, -2:2:15j] V = V[:, \ (V[0,:] >= X_f.min()) & \ (V[0,:] <= X_f.max()) & \ (V[1,:] >= Y_f.min()) & \ (V[1,:] <= Y_f.max())] # V = V[:,::20] tv.plot_fiber(X_f, Y_f, V, f, scale_XY=8, scale_V=5) pt.plot(*fxpts, marker='o', color='k', linestyle='none') pt.xlabel("x") pt.ylabel("y") pt.show()