Beispiel #1
0
    ax_surface = pt.gcf().add_subplot(2,1,1,projection="3d")
    ax_surface.plot_surface(X_surface, Y_surface, L, linewidth=0, antialiased=False, color='gray')

    # Set up fiber arguments
    # v = 0.*np.ones((2,1)) + 1*np.random.randn(2,1)
    v = 20*np.random.rand(2,1) - 10
    c = f(v)
    c = c + 0.1*np.random.randn(2,1)
    fiber_kwargs = {
        "f": f,
        "ef": ef,
        "Df": Df,
        "compute_step_amount": lambda trace: (0.001, 0, False),
        "v": v,
        "c": c,
        "logger": lu.Logger(sys.stdout),
        "max_step_size": 1,
        "terminate": lambda trace: (np.fabs(trace.x[:-1]) > 100).any(),
        "max_traverse_steps": 20000,
        "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]
    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
Beispiel #2
0
def run_trials(args):
    basename, proc, N, timeout = args

    # Reseed randomness, start logging, start timing
    np.random.seed()
    logfile = open("%s_p%d_N%d.log" % (basename, proc, N), "w")
    logger = lu.Logger(logfile)
    stop_time = time.clock() + timeout

    # Keep sampling until timeout
    for sample in it.count(0):

        # Sample new network
        f, Df, ef, W, V = rnn.make_known_fixed_points(N)
        dups = rnn.duplicates_factory(W)
        results = {}
        data = {"W": W}

        # Keep fiber-solving until some c finds all V or timeout
        for trial in it.count(0):
            print("proc %d, N %d, sample %d, trial %d" %
                  (proc, N, sample, trial))

            # Run fiber solver
            start_time = time.clock()
            fxpts, solution = rnn.run_fiber_solver(
                W,
                # max_traverse_steps = 2**15,
                stop_time=stop_time,
                logger=logger.plus_prefix("(%d,%d,%d): " % (N, sample, trial)))
            run_time = time.clock() - start_time

            # Update union of fxpts
            V = fx.sanitize_points(
                np.concatenate((fxpts, V), axis=1),
                f,
                ef,
                Df,
                duplicates=dups,
            )

            # Save results
            trace = solution["Fiber trace"]
            results[trial] = {
                "status": trace.status,
                "seconds": run_time,
                "|fxpts|": fxpts.shape[1],
                "|V|": V.shape[1],
            }
            results["trials"] = trial + 1
            data[str((trial, "c"))] = trace.c
            data[str((trial, "fxpts"))] = fxpts
            data["V"] = V

            sample_basename = "%s_p%d_N%d_s%d" % (basename, proc, N, sample)
            with open(sample_basename + ".pkl", 'w') as rf:
                pk.dump(results, rf)
            with open(sample_basename + ".npz", 'w') as df:
                np.savez(df, **data)

            print("%d,%d,%d,%d: status %s, |fxpts|=%d, |V|=%d" %
                  (proc, N, sample, trial, trace.status, fxpts.shape[1],
                   V.shape[1]))

            # Done if current c found full union
            if fxpts.shape[1] == V.shape[1]: break

            # Done if timeout
            if time.clock() > stop_time: break

        print("%d,%d,%d: %d trials" % (proc, N, sample, trial + 1))
        if time.clock() > stop_time: break

    proc_basename = "%s_p%d_N%d" % (basename, proc, N)
    with open(proc_basename + ".pkl", 'w') as rf:
        pk.dump({"num_samples": sample + 1}, rf)

    logfile.close()
Beispiel #3
0
        ef=ef_factory(W),
        Df=Df_factory(W),
        duplicates=duplicates_factory(W),
    )

    # Return post-processed fixed points and full solver result
    return fxpts, solution


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
Beispiel #4
0
def run_trial(args):
    basename, sample, timeout = args
    stop_time = time.clock() + timeout

    logfile = open("%s_s%d.log" % (basename, sample), "w")

    # Set up fiber arguments
    np.random.seed()
    v = 20 * np.random.rand(2, 1) - 10  # random point in domain
    c = lv.f(v)  # direction at that point
    c = c + 0.1 * np.random.randn(2, 1)  # perturb for more variability
    fiber_kwargs = {
        "f": lv.f,
        "ef": lv.ef,
        "Df": lv.Df,
        "compute_step_amount": lambda trace: (0.0001, 0),
        "v": v,
        "c": c,
        "stop_time": stop_time,
        "terminate": lambda trace: (np.fabs(trace.x[:-1]) > 10).any(),
        "max_solve_iterations": 2**5,
    }

    solve_start = time.clock()

    # Run in one direction
    solution = sv.fiber_solver(logger=lu.Logger(logfile).plus_prefix("+: "),
                               **fiber_kwargs)
    X1 = np.concatenate(solution["Fiber trace"].points, axis=1)
    V1 = solution["Fixed points"]
    z = solution["Fiber trace"].z_initial
    # print("Status: %s\n"%solution["Fiber trace"].status)

    # Run in other direction (negate initial tangent)
    solution = sv.fiber_solver(z=-z,
                               logger=lu.Logger(logfile).plus_prefix("-: "),
                               **fiber_kwargs)
    X2 = np.concatenate(solution["Fiber trace"].points, axis=1)
    V2 = solution["Fixed points"]
    # print("Status: %s\n"%solution["Fiber trace"].status)

    # Join fiber segments
    fiber = np.concatenate((np.fliplr(X1), X2), axis=1)

    # Union solutions
    fxpts = fx.sanitize_points(
        np.concatenate((V1, V2), axis=1),
        f=lv.f,
        ef=lv.ef,
        Df=lv.Df,
        duplicates=lambda V, v: (np.fabs(V - v) < 10**-6).all(axis=0),
    )

    # Save results
    with open("%s_s%d.npz" % (basename, sample), 'w') as rf:
        np.savez(
            rf, **{
                "fxpts": fxpts,
                "fiber": fiber,
                "runtime": time.clock() - solve_start
            })

    logfile.close()
Beispiel #5
0
    v = np.zeros((N, 1))
    # c = np.random.randn(N,1)
    c = V.dot(np.random.randn(P, 1))  # span of training data
    logfile = sys.stdout
    fiber_kwargs = {
        "f": f,
        "ef": ef,
        "Df": Df,
        "compute_step_amount": compute_step_amount,
        "v": v,
        "c": c,
        "terminate": lambda trace: (np.fabs(trace.x[:N, :]) > 3).any(),
        "max_traverse_steps": 2000,
        "max_solve_iterations": 2**5,
        "max_history": 1000,
        "logger": lu.Logger(logfile)
    }

    # Run in one direction
    solution = sv.fiber_solver(**fiber_kwargs)
    V1 = np.concatenate(solution["Fiber trace"].points, axis=1)[:N, :]
    V1_fx = solution["Fixed points"]
    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)[:N, :]
    V2_fx = solution["Fixed points"]

    # Join fiber segments and fixed points
Beispiel #6
0
def run_trial(args):
    basename, N, sample, W, V, timeout = args

    logfile = open(trialname(basename,N,sample)+".log", "w")
    logger = lu.Logger(logfile)

    # Sample network
    f = rnn.f_factory(W)
    Df = rnn.Df_factory(W)
    ef = rnn.ef_factory(W)

    # Run fiber solver
    solve_logger = logger.plus_prefix("(%d,%d): "%(N,sample))
    stop_time = time.clock() + timeout
    fxpts, solution = rnn.run_fiber_solver(W,
        # max_traverse_steps = 2**15,
        stop_time = stop_time,
        logger=solve_logger,
        abs_alpha_min = True,
        within_fiber = True)
    status = solution["Fiber trace"].status

    # Get pre-refinement sign change candidates
    sign_changes = solution["Sign changes"]
    refinements = solution["Refinements"]
    candidates = np.concatenate( # first refinement fiber point
        [refinements[sc].points[0][:-1,:] for sc in sign_changes],
        axis = 1)

    # Post-process sign change candidates
    logger.log("(%d,%d): Sanitizing %d sc points\n"%(
        N,sample, 2*candidates.shape[1] + 1))
    sign_change_fxpts = fx.sanitize_points(
        np.concatenate(
            (-candidates, np.zeros((N,1)), candidates),
            axis=1),
        f, ef, Df,
        duplicates = rnn.duplicates_factory(W),
    )

    # Count union
    logger.log("(%d,%d): Sanitizing %d+%d=%d union points\n"%(
        N,sample, fxpts.shape[1], sign_change_fxpts.shape[1],
        fxpts.shape[1] + sign_change_fxpts.shape[1]))
    union_fxpts = fx.sanitize_points(
        np.concatenate(
            (fxpts, sign_change_fxpts),
            axis=1),
        f, ef, Df,
        duplicates = rnn.duplicates_factory(W),
    )

    logfile.close()

    print("(%d,%d) fxpt counts: new %d, old %d, union %d (status=%s)"%(
        N,sample,fxpts.shape[1], sign_change_fxpts.shape[1],
        union_fxpts.shape[1], status))
        
    results = {
        "status": status,
        "new count": fxpts.shape[1],
        "old count":  sign_change_fxpts.shape[1],
        "union": union_fxpts.shape[1],
        }

    with open(trialname(basename,N,sample)+".pkl",'w') as rf:
        pk.dump(results, rf)
def get_solver_kwargs(data,
                      c=None,
                      step_size=None,
                      ef=None,
                      history=1000,
                      silent=True):
    W = np.matmul(data, data.T)
    for i in range(W.shape[0]):
        W[i, i] = 0
    W *= (1.0 / W.shape[0])

    logfile = sys.stdout

    fiber_kwargs = {
        "f":
        lambda v: _f(W, v),
        "ef":
        ef_factory(W) if ef is None else lambda v: ef * np.ones(
            (W.shape[0], 1)),  # 1e-6 good default for ef
        "Df":
        lambda v: _Df(W, v),
        "compute_step_amount":
        compute_step_amount_factory(W) if step_size is None else lambda trace:
        (step_size, 0, 0),
        "v":
        None,  # Just use default - origin
        "c":
        c,
        "N":
        data.shape[0],
        "terminate":
        None,  # lambda trace: (np.fabs(trace.x[:N,:]) > 3).any(),
        "max_traverse_steps":
        2e5 if step_size is None else int(
            200 / step_size
        ),  # step size computed is normally in 1e-4 range and 200/1e-4 = 2e6. 2e5 is a compromise
        "max_solve_iterations":
        2**5,
        "max_history":
        history,
        "logger":
        lu.Logger(logfile) if not silent else None
    }

    # Old kwargs:
    #     fiber_kwargs = {
    #         "f": lambda v: _f(W,v),
    #         "ef": lambda v: 1e-6*np.ones((W.shape[0],1)),
    #         "Df": lambda v: _Df(W,v),
    #         "compute_step_amount": lambda trace: (step_size, 0),
    #         "v": None, # Just use default - origin
    #         "c": None, # Just use default - random
    #         "N": data.shape[0],
    #         "terminate": None,
    #         #"max_step_size": 1,
    #         "max_traverse_steps": int(200/step_size),
    #         "max_solve_iterations": (2**5),
    #         "max_history": 1000,
    #         #"solve_tolerance": 10**-10,
    #         "logger": lu.Logger(logfile) if not silent else None
    #     }
    #

    return fiber_kwargs