Exemplo n.º 1
0
def test_hypersphere(dimensions):
    n = 100 * dimensions
    if dimensions < 1:
        with pytest.raises(ValueError):
            dist = dists.UniformHypersphere().sample(1, dimensions)
    else:
        dist = dists.UniformHypersphere()
        samples = dist.sample(n, dimensions, np.random.RandomState(1))
        assert samples.shape == (n, dimensions)
        assert np.allclose(
            np.mean(samples, axis=0), np.zeros(dimensions), atol=0.1)
        hist, _ = np.histogramdd(samples, bins=5)
        assert np.allclose(hist - np.mean(hist), 0, atol=0.1 * n)
Exemplo n.º 2
0
def pick_eval_points(ens, n_points, rng):
    if n_points is None:
        # use a heuristic to pick the number of points
        dims, neurons = ens.dimensions, ens.neurons.n_neurons
        n_points = max(np.clip(500 * dims, 750, 2500), 2 * neurons)
    return dists.UniformHypersphere(ens.dimensions).sample(
        n_points, rng=rng) * ens.radius
Exemplo n.º 3
0
def test_hypersphere_surface(dimensions):
    n = 100 * dimensions
    dist = dists.UniformHypersphere(surface=True)
    samples = dist.sample(n, dimensions, np.random.RandomState(1))
    assert samples.shape == (n, dimensions)
    assert np.allclose(npext.norm(samples, axis=1), 1)
    assert np.allclose(
        np.mean(samples, axis=0), np.zeros(dimensions), atol=0.1)
Exemplo n.º 4
0
def build_ensemble(ens, model):  # noqa: C901
    # Create random number generator
    seed = model.next_seed() if ens.seed is None else ens.seed
    rng = np.random.RandomState(seed)

    # Generate eval points
    if ens.eval_points is None or is_integer(ens.eval_points):
        eval_points = pick_eval_points(ens=ens,
                                       n_points=ens.eval_points,
                                       rng=rng)
    else:
        eval_points = npext.array(ens.eval_points,
                                  dtype=np.float64,
                                  min_dims=2)

    # Set up signal
    model.sig_in[ens] = Signal(np.zeros(ens.dimensions),
                               name="%s.signal" % ens.label)
    model.operators.append(Reset(model.sig_in[ens]))

    # Set up encoders
    if ens.encoders is None:
        if isinstance(ens.neurons, nengo.Direct):
            encoders = np.identity(ens.dimensions)
        else:
            sphere = dists.UniformHypersphere(ens.dimensions, surface=True)
            encoders = sphere.sample(ens.neurons.n_neurons, rng=rng)
    else:
        encoders = np.array(ens.encoders, dtype=np.float64)
        enc_shape = (ens.neurons.n_neurons, ens.dimensions)
        if encoders.shape != enc_shape:
            raise ShapeMismatch(
                "Encoder shape is %s. Should be (n_neurons, dimensions); "
                "in this case %s." % (encoders.shape, enc_shape))
        encoders /= npext.norm(encoders, axis=1, keepdims=True)

    # Determine max_rates and intercepts
    if isinstance(ens.max_rates, dists.Distribution):
        max_rates = ens.max_rates.sample(ens.neurons.n_neurons, rng=rng)
    else:
        max_rates = np.array(ens.max_rates)
    if isinstance(ens.intercepts, dists.Distribution):
        intercepts = ens.intercepts.sample(ens.neurons.n_neurons, rng=rng)
    else:
        intercepts = np.array(ens.intercepts)

    # Build the neurons
    if isinstance(ens.neurons, nengo.Direct):
        Builder.build(ens.neurons, ens.dimensions, model=model)
    else:
        Builder.build(ens.neurons, max_rates, intercepts, model=model)
    bn = model.params[ens.neurons]

    # Scale the encoders
    if isinstance(ens.neurons, nengo.Direct):
        scaled_encoders = encoders
    else:
        scaled_encoders = encoders * (bn.gain / ens.radius)[:, np.newaxis]

    # Create output signal, using built Neurons
    model.operators.append(
        DotInc(Signal(scaled_encoders, name="%s.scaled_encoders" % ens.label),
               model.sig_in[ens],
               model.sig_in[ens.neurons],
               tag="%s encoding" % ens.label))

    # Output is neural output
    model.sig_out[ens] = model.sig_out[ens.neurons]

    for probe in ens.probes["decoded_output"]:
        Builder.build(probe, dimensions=ens.dimensions, model=model)
    for probe in ens.probes["spikes"] + ens.probes["voltages"]:
        Builder.build(probe, dimensions=ens.neurons.n_neurons, model=model)

    model.params[ens] = BuiltEnsemble(eval_points=eval_points,
                                      encoders=encoders,
                                      intercepts=intercepts,
                                      max_rates=max_rates,
                                      scaled_encoders=scaled_encoders)