コード例 #1
0
ファイル: processes.py プロジェクト: MarcoSaku/Spiking-C3D
        def step_conv3(t, x):
            x = x.reshape(shape_in)
            nk, ni, nj = shape_in[-3:]
            f = filters.shape[0]
            sk, si, sj = filters.shape[-3:]
            si2 = (si - 1) / 2
            sj2 = (sj - 1) / 2
            sk2 = (sk - 1) / 2

            y = np.zeros(shape_out)
            for k in range(nk):
                for i in range(ni):
                    for j in range(nj):
                        i0, i1 = i - si2, i + si2 + 1
                        j0, j1 = j - sj2, j + sj2 + 1
                        k0, k1 = k - sk2, k + sk2 + 1
                        sli = slice(max(-i0, 0), min(ni + si - i1, si))
                        slj = slice(max(-j0, 0), min(nj + sj - j1, sj))
                        slk = slice(max(-k0, 0), min(nk + sk - k1, sk))
                        w = (filters[:, i, j, :, sli, slj] if local_filters else
                             filters[:, :,slk, sli, slj])
                        xkij = x[:,max(k0, 0):min(k1, nk), max(i0, 0):min(i1, ni), max(j0, 0):min(j1, nj)]
                        y[:,k, i, j] = np.dot(xkij.ravel(), w.reshape(f, -1).T)

            if biases is not None:
                y += biases

            return y.ravel()
コード例 #2
0
ファイル: analysis.py プロジェクト: tbekolay/phd
 def cooccurr_ix(l, th=0.001):
     ix = []
     for i in range(len(l)):
         for j in range(i+1, len(l)):
             if abs(l[i] - l[j]) < th:
                 ix.append((i, j))
     return ix
コード例 #3
0
ファイル: analysis.py プロジェクト: tbekolay/phd
def get_syllables(n_syllables, minfreq, maxfreq, rng=np.random):
    allpaths = []
    for gdir in ['ges-de-ccv', 'ges-de-cv', 'ges-de-cvc', 'ges-de-v']:
        allpaths.extend([ges_path(gdir, gfile)
                         for gfile in os.listdir(ges_path(gdir))])
    indices = rng.permutation(len(allpaths))
    return ([allpaths[indices[i]] for i in range(n_syllables)],
            [rng.uniform(minfreq, maxfreq) for i in range(n_syllables)])
コード例 #4
0
ファイル: vision.py プロジェクト: fmirus/nengo_extras
def cd_encoders_biases(n_encoders, trainX, trainY, rng=np.random, mask=None,
                       norm_min=0.05, norm_tries=10):
    """Constrained difference (CD) method for encoders from data [1]_.

    Parameters
    ==========
    n_encoders : int
        Number of encoders to generate.
    trainX : (n_samples, n_dimensions) array-like
        Training features.
    trainY : (n_samples,) array-like
        Training labels.

    Returns
    =======
    encoders : (n_encoders, n_dimensions) array
        Generated encoders.
    biases : (n_encoders,) array
        Generated biases. These are biases assuming `f = G[E * X + b]`,
        and are therefore more like Nengo's `intercepts`.

    References
    ==========
    .. [1] McDonnell, M. D., Tissera, M. D., Vladusich, T., Van Schaik, A.,
       Tapson, J., & Schwenker, F. (2015). Fast, simple and accurate
       handwritten digit classification by training shallow neural network
       classifiers with the "Extreme learning machine" algorithm. PLoS ONE,
       10(8), 1-20. doi:10.1371/journal.pone.0134254
    """
    assert trainX.shape[0] == trainY.size
    trainX = trainX.reshape(trainX.shape[0], -1)
    trainY = trainY.ravel()
    d = trainX.shape[1]
    classes = np.unique(trainY)
    assert mask is None or mask.shape == (n_encoders, d)

    inds = [(trainY == label).nonzero()[0] for label in classes]
    train_norm = npext.norm(trainX, axis=1).mean()

    encoders = np.zeros((n_encoders, d))
    biases = np.zeros(n_encoders)
    for k in range(n_encoders):
        for _ in range(norm_tries):
            i, j = rng.choice(len(classes), size=2, replace=False)
            a, b = trainX[rng.choice(inds[i])], trainX[rng.choice(inds[j])]
            dab = a - b
            if mask is not None:
                dab *= mask[k]
            ndab = npext.norm(dab)**2
            if ndab >= norm_min * train_norm:
                break
        else:
            raise ValueError("Cannot find valid encoder")

        encoders[k] = (2. / ndab) * dab
        biases[k] = np.dot(a + b, dab) / ndab

    return encoders, biases
コード例 #5
0
def test_lif_step(upsample, n_elements):
    """Test the lif nonlinearity, comparing one step with the Numpy version."""
    rng = np.random

    dt = 1e-3
    n_neurons = [12345, 23456, 34567]
    J = RA([rng.normal(scale=1.2, size=n) for n in n_neurons])
    V = RA([rng.uniform(low=0, high=1, size=n) for n in n_neurons])
    W = RA([rng.uniform(low=-5 * dt, high=5 * dt, size=n) for n in n_neurons])
    OS = RA([np.zeros(n) for n in n_neurons])

    ref = 2e-3
    taus = list(rng.uniform(low=15e-3, high=80e-3, size=len(n_neurons)))

    queue = cl.CommandQueue(ctx)
    clJ = CLRA(queue, J)
    clV = CLRA(queue, V)
    clW = CLRA(queue, W)
    clOS = CLRA(queue, OS)
    clTau = CLRA(queue, RA(taus))

    # simulate host
    nls = [LIF(tau_ref=ref, tau_rc=taus[i])
           for i, n in enumerate(n_neurons)]
    for i, nl in enumerate(nls):
        if upsample <= 1:
            nl.step_math(dt, J[i], OS[i], V[i], W[i])
        else:
            s = np.zeros_like(OS[i])
            for j in range(upsample):
                nl.step_math(dt / upsample, J[i], s, V[i], W[i])
                OS[i] = (1./dt) * ((OS[i] > 0) | (s > 0))

    # simulate device
    plan = plan_lif(queue, clJ, clV, clW, clV, clW, clOS, ref, clTau, dt,
                    n_elements=n_elements, upsample=upsample)
    plan()

    if 1:
        a, b = V, clV
        for i in range(len(a)):
            nc, _ = not_close(a[i], b[i]).nonzero()
            if len(nc) > 0:
                j = nc[0]
                print("i", i, "j", j)
                print("J", J[i][j], clJ[i][j])
                print("V", V[i][j], clV[i][j])
                print("W", W[i][j], clW[i][j])
                print("...", len(nc) - 1, "more")

    n_spikes = np.sum([np.sum(os) for os in OS])
    if n_spikes < 1.0:
        logger.warn("LIF spiking mechanism was not tested!")
    assert ra.allclose(J, clJ.to_host())
    assert ra.allclose(V, clV.to_host())
    assert ra.allclose(W, clW.to_host())
    assert ra.allclose(OS, clOS.to_host())
コード例 #6
0
def test_one_short_segment_many_dots(planner):
    for ND in 2, 20, 100:
        check_from_shapes(
            planner,
            0.5, 0.6, 0.7,
            A_shapes=[(10, 1 + ii % 2) for ii in range(ND)],
            X_shapes=[(1 + ii % 2, 1) for ii in range(ND)],
            A_js=[range(ND)],
            X_js=[range(ND)])
コード例 #7
0
def test_one_short_segment_many_longer_dots(planner):
    for ND in 2, 20, 100:
        check_from_shapes(
            planner,
            0.5, 0.6, 0.7,
            A_shapes=[(2000, ii + 1) for ii in range(ND)],
            X_shapes=[(ii + 1, 1) for ii in range(ND)],
            A_js=[range(ND)],
            X_js=[range(ND)])
コード例 #8
0
ファイル: matplotlib.py プロジェクト: Ocode/nengo
def rasterplot(time, spikes, ax=None, **kwargs):
    """Generate a raster plot of the provided spike data

    Parameters
    ----------
    time : array
        Time data from the simulation
    spikes: array
        The spike data with columns for each neuron and 1s indicating spikes
    ax: matplotlib.axes.Axes
        The figure axes to plot into.

    Returns
    -------
    ax: matplotlib.axes.Axes
        The axes that were plotted into

    Examples
    --------
    >>> import nengo
    >>> model = nengo.Model("Raster")
    >>> A = nengo.Ensemble(nengo.LIF(20), dimensions=1)
    >>> A_spikes = nengo.Probe(A, "spikes")
    >>> sim = nengo.Simulator(model)
    >>> sim.run(1)
    >>> rasterplot(sim.trange(), sim.data[A_spikes])
    """

    if ax is None:
        ax = plt.gca()

    colors = kwargs.pop('colors', None)
    if colors is None:
        color_cycle = plt.rcParams['axes.color_cycle']
        colors = [color_cycle[ix % len(color_cycle)]
                  for ix in range(spikes.shape[1])]

    if hasattr(ax, 'eventplot'):
        spikes = [time[spikes[:, i] > 0].flatten()
                  for i in range(spikes.shape[1])]
        for ix in range(len(spikes)):
            if spikes[ix].shape == (0,):
                spikes[ix] = np.array([-1])
        ax.eventplot(spikes, colors=colors, **kwargs)
        ax.set_ylim(len(spikes) - 0.5, -0.5)
        if len(spikes) == 1:
            ax.set_ylim(0.4, 1.6)  # eventplot plots different for len==1
        ax.set_xlim(left=0, right=max(time))

    else:
        # Older Matplotlib, doesn't have eventplot
        for i in range(spikes.shape[1]):
            ax.plot(time[spikes[:, i] > 0],
                    np.ones_like(np.where(spikes[:, i] > 0)).T + i, ',',
                    color=colors[i], **kwargs)

    return ax
コード例 #9
0
ファイル: pointer.py プロジェクト: 4n6strider/nengo
    def get_convolution_matrix(self):
        """Return the matrix that does a circular convolution by this vector.

        This should be such that ``A*B == dot(A.get_convolution_matrix, B.v)``.
        """
        D = len(self.v)
        T = []
        for i in range(D):
            T.append([self.v[(i - j) % D] for j in range(D)])
        return np.array(T)
コード例 #10
0
def check_from_shapes(
    planner,
    alpha, beta, gamma,
    A_shapes, X_shapes,
    A_js,
    X_js,
):
    rng = np.random.RandomState(1234)
    A = RA([0.1 + rng.rand(*shp) for shp in A_shapes])
    X = RA([0.1 + rng.rand(*shp) for shp in X_shapes])
    Y = RA([0.1 + rng.rand(
        A_shapes[A_js[ii][0]][0],
        X_shapes[X_js[ii][0]][1])
        for ii in range(len(A_js))])
    A_js = RA(A_js)
    X_js = RA(X_js)
    # -- prepare initial conditions on device
    queue = cl.CommandQueue(ctx)
    clA = CLRA(queue, A)
    clX = CLRA(queue, X)
    clY = CLRA(queue, Y)
    clA_js = CLRA(queue, A_js)
    clX_js = CLRA(queue, X_js)
    assert allclose(A, clA)
    assert allclose(X, clX)
    assert allclose(Y, clY)
    assert allclose(A_js, clA_js)
    assert allclose(X_js, clX_js)

    # -- run cl computation
    prog = planner(
        queue, alpha, clA, clA_js, clX, clX_js, beta, clY, gamma=gamma)

    plans = prog.plans
    assert len(plans) == 1
    plans[0]()

    # -- ensure they match
    for i in range(len(A_js)):
        ref = gamma + beta * Y[i] + alpha * sum(
            [np.dot(A[aj], X[xj])
             for aj, xj in zip(A_js[i].ravel(), X_js[i].ravel())])
        sim = clY[i]
        if not np.allclose(ref, sim, atol=1e-3, rtol=1e-3):
            print('A_shapes',  A_shapes)
            print('X_shapes', X_shapes)
            if len(ref) > 20:
                print('ref', ref[:10], '...', ref[-10:])
                print('sim', sim[:10], '...', sim[-10:])
            else:
                print('ref', ref)
                print('sim', sim)
            assert 0
コード例 #11
0
def _mmul_transforms(A_shape, B_shape, C_dim):
    transformA = np.zeros((C_dim, A_shape[0] * A_shape[1]))
    transformB = np.zeros((C_dim, B_shape[0] * B_shape[1]))

    for i in range(A_shape[0]):
        for j in range(A_shape[1]):
            for k in range(B_shape[1]):
                tmp = (j + k * A_shape[1] + i * B_shape[0] * B_shape[1])
                transformA[tmp * 2][j + i * A_shape[1]] = 1
                transformB[tmp * 2 + 1][k + j * B_shape[1]] = 1

    return transformA, transformB
コード例 #12
0
def test_lif_speed(rng, heterogeneous):
    """Test the speed of the lif nonlinearity

    heterogeneous: if true, use a wide range of population sizes.
    """
    dt = 1e-3
    ref = 2e-3
    tau = 20e-3

    n_iters = 1000
    if heterogeneous:
        n_neurons = [1.0e5] * 50 + [1e3] * 500
    else:
        n_neurons = [1.1e5] * 50
    n_neurons = list(map(int, n_neurons))

    J = RA([rng.randn(n) for n in n_neurons], dtype=np.float32)
    V = RA([rng.uniform(low=0, high=1, size=n) for n in n_neurons],
           dtype=np.float32)
    W = RA([rng.uniform(low=-10 * dt, high=10 * dt, size=n)
            for n in n_neurons], dtype=np.float32)
    OS = RA([np.zeros(n) for n in n_neurons], dtype=np.float32)

    queue = cl.CommandQueue(
        ctx, properties=cl.command_queue_properties.PROFILING_ENABLE)

    clJ = CLRA(queue, J)
    clV = CLRA(queue, V)
    clW = CLRA(queue, W)
    clOS = CLRA(queue, OS)

    for i, blockify in enumerate([False, True]):
        plan = plan_lif(queue, dt, clJ, clV, clW, clOS, ref, tau,
                        blockify=blockify)

        with Timer() as timer:
            for j in range(n_iters):
                plan()

        print("plan %d: blockify = %s, dur = %0.3f"
              % (i, blockify, timer.duration))
    print "Original LIF impl"
    for i, blockify in enumerate([False, True]):
        plan = plan_lif_old(queue, dt, clJ, clV, clW, clOS, ref, tau,
                        blockify=blockify)

        with Timer() as timer:
            for j in range(n_iters):
                plan()

        print("plan %d: blockify = %s, dur = %0.3f"
              % (i, blockify, timer.duration))
コード例 #13
0
ファイル: solvers.py プロジェクト: xiaocuilin/Spiking-C3D
def conjgrad_scipy(A, Y, sigma, tol=1e-4):
    """Solve the least-squares system using Scipy's conjugate gradient."""
    import scipy.sparse.linalg
    Y, m, n, d, matrix_in = _format_system(A, Y)

    damp = m * sigma**2
    calcAA = lambda x: np.dot(A.T, np.dot(A, x)) + damp * x
    G = scipy.sparse.linalg.LinearOperator((n, n),
                                           matvec=calcAA,
                                           matmat=calcAA,
                                           dtype=A.dtype)
    B = np.dot(A.T, Y)

    X = np.zeros((n, d), dtype=B.dtype)
    infos = np.zeros(d, dtype='int')
    itns = np.zeros(d, dtype='int')
    for i in range(d):

        def callback(x):
            itns[i] += 1  # use the callback to count the number of iterations

        X[:, i], infos[i] = scipy.sparse.linalg.cg(G,
                                                   B[:, i],
                                                   tol=tol,
                                                   callback=callback)

    info = {'rmses': _rmses(A, X, Y), 'iterations': itns, 'info': infos}
    return X if matrix_in else X.flatten(), info
コード例 #14
0
ファイル: solvers.py プロジェクト: xiaocuilin/Spiking-C3D
def _conjgrad_iters(calcAx, b, x, maxiters=None, rtol=1e-6):
    """Solve the single-RHS linear system using conjugate gradient."""

    if maxiters is None:
        maxiters = b.shape[0]

    r = b - calcAx(x)
    p = r.copy()
    rsold = np.dot(r, r)

    for i in range(maxiters):
        Ap = calcAx(p)
        alpha = rsold / np.dot(p, Ap)
        x += alpha * p
        r -= alpha * Ap

        rsnew = np.dot(r, r)
        beta = rsnew / rsold

        if np.sqrt(rsnew) < rtol:
            break

        if beta < 1e-12:  # no perceptible change in p
            break

        # p = r + beta*p
        p *= beta
        p += r
        rsold = rsnew

    return x, i + 1
コード例 #15
0
def transform_in(dims, align, invert):
    """Create a transform to map the input into the Fourier domain.

    See CircularConvolution docstring for more details.

    Parameters
    ----------
    dims : int
        Input dimensions.
    align : 'A' or 'B'
        How to align the real and imaginary components; the alignment
        depends on whether we're doing transformA or transformB.
    invert : bool
        Whether to reverse the order of elements.
    """
    if align not in ('A', 'B'):
        raise ValueError("'align' must be either 'A' or 'B'")

    dims2 = 4 * (dims // 2 + 1)
    tr = np.zeros((dims2, dims))
    dft = dft_half(dims)

    for i in range(dims2):
        row = dft[i // 4] if not invert else dft[i // 4].conj()
        if align == 'A':
            tr[i] = row.real if i % 2 == 0 else row.imag
        else:  # align == 'B'
            tr[i] = row.real if i % 4 == 0 or i % 4 == 3 else row.imag

    remove_imag_rows(tr)
    return tr.reshape((-1, dims))
コード例 #16
0
ファイル: base.py プロジェクト: FishShrimp/nengo
    def run_steps(self, n_steps, d=None, dt=None, rng=np.random, **kwargs):
        """Run process without input for given number of steps.

        Keyword arguments that do not appear in the parameter list below
        will be passed to the ``make_step`` function of this process.

        Parameters
        ----------
        n_steps : int
            The number of steps to run.
        d : int, optional (Default: None)
            Output dimensionality. If None, ``default_size_out`` will be used.
        dt : float, optional (Default: None)
            Simulation timestep. If None, ``default_dt`` will be used.
        rng : `numpy.random.RandomState` (Default: ``numpy.random``)
            Random number generator used for stochstic processes.
        """
        shape_in = as_shape(0)
        shape_out = as_shape(self.default_size_out if d is None else d)
        dt = self.default_dt if dt is None else dt
        rng = self.get_rng(rng)
        step = self.make_step(shape_in, shape_out, dt, rng, **kwargs)
        output = np.zeros((n_steps, ) + shape_out)
        for i in range(n_steps):
            output[i] = step((i + 1) * dt)
        return output
コード例 #17
0
ファイル: test_product.py プロジェクト: Ocode/nengo
def test_sine_waves(Simulator, nl, plt):
    radius = 2
    dim = 5
    product = nengo.networks.Product(
        200, dim, radius, neuron_type=nl(), seed=63)

    func_A = lambda t: np.sqrt(radius)*np.sin(np.arange(1, dim+1)*2*np.pi*t)
    func_B = lambda t: np.sqrt(radius)*np.sin(np.arange(dim, 0, -1)*2*np.pi*t)
    with product:
        input_A = nengo.Node(func_A)
        input_B = nengo.Node(func_B)
        nengo.Connection(input_A, product.A)
        nengo.Connection(input_B, product.B)
        p = nengo.Probe(product.output, synapse=0.005)

    sim = Simulator(product)
    sim.run(1.0)

    t = sim.trange()
    AB = np.asarray(list(map(func_A, t))) * np.asarray(list(map(func_B, t)))
    delay = 0.013
    offset = np.where(t >= delay)[0]

    for i in range(dim):
        plt.subplot(dim+1, 1, i+1)
        plt.plot(t + delay, AB[:, i])
        plt.plot(t, sim.data[p][:, i])

    assert rmse(AB[:len(offset), :], sim.data[p][offset, :]) < 0.2
コード例 #18
0
def test_sine_waves(Simulator, nl):
    radius = 2
    dim = 5
    product = nengo.networks.Product(
        200, dim, radius, neuron_type=nl(), seed=63)

    func_A = lambda t: radius*np.sin(np.arange(1, dim+1)*2*np.pi*t)
    func_B = lambda t: radius*np.sin(np.arange(dim, 0, -1)*2*np.pi*t)
    pstc = 0.003
    with product:
        input_A = nengo.Node(func_A)
        input_B = nengo.Node(func_B)
        nengo.Connection(input_A, product.A)
        nengo.Connection(input_B, product.B)
        p = nengo.Probe(product.output, synapse=pstc)

    sim = Simulator(product)
    sim.run(1.0)

    t = sim.trange()
    AB = np.asarray(list(map(func_A, t))) * np.asarray(list(map(func_B, t)))
    delay = 0.011
    offset = np.where(t > delay)[0]

    with Plotter(Simulator) as plt:
        for i in range(dim):
            plt.subplot(dim+1, 1, i+1)
            plt.plot(t + delay, AB[:, i], label="$A \cdot B$")
            plt.plot(t, sim.data[p][:, i], label="Output")
            plt.legend()
        plt.savefig('test_product.test_sine_waves.pdf')
        plt.close()

    assert rmse(AB[:len(offset), :], sim.data[p][offset, :]) < 0.3
コード例 #19
0
    def reset(self, seed=None):
        if self.closed:
            raise SimulatorClosed("Cannot reset closed Simulator.")

        if seed is not None:
            raise NotImplementedError("Seed changing not implemented")

        # reset signals
        for base in self.all_bases:
            # TODO: copy all data on at once
            if not base.readonly:
                self.all_data[self.sidx[base]] = base.initial_value

        for clra, ra in iteritems(self._raggedarrays_to_reset):
            # TODO: copy all data on at once
            for i in range(len(clra)):
                clra[i] = ra[i]

        # clear probe data
        if self._cl_probe_plan is not None:
            self._cl_probe_plan.cl_bufpositions.fill(0)

            for probe in self.model.probes:
                del self._probe_outputs[probe][:]

        self._reset_rng()
        self._reset_cl_rngs()
        self._probe_step_time()
コード例 #20
0
ファイル: test_product.py プロジェクト: dunovank/nengo
def test_sine_waves(Simulator, plt, seed):
    radius = 2
    dim = 5
    product = nengo.networks.Product(
        200, dim, radius, net=nengo.Network(seed=seed))

    func_A = lambda t: np.sqrt(radius)*np.sin(np.arange(1, dim+1)*2*np.pi*t)
    func_B = lambda t: np.sqrt(radius)*np.sin(np.arange(dim, 0, -1)*2*np.pi*t)
    with product:
        input_A = nengo.Node(func_A)
        input_B = nengo.Node(func_B)
        nengo.Connection(input_A, product.A)
        nengo.Connection(input_B, product.B)
        p = nengo.Probe(product.output, synapse=0.005)

    sim = Simulator(product)
    sim.run(1.0)

    t = sim.trange()
    AB = np.asarray(list(map(func_A, t))) * np.asarray(list(map(func_B, t)))
    delay = 0.013
    offset = np.where(t >= delay)[0]

    for i in range(dim):
        plt.subplot(dim+1, 1, i+1)
        plt.plot(t + delay, AB[:, i])
        plt.plot(t, sim.data[p][:, i])
    plt.xlim(right=t[-1])

    assert rmse(AB[:len(offset), :], sim.data[p][offset, :]) < 0.2
コード例 #21
0
def test_sine_waves(Simulator, plt, seed):
    radius = 2
    dim = 5
    product = nengo.networks.Product(200, dim, radius, seed=seed)

    func_a = lambda t: np.sqrt(radius) * np.sin(
        np.arange(1, dim + 1) * 2 * np.pi * t)
    func_b = lambda t: np.sqrt(radius) * np.sin(
        np.arange(dim, 0, -1) * 2 * np.pi * t)
    with product:
        input_a = nengo.Node(func_a)
        input_b = nengo.Node(func_b)
        nengo.Connection(input_a, product.input_a)
        nengo.Connection(input_b, product.input_b)
        p = nengo.Probe(product.output, synapse=0.005)

    with Simulator(product) as sim:
        sim.run(1.0)

    t = sim.trange()
    ideal = np.asarray(list(map(func_a, t))) * np.asarray(list(map(func_b, t)))
    delay = 0.013
    offset = np.where(t >= delay)[0]

    for i in range(dim):
        plt.subplot(dim + 1, 1, i + 1)
        plt.plot(t + delay, ideal[:, i])
        plt.plot(t, sim.data[p][:, i])
        plt.xlim(right=t[-1])
        plt.yticks((-2, 0, 2))

    assert rmse(ideal[:len(offset), :], sim.data[p][offset, :]) < 0.2
コード例 #22
0
ファイル: base.py プロジェクト: CaiZhongda/nengo
    def run_steps(self, n_steps, d=None, dt=None, rng=np.random, **kwargs):
        """Run process without input for given number of steps.

        Keyword arguments that do not appear in the parameter list below
        will be passed to the ``make_step`` function of this process.

        Parameters
        ----------
        n_steps : int
            The number of steps to run.
        d : int, optional (Default: None)
            Output dimensionality. If None, ``default_size_out`` will be used.
        dt : float, optional (Default: None)
            Simulation timestep. If None, ``default_dt`` will be used.
        rng : `numpy.random.RandomState` (Default: ``numpy.random``)
            Random number generator used for stochstic processes.
        """
        shape_in = as_shape(0)
        shape_out = as_shape(self.default_size_out if d is None else d)
        dt = self.default_dt if dt is None else dt
        rng = self.get_rng(rng)
        step = self.make_step(shape_in, shape_out, dt, rng, **kwargs)
        output = np.zeros((n_steps,) + shape_out)
        for i in range(n_steps):
            output[i] = step((i+1) * dt)
        return output
コード例 #23
0
    def __call__(self, A, Y, rng=None, E=None):
        tstart = time.time()
        Y, m, n, _, matrix_in = format_system(A, Y)

        # solve for coefficients using standard solver
        X, info0 = self.solver1(A, Y, rng=rng)
        X = self.mul_encoders(X, E)

        # drop weights close to zero, based on `drop` ratio
        Xabs = np.sort(np.abs(X.flat))
        threshold = Xabs[int(np.round(self.drop * Xabs.size))]
        X[np.abs(X) < threshold] = 0

        # retrain nonzero weights
        Y = self.mul_encoders(Y, E)
        for i in range(X.shape[1]):
            nonzero = X[:, i] != 0
            if nonzero.sum() > 0:
                X[nonzero, i], info1 = self.solver2(
                    A[:, nonzero], Y[:, i], rng=rng)

        t = time.time() - tstart
        info = {'rmses': rmses(A, X, Y), 'info0': info0, 'info1': info1,
                'time': t}
        return X if matrix_in or X.shape[1] > 1 else X.ravel(), info
コード例 #24
0
def test_noise(RefSimulator, seed):
    """Make sure that we can generate noise properly."""

    n = 1000
    mean, std = 0.1, 0.8
    noise = Signal(np.zeros(n), name="noise")
    process = nengo.processes.StochasticProcess(nengo.dists.Gaussian(
        mean, std))

    m = Model(dt=0.001)
    m.operators += [Reset(noise), SimNoise(noise, process)]

    sim = RefSimulator(None, model=m, seed=seed)
    samples = np.zeros((100, n))
    for i in range(100):
        sim.step()
        samples[i] = sim.signals[noise]

    h, xedges = np.histogram(samples.flat, bins=51)
    x = 0.5 * (xedges[:-1] + xedges[1:])
    dx = np.diff(xedges)
    z = 1. / np.sqrt(2 * np.pi * std**2) * np.exp(-0.5 *
                                                  (x - mean)**2 / std**2)
    y = h / float(h.sum()) / dx
    assert np.allclose(y, z, atol=0.02)
コード例 #25
0
ファイル: test_probe.py プロジェクト: pblouw/blouw-etal-2016
def test_large(Simulator):
    """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=3249)
    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.debug("Ran %d probes for %f sec simtime in %0.3f sec",
                 n, simtime, timer.duration)

    t = sim.dt * np.arange(int(np.round(simtime / sim.dt)))
    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
コード例 #26
0
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)
コード例 #27
0
ファイル: test_builder.py プロジェクト: shaunren/nengo
def test_signal_slicing(rng):
    slices = [
        0, 1,
        slice(None, -1),
        slice(1, None),
        slice(1, -1),
        slice(None, None, 3),
        slice(1, -1, 2)
    ]

    x = np.arange(12, dtype=float)
    y = np.arange(24, dtype=float).reshape(4, 6)
    a = Signal(x.copy())
    b = Signal(y.copy())

    for i in range(100):
        si0, si1 = rng.randint(0, len(slices), size=2)
        s0, s1 = slices[si0], slices[si1]
        assert np.array_equiv(a[s0].initial_value, x[s0])
        assert np.array_equiv(b[s0, s1].initial_value, y[s0, s1])

    with pytest.raises(ValueError):
        a[[0, 2]]
    with pytest.raises(ValueError):
        b[[0, 1], [3, 4]]
コード例 #28
0
ファイル: test_pointer.py プロジェクト: dunovank/nengo
def test_init():
    a = SemanticPointer([1, 2, 3, 4])
    assert len(a) == 4

    a = SemanticPointer([1, 2, 3, 4, 5])
    assert len(a) == 5

    a = SemanticPointer(list(range(100)))
    assert len(a) == 100

    a = SemanticPointer(27)
    assert len(a) == 27
    assert np.allclose(a.length(), 1)

    with pytest.raises(Exception):
        a = SemanticPointer(np.zeros(2, 2))

    with pytest.raises(Exception):
        a = SemanticPointer(-1)
    with pytest.raises(Exception):
        a = SemanticPointer(0)
    with pytest.raises(Exception):
        a = SemanticPointer(1.7)
    with pytest.raises(Exception):
        a = SemanticPointer(None)
    with pytest.raises(Exception):
        a = SemanticPointer(int)
コード例 #29
0
ファイル: solvers.py プロジェクト: epaxon/nengo
def _conjgrad_iters(calcAx, b, x, maxiters=None, rtol=1e-6):
    """Solve the single-RHS linear system using conjugate gradient."""

    if maxiters is None:
        maxiters = b.shape[0]

    r = b - calcAx(x)
    p = r.copy()
    rsold = np.dot(r, r)

    for i in range(maxiters):
        Ap = calcAx(p)
        alpha = rsold / np.dot(p, Ap)
        x += alpha * p
        r -= alpha * Ap

        rsnew = np.dot(r, r)
        beta = rsnew / rsold

        if np.sqrt(rsnew) < rtol:
            break

        if beta < 1e-12:  # no perceptible change in p
            break

        # p = r + beta*p
        p *= beta
        p += r
        rsold = rsnew

    return x, i+1
コード例 #30
0
ファイル: test_product.py プロジェクト: CamZHU/nengo
def test_direct_mode_with_single_neuron(Simulator, plt, seed):
    radius = 2
    dim = 5

    config = nengo.Config(nengo.Ensemble)
    config[nengo.Ensemble].neuron_type = nengo.Direct()
    with config:
        product = nengo.networks.Product(
            1, dim, radius, net=nengo.Network(seed=seed))

    func_A = lambda t: np.sqrt(radius)*np.sin(np.arange(1, dim+1)*2*np.pi*t)
    func_B = lambda t: np.sqrt(radius)*np.sin(np.arange(dim, 0, -1)*2*np.pi*t)
    with product:
        input_A = nengo.Node(func_A)
        input_B = nengo.Node(func_B)
        nengo.Connection(input_A, product.A)
        nengo.Connection(input_B, product.B)
        p = nengo.Probe(product.output, synapse=0.005)

    sim = Simulator(product)
    sim.run(1.0)

    t = sim.trange()
    AB = np.asarray(list(map(func_A, t))) * np.asarray(list(map(func_B, t)))
    delay = 0.013
    offset = np.where(t >= delay)[0]

    for i in range(dim):
        plt.subplot(dim+1, 1, i+1)
        plt.plot(t + delay, AB[:, i])
        plt.plot(t, sim.data[p][:, i])
    plt.xlim(right=t[-1])

    assert rmse(AB[:len(offset), :], sim.data[p][offset, :]) < 0.2
コード例 #31
0
ファイル: solvers.py プロジェクト: epaxon/nengo
def conjgrad_scipy(A, Y, sigma, tol=1e-4):
    """Solve the least-squares system using Scipy's conjugate gradient."""
    import scipy.sparse.linalg
    Y, m, n, d, matrix_in = _format_system(A, Y)

    damp = m * sigma**2
    calcAA = lambda x: np.dot(A.T, np.dot(A, x)) + damp * x
    G = scipy.sparse.linalg.LinearOperator(
        (n, n), matvec=calcAA, matmat=calcAA, dtype=A.dtype)
    B = np.dot(A.T, Y)

    X = np.zeros((n, d), dtype=B.dtype)
    infos = np.zeros(d, dtype='int')
    itns = np.zeros(d, dtype='int')
    for i in range(d):
        def callback(x):
            itns[i] += 1  # use the callback to count the number of iterations

        X[:, i], infos[i] = scipy.sparse.linalg.cg(
            G, B[:, i], tol=tol, callback=callback)

    info = {'rmses': npext.rms(Y - np.dot(A, X), axis=0),
            'iterations': itns,
            'info': infos}
    return X if matrix_in else X.flatten(), info
コード例 #32
0
ファイル: solvers.py プロジェクト: pblouw/blouw-etal-2016
    def __call__(self, A, Y, rng=None, E=None):
        Y, m, n, d, matrix_in = _format_system(A, Y)

        # solve for coefficients using standard solver
        X, info0 = self.solver1(A, Y, rng=rng)
        X = self.mul_encoders(X, E)

        # drop weights close to zero, based on `drop` ratio
        Xabs = np.sort(np.abs(X.flat))
        threshold = Xabs[int(np.round(self.drop * Xabs.size))]
        X[np.abs(X) < threshold] = 0

        # retrain nonzero weights
        Y = self.mul_encoders(Y, E)
        for i in range(X.shape[1]):
            nonzero = X[:, i] != 0
            if nonzero.sum() > 0:
                X[nonzero, i], info1 = self.solver2(A[:, nonzero],
                                                    Y[:, i],
                                                    rng=rng)

        info = {
            'rmses': npext.rms(Y - np.dot(A, X), axis=0),
            'info0': info0,
            'info1': info1
        }
        return X if matrix_in else X.flatten(), info
コード例 #33
0
ファイル: solvers.py プロジェクト: CaiZhongda/nengo
    def __call__(self, A, Y, rng=None, E=None):
        tstart = time.time()
        Y, m, n, d, matrix_in = format_system(A, Y)

        # solve for coefficients using standard solver
        X, info0 = self.solver1(A, Y, rng=rng)
        X = self.mul_encoders(X, E)

        # drop weights close to zero, based on `drop` ratio
        Xabs = np.sort(np.abs(X.flat))
        threshold = Xabs[int(np.round(self.drop * Xabs.size))]
        X[np.abs(X) < threshold] = 0

        # retrain nonzero weights
        Y = self.mul_encoders(Y, E)
        for i in range(X.shape[1]):
            nonzero = X[:, i] != 0
            if nonzero.sum() > 0:
                X[nonzero, i], info1 = self.solver2(
                    A[:, nonzero], Y[:, i], rng=rng)

        t = time.time() - tstart
        info = {'rmses': rmses(A, X, Y), 'info0': info0, 'info1': info1,
                'time': t}
        return X if matrix_in else X.flatten(), info
コード例 #34
0
ファイル: filter_design.py プロジェクト: Ocode/nengo
def zpk2tf(z, p, k):
    """Return polynomial transfer function representation from zeros
    and poles

    Parameters
    ----------
    z : ndarray
        Zeros of the transfer function.
    p : ndarray
        Poles of the transfer function.
    k : float
        System gain.

    Returns
    -------
    b : ndarray
        Numerator polynomial.
    a : ndarray
        Denominator polynomial.

    """
    z = atleast_1d(z)
    k = atleast_1d(k)
    if len(z.shape) > 1:
        temp = poly(z[0])
        b = zeros((z.shape[0], z.shape[1] + 1), temp.dtype.char)
        if len(k) == 1:
            k = [k[0]] * z.shape[0]
        for i in range(z.shape[0]):
            b[i] = k[i] * poly(z[i])
    else:
        b = k * poly(z)
    a = atleast_1d(poly(p))
    return b, a
コード例 #35
0
def transform_in(dims, align, invert):
    """Create a transform to map the input into the Fourier domain.

    See CircularConvolution docstring for more details.

    Parameters
    ----------
    dims : int
        Input dimensions.
    align : 'A' or 'B'
        How to align the real and imaginary components; the alignment
        depends on whether we're doing transformA or transformB.
    invert : bool
        Whether to reverse the order of elements.
    """
    if align not in ('A', 'B'):
        raise ValidationError("'align' must be either 'A' or 'B'", 'align')

    dims2 = 4 * (dims // 2 + 1)
    tr = np.zeros((dims2, dims))
    dft = dft_half(dims)

    for i in range(dims2):
        row = dft[i // 4] if not invert else dft[i // 4].conj()
        if align == 'A':
            tr[i] = row.real if i % 2 == 0 else row.imag
        else:  # align == 'B'
            tr[i] = row.real if i % 4 == 0 or i % 4 == 3 else row.imag

    remove_imag_rows(tr)
    return tr.reshape((-1, dims))
コード例 #36
0
ファイル: test_stdlib.py プロジェクト: 4n6strider/nengo
def test_groupby(hashable, force_list, rng):
    if hashable:
        keys = list(range(1, 5))
    else:
        keys = [[0, 0], [0, 1], [1, 0], [1, 1]]

    keys = sorted(keys)

    # make groups and pairs
    groups = [rng.randn(rng.randint(5, 10)) for _ in keys]

    pairs = []
    for key, group in zip(keys, groups):
        pairs.extend((key, value) for value in group)

    # shuffle pairs
    pairs = [pairs[i] for i in rng.permutation(len(pairs))]

    # call groupby
    keygroups = groupby(pairs, lambda p: p[0], force_list=force_list)

    keys2 = sorted(map(lambda x: x[0], keygroups))
    assert keys2 == keys

    for key2, keygroup2 in keygroups:
        group = groups[keys.index(key2)]
        group2 = map(lambda x: x[1], keygroup2)
        assert sorted(group2) == sorted(group)
コード例 #37
0
ファイル: test_stdlib.py プロジェクト: weizx208/nengo
def test_groupby(hashable, force_list, rng):
    if hashable:
        keys = list(range(1, 5))
    else:
        keys = [[0, 0], [0, 1], [1, 0], [1, 1]]

    keys = sorted(keys)

    # make groups and pairs
    groups = [rng.randn(rng.randint(5, 10)) for _ in keys]

    pairs = []
    for key, group in zip(keys, groups):
        pairs.extend((key, value) for value in group)

    # shuffle pairs
    pairs = [pairs[i] for i in rng.permutation(len(pairs))]

    # call groupby
    keygroups = groupby(pairs, lambda p: p[0], force_list=force_list)

    keys2 = sorted(map(lambda x: x[0], keygroups))
    assert keys2 == keys

    for key2, keygroup2 in keygroups:
        group = groups[keys.index(key2)]
        group2 = map(lambda x: x[1], keygroup2)
        assert sorted(group2) == sorted(group)
コード例 #38
0
ファイル: test_pointer.py プロジェクト: 4n6strider/nengo
def test_init():
    a = SemanticPointer([1, 2, 3, 4])
    assert len(a) == 4

    a = SemanticPointer([1, 2, 3, 4, 5])
    assert len(a) == 5

    a = SemanticPointer(list(range(100)))
    assert len(a) == 100

    a = SemanticPointer(27)
    assert len(a) == 27
    assert np.allclose(a.length(), 1)

    with pytest.raises(Exception):
        a = SemanticPointer(np.zeros(2, 2))

    with pytest.raises(Exception):
        a = SemanticPointer(-1)
    with pytest.raises(Exception):
        a = SemanticPointer(0)
    with pytest.raises(Exception):
        a = SemanticPointer(1.7)
    with pytest.raises(Exception):
        a = SemanticPointer(None)
    with pytest.raises(Exception):
        a = SemanticPointer(int)
コード例 #39
0
    def reset(self, seed=None):
        if self.closed:
            raise SimulatorClosed("Cannot reset closed Simulator.")

        if seed is not None:
            raise NotImplementedError("Seed changing not implemented")

        # reset signals
        for base in self.all_bases:
            # TODO: copy all data on at once
            if not base.readonly:
                self.all_data[self.sidx[base]] = base.initial_value

        for clra, ra in iteritems(self._raggedarrays_to_reset):
            # TODO: copy all data on at once
            for i in range(len(clra)):
                clra[i] = ra[i]

        # clear probe data
        if self._cl_probe_plan is not None:
            self._cl_probe_plan.cl_bufpositions.fill(0)

            for probe in self.model.probes:
                del self._probe_outputs[probe][:]

        self._reset_rng()
        self._reset_cl_rngs()
        self._probe_step_time()
コード例 #40
0
def test_basic():
    # -- prepare initial conditions on host
    A = RA([[[0.1, .2], [.3, .4]], [[.5, .6]]])
    X = RA([[3, 5]])
    Y = RA([[0.0], [2, 3], ])
    A_js = RA([[1], [0]], dtype=np.int32)
    X_js = RA([[0], [0]], dtype=np.int32)
    # alpha = 0.5
    alpha = 1.0
    # beta = 0.1
    beta = 1.0

    # -- prepare initial conditions on device
    queue = cl.CommandQueue(ctx)
    clA = CLRA(queue, A)
    clX = CLRA(queue, X)
    clY = CLRA(queue, Y)
    assert allclose(A, clA)
    assert allclose(X, clX)
    assert allclose(Y, clY)

    # -- run cl computation
    prog = plan_ragged_gather_gemv(
        queue, alpha, clA, A_js, clX, X_js, beta, clY)
    # plans = prog.choose_plans()
    # assert len(plans) == 1
    for plan in prog.plans:
        plan()

    # -- ensure they match
    for i in range(len(A_js)):
        aj, xj = int(A_js[i]), int(X_js[i])
        ref = alpha * np.dot(A[aj], X[xj]) + beta * Y[i]
        sim = clY[i]
        assert np.allclose(ref, sim)
コード例 #41
0
ファイル: test_probe.py プロジェクト: CamZHU/nengo
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
コード例 #42
0
def test_linearfilter(ctx, n_per_kind, rng):
    kinds = (
        nengo.synapses.LinearFilter((2.,), (1.,), analog=False),
        nengo.synapses.Lowpass(0.005),
        nengo.synapses.Alpha(0.005),
        )
    assert len(n_per_kind) == len(kinds)
    kinds_n = [(kind, n) for kind, n in zip(kinds, n_per_kind) if n > 0]

    dt = 0.001
    steps = [kind.make_step((n,), (n,), dt, None, dtype=np.float32)
             for kind, n in kinds_n]
    A = RA([step.den for step in steps])
    B = RA([step.num for step in steps])

    X = RA([rng.normal(size=n) for kind, n in kinds_n])
    Y = RA([np.zeros(n) for kind, n in kinds_n])
    Xbuf = RA([np.zeros(shape) for shape in zip(B.sizes, X.sizes)])
    Ybuf = RA([np.zeros(shape) for shape in zip(A.sizes, Y.sizes)])

    queue = cl.CommandQueue(ctx)
    clA = CLRA(queue, A)
    clB = CLRA(queue, B)
    clX = CLRA(queue, X)
    clY = CLRA(queue, Y)
    clXbuf = CLRA(queue, Xbuf)
    clYbuf = CLRA(queue, Ybuf)

    n_calls = 3
    plans = plan_linearfilter(queue, clX, clY, clA, clB, clXbuf, clYbuf)
    with Timer() as timer:
        for _ in range(n_calls):
            [plan() for plan in plans]

    print(timer.duration)

    for i, [kind, n] in enumerate(kinds_n):
        n = min(n, 100)
        step = kind.make_step((n, 1), (n, 1), dt, None, dtype=np.float32)

        x = X[i][:n]
        y = np.zeros_like(x)
        for _ in range(n_calls):
            y[:] = step(0, x)

        z = clY[i][:n]
        assert np.allclose(z, y, atol=1e-7, rtol=1e-5), kind
コード例 #43
0
def test_linearfilter(n_per_kind, rng):
    kinds = (
        nengo.synapses.LinearFilter((2.,), (1.,), analog=False),
        nengo.synapses.Lowpass(0.005),
        nengo.synapses.Alpha(0.005),
        )
    assert len(n_per_kind) == len(kinds)
    kinds_n = [(kind, n) for kind, n in zip(kinds, n_per_kind) if n > 0]

    dt = 0.001
    steps = [kind.make_step((n,), (n,), dt, None, dtype=np.float32)
             for kind, n in kinds_n]
    A = RA([step.den for step in steps])
    B = RA([step.num for step in steps])

    X = RA([rng.normal(size=n) for kind, n in kinds_n])
    Y = RA([np.zeros(n) for kind, n in kinds_n])
    Xbuf = RA([np.zeros(shape) for shape in zip(B.sizes, X.sizes)])
    Ybuf = RA([np.zeros(shape) for shape in zip(A.sizes, Y.sizes)])

    queue = cl.CommandQueue(ctx)
    clA = CLRA(queue, A)
    clB = CLRA(queue, B)
    clX = CLRA(queue, X)
    clY = CLRA(queue, Y)
    clXbuf = CLRA(queue, Xbuf)
    clYbuf = CLRA(queue, Ybuf)

    n_calls = 3
    plan = plan_linearfilter(queue, clX, clY, clA, clB, clXbuf, clYbuf)
    with Timer() as timer:
        for _ in range(n_calls):
            plan()

    print(timer.duration)

    for i, [kind, n] in enumerate(kinds_n):
        n = min(n, 100)
        step = kind.make_step((n, 1), (n, 1), dt, None, dtype=np.float32)

        x = X[i][:n]
        y = np.zeros_like(x)
        for _ in range(n_calls):
            y[:] = step(0, x)

        z = clY[i][:n]
        assert np.allclose(z, y, atol=1e-7, rtol=1e-5), kind
コード例 #44
0
ファイル: test_ensemblearray.py プロジェクト: Tayyar/nengo
 def plot(sim, a, p, title=""):
     a_ref = np.tile(a, (len(t), 1))
     a_sim = sim.data[p]
     colors = ['b', 'g', 'r', 'c', 'm', 'y']
     for i in range(a_sim.shape[1]):
         plt.plot(t, a_ref[:, i], '--', color=colors[i % 6])
         plt.plot(t, a_sim[:, i], '-', color=colors[i % 6])
     plt.title(title)
コード例 #45
0
ファイル: analysis.py プロジェクト: tbekolay/phd
def cl_timing(recorded, target):
    recalign, tgtalign, rectimes, tgttimes = cl2string(recorded, target)

    correct_ix = [i for i in range(len(recalign))
                  if recalign[i] == tgtalign[i]]
    t_diff = [rectimes[i] - tgttimes[i] for i in correct_ix]

    return np.mean(t_diff), np.var(t_diff)
コード例 #46
0
 def plot(actual, probe, title=""):
     ref_y = np.tile(actual, (len(t), 1))
     sim_y = sim.data[probe]
     colors = ['b', 'g', 'r', 'c', 'm', 'y']
     for i in range(min(dims, len(colors))):
         plt.plot(t, ref_y[:, i], '--', color=colors[i])
         plt.plot(t, sim_y[:, i], '-', color=colors[i])
         plt.title(title)
コード例 #47
0
 def plot(sim, a, p, title=""):
     a_ref = np.tile(a, (len(t), 1))
     a_sim = sim.data[p]
     colors = ['b', 'g', 'r', 'c', 'm', 'y']
     for i in range(a_sim.shape[1]):
         plt.plot(t, a_ref[:, i], '--', color=colors[i % 6])
         plt.plot(t, a_sim[:, i], '-', color=colors[i % 6])
     plt.title(title)
コード例 #48
0
    def __init__(self,
                 n_neurons,
                 n_ensembles,
                 ens_dimensions=1,
                 neuron_nodes=False,
                 label=None,
                 seed=None,
                 add_to_container=None,
                 **ens_kwargs):
        if "dimensions" in ens_kwargs:
            raise ValidationError(
                "'dimensions' is not a valid argument to EnsembleArray. "
                "To set the number of ensembles, use 'n_ensembles'. To set "
                "the number of dimensions per ensemble, use 'ens_dimensions'.",
                attr='dimensions',
                obj=self)

        super(EnsembleArray, self).__init__(label, seed, add_to_container)

        for param in ens_kwargs:
            if is_iterable(ens_kwargs[param]):
                ens_kwargs[param] = nengo.dists.Samples(ens_kwargs[param])

        self.config[nengo.Ensemble].update(ens_kwargs)

        label_prefix = "" if label is None else label + "_"

        self.n_neurons = n_neurons
        self.n_ensembles = n_ensembles
        self.dimensions_per_ensemble = ens_dimensions

        # These may be set in add_neuron_input and add_neuron_output
        self.neuron_input, self.neuron_output = None, None

        self.ea_ensembles = []

        with self:
            self.input = nengo.Node(size_in=self.dimensions, label="input")

            for i in range(n_ensembles):
                e = nengo.Ensemble(n_neurons,
                                   self.dimensions_per_ensemble,
                                   label="%s%d" % (label_prefix, i))
                nengo.Connection(self.input[i * ens_dimensions:(i + 1) *
                                            ens_dimensions],
                                 e,
                                 synapse=None)
                self.ea_ensembles.append(e)

        if neuron_nodes:
            self.add_neuron_input()
            self.add_neuron_output()
            warnings.warn(
                "'neuron_nodes' argument will be removed in Nengo 2.2. Use "
                "'add_neuron_input' and 'add_neuron_output' methods instead.",
                DeprecationWarning)

        self.add_output('output', function=None)
コード例 #49
0
ファイル: neurons.py プロジェクト: rsantana-isg/nengo
    def gain_bias(self, max_rates, intercepts):
        """Compute the gain and bias needed to satisfy max_rates, intercepts.

        This takes the neurons, approximates their response function, and then
        uses that approximation to find the gain and bias value that will give
        the requested intercepts and max_rates.

        Note that this default implementation is very slow! Whenever possible,
        subclasses should override this with a neuron-specific implementation.

        Parameters
        ----------
        max_rates : ndarray(dtype=float64)
            Maximum firing rates of neurons.
        intercepts : ndarray(dtype=float64)
            X-intercepts of neurons.

        Returns
        -------
        gain : ndarray(dtype=float64)
            Gain associated with each neuron. Sometimes denoted alpha.
        bias : ndarray(dtype=float64)
            Bias current associated with each neuron.
        """
        J_max = 0
        J_steps = 101  # Odd number so that 0 is a sample
        max_rate = max_rates.max()

        # Start with dummy gain and bias so x == J in rate calculation
        gain = np.ones(J_steps)
        bias = np.zeros(J_steps)
        rate = np.zeros(J_steps)

        # Find range of J that will achieve max rates
        while rate[-1] < max_rate and J_max < 100:
            J_max += 10
            J = np.linspace(-J_max, J_max, J_steps)
            rate = self.rates(J, gain, bias)
        J_threshold = J[np.where(rate <= 1e-16)[0][-1]]

        gain = np.zeros_like(max_rates)
        bias = np.zeros_like(max_rates)
        for i in range(intercepts.size):
            ix = np.where(rate > max_rates[i])[0]
            if len(ix) == 0:
                ix = -1
            else:
                ix = ix[0]
            if rate[ix] == rate[ix - 1]:
                p = 1
            else:
                p = (max_rates[i] - rate[ix - 1]) / (rate[ix] - rate[ix - 1])
            J_top = p * J[ix] + (1 - p) * J[ix - 1]

            gain[i] = (J_threshold - J_top) / (intercepts[i] - 1)
            bias[i] = J_top - gain[i]

        return gain, bias
コード例 #50
0
def _test_conn(OclOnlySimulator, fn, size_in, dist_in=None, n=1):
    seed = sum(map(ord, fn.__name__)) % 2**30
    rng = np.random.RandomState(seed + 1)

    # make input
    if dist_in is None:
        dist_in = [Uniform(-10, 10) for i in range(size_in)]
    elif not isinstance(dist_in, (list, tuple)):
        dist_in = [dist_in]
    assert len(dist_in) == size_in

    x = list(zip(*[d.sample(n, rng=rng) for d in dist_in]))  # preserve types
    # x = zip(*[arggen.gen(n, rng=rng) for arggen in arggens])
    y = [fn(xx) for xx in x]

    y = np.array([fn(xx) for xx in x])
    if y.ndim < 2:
        y.shape = (n, 1)
    size_out = y.shape[1]

    # make model
    model = nengo.Network("test_%s" % fn.__name__, seed=seed)
    with model:
        probes = []
        for i in range(n):
            u = nengo.Node(output=x[i])
            v = nengo.Ensemble(1,
                               dimensions=size_in,
                               neuron_type=nengo.Direct())
            w = nengo.Ensemble(1,
                               dimensions=size_out,
                               neuron_type=nengo.Direct())
            nengo.Connection(u, v, synapse=None)
            nengo.Connection(v, w, synapse=None, function=fn, eval_points=x)
            probes.append(nengo.Probe(w))

    # run model
    with OclOnlySimulator(model) as sim:
        sim.step()
        # sim.step()
        # sim.step()

    # compare output
    z = np.array([sim.data[p][-1] for p in probes])
    assert np.allclose(z, y, atol=2e-7)
コード例 #51
0
def cl_timing(recorded, target):
    recalign, tgtalign, rectimes, tgttimes = cl2string(recorded, target)

    correct_ix = [
        i for i in range(len(recalign)) if recalign[i] == tgtalign[i]
    ]
    t_diff = [rectimes[i] - tgttimes[i] for i in correct_ix]

    return np.mean(t_diff), np.var(t_diff)
コード例 #52
0
 def run_steps(self, n_steps, d=None, dt=None, rng=np.random):
     d = self.default_size_out if d is None else d
     dt = self.default_dt if dt is None else dt
     rng = self.get_rng(rng)
     step = self.make_step(0, d, dt, rng)
     output = np.zeros((n_steps, d))
     for i in range(n_steps):
         output[i] = step(i * dt)
     return output
コード例 #53
0
    def __init__(self,
                 n_neurons,
                 n_ensembles,
                 ens_dimensions=1,
                 neuron_nodes=False,
                 label=None,
                 **ens_kwargs):
        if "dimensions" in ens_kwargs:
            raise TypeError(
                "'dimensions' is not a valid argument to EnsembleArray. "
                "To set the number of ensembles, use 'n_ensembles'. To set "
                "the number of dimensions per ensemble, use 'ens_dimensions'.")

        self.config[nengo.Ensemble].update(ens_kwargs)

        label_prefix = "" if label is None else label + "_"

        self.n_neurons = n_neurons
        self.n_ensembles = n_ensembles
        self.dimensions_per_ensemble = ens_dimensions

        self.input = nengo.Node(size_in=self.dimensions, label="input")

        if neuron_nodes:
            self.neuron_input = nengo.Node(size_in=n_neurons * n_ensembles,
                                           label="neuron_input")
            self.neuron_output = nengo.Node(size_in=n_neurons * n_ensembles,
                                            label="neuron_output")

        self.ea_ensembles = []
        for i in range(n_ensembles):
            e = nengo.Ensemble(n_neurons,
                               self.dimensions_per_ensemble,
                               label=label_prefix + str(i))

            nengo.Connection(self.input[i * ens_dimensions:(i + 1) *
                                        ens_dimensions],
                             e,
                             synapse=None)

            if neuron_nodes and not isinstance(e.neuron_type, nengo.Direct):
                nengo.Connection(self.neuron_input[i * n_neurons:(i + 1) *
                                                   n_neurons],
                                 e.neurons,
                                 synapse=None)
                nengo.Connection(e.neurons,
                                 self.neuron_output[i * n_neurons:(i + 1) *
                                                    n_neurons],
                                 synapse=None)

            self.ea_ensembles.append(e)

        if neuron_nodes and isinstance(e.neuron_type, nengo.Direct):
            warnings.warn("Creating neuron nodes in an EnsembleArray"
                          " with Direct neurons")

        self.add_output('output', function=None)
コード例 #54
0
 def run_steps(self, n_steps, d=None, dt=None, rng=np.random):
     # TODO: allow running with input
     d = self.default_size_out if d is None else d
     dt = self.default_dt if dt is None else dt
     step = self.make_step(0, d, dt, rng)
     output = np.zeros((n_steps, d))
     for i in range(n_steps):
         output[i] = step(i * dt)
     return output
コード例 #55
0
 def plot(sim, expected, probe, title=""):
     simdata = sim.data[probe]
     colors = ['b', 'g', 'r', 'c']
     for i in range(simdata.shape[1]):
         plt.axhline(expected[i], ls='--', color=colors[i % 4])
         plt.plot(t, simdata[:, i], color=colors[i % 4])
     plt.xticks(np.linspace(0, 0.4, 5))
     plt.xlim(right=t[-1])
     plt.title(title)
コード例 #56
0
def whitenoise(step, high, rms=0.5, seed=None, dimensions=None):
    """Generate white noise inputs

    Parameters
    ----------
    step : float
        The step size of different frequencies to generate

    high : float
        The highest frequency to generate (should be a multiple of step)

    rms : float
        The RMS power of the signal

    seed : int or None
        Random number seed

    dimensions : int or None
        The number of different random signals to generate.  The resulting
        function will return an array of length `dimensions` for every
        point in time.  If `dimensions` is None, the resulting function will
        just return a float for each point in time.

    Returns
    -------
    function:
        A function that takes a variable t and returns the value of the
        randomly generated signal.  This value is a float if `dimensions` is
        None; otherwise it is a list of length `dimensions`.
    """
    rng = np.random.RandomState(seed)

    if dimensions is not None:
        signals = [
            whitenoise(step, high, rms=rms, seed=rng.randint(0x7ffffff))
            for i in range(dimensions)
        ]

        def whitenoise_function(t, signals=signals):
            return [signal(t) for signal in signals]

        return whitenoise_function

    N = int(float(high) / step)  # number of samples
    frequencies = np.arange(1, N + 1) * step * 2 * np.pi  # frequency of each
    amplitude = rng.uniform(0, 1, N)  # amplitude for each sample
    phase = rng.uniform(0, 2 * np.pi, N)  # phase of each sample

    # compute the rms of the signal
    rawRMS = np.sqrt(np.sum(amplitude**2) / 2)
    amplitude = amplitude * rms / rawRMS  # rescale

    # create a function that computes the bases and weights them by amplitude
    def whitenoise_function(t, f=frequencies, a=amplitude, p=phase):
        return np.dot(np.sin(f * t[..., np.newaxis] + p), a)

    return whitenoise_function
コード例 #57
0
def get_filterbanks(n_filters=20, n_fft=512, fs=16000,
                    minfreq=0, maxfreq=None):
    """Compute a Mel-filterbank.

    The filters are stored in the rows, the columns correspond to fft bins.
    The filters are returned as an array of shape (n_filters, n_fft/2 + 1).

    Parameters
    ----------
    n_filters : int, optional
        The number of filters in the filterbank. Default: 20
    n_fft : int, optional
        The FFT size. Default: 512
    fs : float, optional
        The samplerate of the signal we are working with. Affects mel spacing.
        Default: 16000
    minfreq : int, optional
        Lowest band edge of mel filters, in Hz. Default: 0
    maxfreq : int, optional
        highest band edge of mel filters, in Hz. Default: fs / 2

    Returns
    -------
    Numpy array of shape (n_filters, n_fft/2 + 1) containing the filterbank.
    Each row holds 1 filter.
    """
    maxfreq = fs // 2 if maxfreq is None else maxfreq
    assert maxfreq <= fs // 2, "maxfreq is greater than fs/2"

    # compute points evenly spaced in mels
    lowmel = hz2mel(minfreq)
    highmel = hz2mel(maxfreq)
    melpoints = np.linspace(lowmel, highmel, n_filters + 2)
    # Our points are in Hz, but we use fft bins, so we have to convert
    # from Hz to fft bin number
    fbin = np.floor((n_fft + 1) * mel2hz(melpoints) / fs)

    fbank = np.zeros([n_filters, int(n_fft/2+1)])
    for j in range(n_filters):
        for i in range(int(fbin[j]), int(fbin[j + 1])):
            fbank[j, i] = (i - fbin[j]) / (fbin[j + 1] - fbin[j])
        for i in range(int(fbin[j + 1]), int(fbin[j + 2])):
            fbank[j, i] = (fbin[j + 2] - i) / (fbin[j + 2] - fbin[j + 1])
    return fbank
コード例 #58
0
def ss2tf(A, B, C, D, input=0):
    """State-space to transfer function.

    Parameters
    ----------
    A, B, C, D : ndarray
        State-space representation of linear system.
    input : int, optional
        For multiple-input systems, the input to use.

    Returns
    -------
    num : 2-D ndarray
        Numerator(s) of the resulting transfer function(s).  `num` has one row
        for each of the system's outputs. Each row is a sequence representation
        of the numerator polynomial.
    den : 1-D ndarray
        Denominator of the resulting transfer function(s).  `den` is a sequence
        representation of the denominator polynomial.

    """
    # transfer function is C (sI - A)**(-1) B + D
    A, B, C, D = map(asarray, (A, B, C, D))
    # Check consistency and make them all rank-2 arrays
    A, B, C, D = abcd_normalize(A, B, C, D)

    nout, nin = D.shape
    if input >= nin:
        raise ValueError("System does not have the input specified.")

    # make MOSI from possibly MOMI system.
    if B.shape[-1] != 0:
        B = B[:, input]
    B.shape = (B.shape[0], 1)
    if D.shape[-1] != 0:
        D = D[:, input]

    try:
        den = poly(A)
    except ValueError:
        den = 1

    if (product(B.shape, axis=0) == 0) and (product(C.shape, axis=0) == 0):
        num = np.ravel(D)
        if (product(D.shape, axis=0) == 0) and (product(A.shape, axis=0) == 0):
            den = []
        return num, den

    num_states = A.shape[0]
    type_test = A[:, 0] + B[:, 0] + C[0, :] + D
    num = np.zeros((nout, num_states + 1), type_test.dtype)
    for k in range(nout):
        Ck = atleast_2d(C[k, :])
        num[k] = poly(A - dot(B, Ck)) + (D[k] - 1) * den

    return num, den
コード例 #59
0
 def plot(sim, a, p, title=""):
     a_ref = np.tile(a, (len(t), 1))
     a_sim = sim.data[p]
     colors = ['b', 'g', 'r', 'c', 'm', 'y']
     for i in range(a_sim.shape[1]):
         plt.plot(t, a_ref[:, i], '--', color=colors[i % 6])
         plt.plot(t, a_sim[:, i], '-', color=colors[i % 6])
     plt.xticks(np.linspace(0, 0.4, 5))
     plt.xlim(right=t[-1])
     plt.title(title)