def test_choice(weights, rng): n = 1000 choices = [[1, 1], [1, -1], [-1, 1], [-1, -1]] N = len(choices) dist = dists.Choice(choices, weights=weights) # If d is passed, it has to match with pytest.raises(ValueError): dist.sample(n, d=4, rng=rng) sample = dist.sample(n, rng=rng) tsample, tchoices = list(map(tuple, sample)), list(map(tuple, choices)) # check that frequency of choices matches weights inds = [tchoices.index(s) for s in tsample] hist, bins = np.histogram(inds, bins=np.linspace(-0.5, N - 0.5, N + 1)) p_empirical = hist / float(hist.sum()) p = np.ones(N) / N if dist.p is None else dist.p sterr = 1. / np.sqrt(n) # expected maximum standard error assert np.allclose(p, p_empirical, atol=2 * sterr)
def ThresholdingEnsembles(threshold, intercept_width=0.15, radius=1.0): """Configuration preset for a thresholding ensemble. This preset adjust ensemble parameters for thresholding. The ensemble's neurons will only fire for values above threshold. One can either decode the represented value (if it is above the threshold) or decode a step function if binary classification is desired. This preset: - Sets intercepts to be between ``threshold`` and ``radius`` with an exponential distribution (shape parameter of ``intercept_width``). This clusters intercepts near the threshold for better approximation. - Sets encoders to 1. - Sets evaluation points to be uniformly distributed between ``threshold`` and ``radius``. - Sets the radius. Parameters ---------- threshold : float Point at which ensembles should start firing. intercept_width : float, optional Controls how widely distributed the intercepts are. Smaller values give more clustering at the threshold, larger values give a more uniform distribution. radius : float, optional Ensemble radius. Returns ------- `nengo.Config` Configuration with presets. """ config = Config(Ensemble) config[Ensemble].radius = radius config[Ensemble].intercepts = dists.Exponential(intercept_width, threshold, radius) config[Ensemble].encoders = dists.Choice([[1]]) config[Ensemble].eval_points = dists.Uniform(threshold / radius, 1) return config