Ejemplo n.º 1
0
    def test_tutorial_works(self):

        stdp_model = learning.STDP(eta=0.05,
                                   w_in=0.5,
                                   w_out=0.5,
                                   tau=10.0,
                                   window_size=5)

        weights = np.array([[0, 0, 1.], [0, 0, 1.], [0, 0, 0]])

        spiketrain = np.array(
            [[0, 0, 1, 0, 0, 0, 1, 1, 0, 0], [1, 0, 0, 0, 0, 0, 1, 1, 0, 0],
             [0, 0, 0, 1, 0, 0, 0, 0, 1, 0]],
            dtype=bool)

        for time in range(10):
            stdp_model.weight_change(spiketrain, weights, time)

        print("Weights after")
        print(weights)

        # That's the output that I got during my first run
        expected_weights = np.array([[0, 0, 1.18586337], [0, 0, 1.17766241],
                                     [0, 0, 0]])
        nullmatrix = np.zeros((3, 3))
        assert np.array_equal(nullmatrix,
                              np.around(expected_weights - weights, 5))
Ejemplo n.º 2
0
def learning_model_standard():
    model = learning.STDP(eta=0.05,
                          w_in=0.5,
                          w_out=0.5,
                          tau=10.0,
                          window_size=5)
    return model
Ejemplo n.º 3
0
 def setup_class(cls):
     """ setup any state specific to the execution of the given class (which
     usually contains tests).
     """
     cls.spiketrain = np.zeros((2, 51), dtype=bool)
     cls.spiketrain[0, (20, 24, 25, 40, 50)] = True
     cls.spiketrain[1, (10, 45)] = True
     cls.weights = np.array([[0, 1], [0, 0]], dtype=float)
     cls.simulation_started = False
     cls.model = learning.STDP(eta=1,
                               w_in=0.5,
                               w_out=-0.5,
                               tau=5.,
                               window_size=10)
Ejemplo n.º 4
0
    def test2(self, spiketrain, weights):
        " Only incoming spikes change the weight "
        neurons, timesteps = spiketrain.shape
        weights_before = weights.copy()

        # Suggested weights
        last_spike = spiketrain[:, -1].reshape((neurons, 1))
        suggested_weights = np.where(weights_before != 0,
                                     weights_before + last_spike,
                                     weights_before)

        # Update weights by the model
        epsilon = np.finfo(float).eps
        model = learning.STDP(eta=1.,
                              w_in=1.,
                              w_out=0.,
                              tau=epsilon,
                              window_size=5)
        model.weight_change(spiketrain, weights, timesteps)

        assert np.array_equal(suggested_weights, weights)
Ejemplo n.º 5
0
    def test_tutorial_works(self):
        srm_model = spiking.SRM(neurons=3,
                                threshold=1,
                                t_current=0.3,
                                t_membrane=20,
                                eta_reset=5)

        stdp_model = learning.STDP(eta=0.05,
                                   w_in=0.5,
                                   w_out=0.5,
                                   tau=10.0,
                                   window_size=5)

        weights = np.array([[0, 0, 1.], [0, 0, 1.], [0, 0, 0]])

        spiketrain = np.array(
            [[0, 0, 1, 0, 0, 0, 1, 1, 0, 0], [1, 0, 0, 0, 0, 0, 1, 1, 0, 0],
             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
            dtype=bool)

        for time in range(10):
            srm_model.check_spikes(spiketrain, weights, time)
            stdp_model.weight_change(spiketrain, weights, time)

        # Output that I got during my first run. There's a possibility that this is wrong calculations.
        expected_spiketrain = np.array(
            [[0, 0, 1, 0, 0, 0, 1, 1, 0, 0], [1, 0, 0, 0, 0, 0, 1, 1, 0, 0],
             [0, 0, 0, 1, 0, 0, 0, 0, 1, 0]],
            dtype=bool)
        print(weights)
        expected_weights = np.array([[0, 0, 1.18586337], [0, 0, 1.17766241],
                                     [0, 0, 0]])

        assert np.array_equal(spiketrain, expected_spiketrain)
        nullmatrix = np.zeros((3, 3))
        assert np.array_equal(nullmatrix,
                              np.around(expected_weights - weights, 5))
Ejemplo n.º 6
0
def learning_model_empty():
    model = learning.STDP(eta=0.0, w_in=0.0, w_out=0.0, tau=0.0, window_size=5)
    return model
Ejemplo n.º 7
0
def test_modell():
    timesteps = 12000

    neurons_input = 180
    neurons_output = 180
    neurons = neurons_input + neurons_output

    spiking_model = spiking.SRM(neurons=neurons,
                                threshold=1.0,
                                t_current=0.3,
                                t_membrane=20,
                                eta_reset=5,
                                verbose=False)

    learning_model = learning.STDP(eta=0.05,
                                   w_in=0.0,
                                   w_out=-0.05,
                                   tau=10.0,
                                   window_size=5,
                                   verbose=False)

    weights = np.zeros((neurons, neurons))

    # Input Neurons are fully connected to the output neurons
    mu, sigma = 0.5, 0.05
    weights[:neurons_input, neurons_input:] = sigma * np.random.randn(
        neurons_input, neurons_output) + mu

    # Output Neurons are fully connected to each other with inhibitory weights
    mu, sigma = -0.5, 0.05
    weights_output = sigma * np.random.randn(neurons_output,
                                             neurons_output) + mu
    weights[neurons_input:, neurons_input:] = weights_output

    # Output neurons have exhibitory weights for their neighboring neurons
    mu, sigma = 0.5, 0.05
    b = np.eye(neurons_output) * (sigma * np.random.randn(neurons_output, 1) +
                                  mu)
    weights[neurons_input:, neurons_input:] += np.roll(b, 1, 1)
    weights_output = weights[neurons_input:, neurons_input:].copy()

    spikes = np.zeros((neurons, timesteps), dtype=bool)

    # Generate some spikes in the input layer
    for i in range(neurons_input):
        spikes[i] = tools.sound(timesteps, 20 * i, 0.4, 50)
        spikes[i + neurons_input, 20 * i - 5:20 * i + 5] = True

    for i in range(neurons_input - 1, -1, -1):
        spikes[i] = spikes[i] + tools.sound(
            timesteps, neurons_input * 20 + 20 * i, 0.4, 50)
        spikes[i + neurons_input, neurons_input * 20 + 20 * i -
               5:neurons_input * 20 + 20 * i + 5] = True

    # Weight plot
    weightplotter = plotting.WeightHeatmapAnimation()
    weightplotter.add(weights)

    save_interval = 10

    for t in range(timesteps):

        print("Timestep: ", t)

        if weightplotter and t % save_interval == 0:
            weightplotter.add(weights)

        spiking_model.simulate(spikes, weights, t)

        learning_model.weight_change(spikes, weights, t)
        # Reset output layer weights
        weights[neurons_input:, neurons_input:] = weights_output

    weightplotter.show_animation()