def test_subsolvers_L2(rng, logger): pytest.importorskip('scipy', minversion='0.11') # version for lsmr ref_solver = cholesky solvers = [conjgrad, block_conjgrad, conjgrad_scipy, lsmr_scipy] A, B = get_system(m=2000, n=1000, d=10, rng=rng) sigma = 0.1 * A.max() with Timer() as t0: x0, _ = ref_solver(A, B, sigma) xs = np.zeros((len(solvers),) + x0.shape) for i, solver in enumerate(solvers): with Timer() as t: xs[i], info = solver(A, B, sigma) logger.info('solver: %s', solver.__name__) logger.info('duration: %0.3f', t.duration) logger.info('duration relative to reference solver: %0.2f', (t.duration / t0.duration)) logger.info('info: %s', info) for solver, x in zip(solvers, xs): assert np.allclose(x0, x, atol=1e-5, rtol=1e-3), ( "Solver %s" % solver.__name__)
def test_eval_points(Simulator, nl_nodirect, plt, seed, rng): n = 100 d = 5 filter = 0.08 eval_points = np.logspace(np.log10(300), np.log10(5000), 11) eval_points = np.round(eval_points).astype('int') max_points = eval_points.max() n_trials = 1 rmses = np.nan * np.zeros((len(eval_points), n_trials)) for j in range(n_trials): points = rng.normal(size=(max_points, d)) points *= (rng.uniform(size=max_points) / norm(points, axis=-1))[:, None] rng_j = np.random.RandomState(348 + j) seed = 903824 + j # generate random input in unit hypersphere x = rng_j.normal(size=d) x *= rng_j.uniform() / norm(x) for i, n_points in enumerate(eval_points): model = nengo.Network(seed=seed) with model: model.config[nengo.Ensemble].neuron_type = nl_nodirect() u = nengo.Node(output=x) a = nengo.Ensemble(n * d, dimensions=d, eval_points=points[:n_points]) nengo.Connection(u, a, synapse=0) up = nengo.Probe(u) ap = nengo.Probe(a) with Timer() as timer: sim = Simulator(model) sim.run(10 * filter) t = sim.trange() xt = nengo.synapses.filtfilt(sim.data[up], filter, dt=sim.dt) yt = nengo.synapses.filtfilt(sim.data[ap], filter, dt=sim.dt) t0 = 5 * filter t1 = 7 * filter tmask = (t > t0) & (t < t1) rmses[i, j] = rms(yt[tmask] - xt[tmask]) print("done %d (%d) in %0.3f s" % (n_points, j, timer.duration)) # subtract out mean for each model rmses_norm = rmses - rmses.mean(0, keepdims=True) mean = rmses_norm.mean(1) low = rmses_norm.min(1) high = rmses_norm.max(1) plt.semilogx(eval_points, mean, 'k-') plt.semilogx(eval_points, high, 'r-') plt.semilogx(eval_points, low, 'b-') plt.xlim([eval_points[0], eval_points[-1]]) plt.xticks(eval_points, eval_points)
def test_subsolvers_L1(rng): A, B = get_system(m=2000, n=1000, d=10, rng=rng) l1 = 1e-4 with Timer() as t: LstsqL1(l1=l1, l2=0)(A, B, rng=rng) print(t.duration)
def test_large(Simulator, seed, logger): """Test with a lot of big probes. Can also be used for speed.""" n = 10 def input_fn(t): return list(range(1, 10)) model = nengo.Network(label='test_large_probes', seed=seed) with model: probes = [] for i in range(n): xi = nengo.Node(label='x%d' % i, output=input_fn) probes.append(nengo.Probe(xi, 'output')) sim = Simulator(model) simtime = 2.483 with Timer() as timer: sim.run(simtime) logger.info("Ran %d probes for %f sec simtime in %0.3f sec", n, simtime, timer.duration) t = sim.trange() x = np.asarray([input_fn(ti) for ti in t]) for p in probes: y = sim.data[p] assert np.allclose(y[1:], x[:-1]) # 1-step delay
def test_dts(Simulator): """Test probes with different sampling times.""" n = 10 rng = np.random.RandomState(48392) dts = 0.001 * rng.randint(low=1, high=100, size=n) # dts = 0.001 * np.hstack([2, rng.randint(low=1, high=100, size=n-1)]) def input_fn(t): return list(range(1, 10)) model = nengo.Network(label='test_probe_dts', seed=2891) with model: probes = [] for i, dt in enumerate(dts): xi = nengo.Node(label='x%d' % i, output=input_fn) p = nengo.Probe(xi, 'output', sample_every=dt) probes.append(p) sim = Simulator(model) simtime = 2.483 # simtime = 2.484 with Timer() as timer: sim.run(simtime) logger.debug("Ran %d probes for %f sec simtime in %0.3f sec", n, simtime, timer.duration) for i, p in enumerate(probes): t = sim.dt * np.arange(int(np.ceil(simtime / dts[i]))) x = np.asarray([input_fn(tt) for tt in t]) y = sim.data[p] assert len(x) == len(y) assert np.allclose(y[1:], x[:-1]) # 1-step delay
def test_cache_performance(tmpdir, Simulator): cache_dir = str(tmpdir) model = nengo.Network(seed=1) with model: nengo.Connection(nengo.Ensemble(1500, 10), nengo.Ensemble(1500, 10)) built_model = nengo.builder.Model(dt=0.001, seed=model.seed, decoder_cache=DecoderCache(cache_dir)) with Timer() as t_no_cache: nengo.Simulator(model, caching=False) with Timer() as t_cache_miss: nengo.Simulator(model, model=built_model, caching=True) with Timer() as t_cache_hit: nengo.Simulator(model, model=built_model, caching=True) assert calc_relative_timer_diff(t_no_cache, t_cache_miss) < 0.1 assert calc_relative_timer_diff(t_cache_hit, t_no_cache) > 0.75
def test_base_solvers_L1(): rng = np.random.RandomState(39408) A, B = get_system(m=500, n=100, d=1, rng=rng) l1 = 1e-4 with Timer() as t: lstsq_L1(A, B, l1=l1, l2=0) print(t.duration)
def test_subsolvers_L1(): rng = np.random.RandomState(39408) A, B = get_system(m=2000, n=1000, d=10, rng=rng) l1 = 1e-4 with Timer() as t: LstsqL1(l1=l1, l2=0)(A, B, rng=rng) print(t.duration)
def test_subsolvers_L1(rng, logger): pytest.importorskip('sklearn') A, B = get_system(m=2000, n=1000, d=10, rng=rng) l1 = 1e-4 with Timer() as t: LstsqL1(l1=l1, l2=0)(A, B, rng=rng) logger.info('duration: %0.3f', t.duration)
def test_subsolvers_L1(rng): pytest.importorskip('sklearn') A, B = get_system(m=2000, n=1000, d=10, rng=rng) l1 = 1e-4 with Timer() as t: LstsqL1(l1=l1, l2=0)(A, B, rng=rng) print(t.duration)
def test_subsolvers_L2(rng): ref_solver = cholesky solvers = [conjgrad, block_conjgrad, conjgrad_scipy, lsmr_scipy] A, B = get_system(m=2000, n=1000, d=10, rng=rng) sigma = 0.1 * A.max() with Timer() as t0: x0, _ = ref_solver(A, B, sigma) xs = np.zeros((len(solvers), ) + x0.shape) print() for i, solver in enumerate(solvers): with Timer() as t: xs[i], info = solver(A, B, sigma) print("%s: %0.3f (%0.2f) %s" % (solver.__name__, t.duration, t.duration / t0.duration, info)) for solver, x in zip(solvers, xs): assert np.allclose(x0, x, atol=1e-5, rtol=1e-3), ("Solver %s" % solver.__name__)
def test_base_solvers_L2(): ref_solver = _cholesky solvers = [_conjgrad, _block_conjgrad, _conjgrad_scipy, _lsmr_scipy] rng = np.random.RandomState(39408) A, B = get_system(m=5000, n=3000, d=3, rng=rng) sigma = 0.1 * A.max() with Timer() as t0: x0, _ = ref_solver(A, B, sigma) xs = np.zeros((len(solvers),) + x0.shape) print() for i, solver in enumerate(solvers): with Timer() as t: xs[i], info = solver(A, B, sigma) print("%s: %0.3f (%0.2f) %s" % ( solver.__name__, t.duration, t.duration / t0.duration, info)) for solver, x in zip(solvers, xs): assert np.allclose(x0, x, atol=1e-5, rtol=1e-3), ( "Solver %s" % solver.__name__)
def test_cache_performance(tmpdir, Simulator, seed): cache_dir = str(tmpdir) model = nengo.Network(seed=seed) with model: nengo.Connection(nengo.Ensemble(2000, 10), nengo.Ensemble(2000, 10)) with Timer() as t_no_cache: Simulator(model, model=nengo.builder.Model(dt=0.001, decoder_cache=NoDecoderCache())) with Timer() as t_cache_miss: Simulator(model, model=nengo.builder.Model( dt=0.001, decoder_cache=DecoderCache(cache_dir=cache_dir))) with Timer() as t_cache_hit: Simulator(model, model=nengo.builder.Model( dt=0.001, decoder_cache=DecoderCache(cache_dir=cache_dir))) assert calc_relative_timer_diff(t_no_cache, t_cache_miss) < 0.1 assert calc_relative_timer_diff(t_cache_hit, t_no_cache) > 0.4
def test_timer(): with Timer() as timer: 2 + 2 assert timer.duration > 0.0 assert timer.duration < 1.0 # Pretty bad worst case
def test_timer(): with Timer() as timer: for i in range(1000): 2 + 2 assert timer.duration > 0.0 assert timer.duration < 1.0 # Pretty bad worst case