Beispiel #1
0
 def __init__(self, dist=Gaussian(mean=0, std=1), **kwargs):
     super().__init__(synapse=LinearFilter([1], [1, 0]),
                      synapse_kwargs=dict(method='euler'),
                      dist=dist,
                      **kwargs)
Beispiel #2
0
 def __init__(self, dist=Gaussian(mean=0, std=1), scale=True, **kwargs):
     super().__init__(default_size_in=0, **kwargs)
     self.dist = dist
     self.scale = scale
Beispiel #3
0
 def __init__(self, dist=Gaussian(mean=0, std=1), **kwargs):
     super().__init__(synapse=LinearFilter([1], [1, 0], method="euler"),
                      dist=dist,
                      **kwargs)
def create_model(seed=None):

    #create vocabulary to show representations in gui
    if nengo_gui_on:
        vocab_angles = spa.Vocabulary(D)
        for name in [0, 5, 10, 16, 24, 32, 40]:
            #vocab_angles.add('D' + str(name), np.linalg.norm(compressed_im_first[name+90])) #take mean across phases
            v = compressed_im_first[name + 90]
            nrm = np.linalg.norm(v)
            if nrm > 0:
                v /= nrm
            vocab_angles.add('D' + str(name), v)  #take mean across phases

        v = np.dot(imagearr[-1, :] / 50, U_first[:, :D])
        nrm = np.linalg.norm(v)
        if nrm > 0:
            v /= nrm
        vocab_angles.add('Impulse', v)

    # model = nengo.Network(seed=seed)
    model = spa.SPA(seed=seed)
    with model:

        #input nodes
        if not batch_processing:
            model.inputNode_first = nengo.Node(input_func_first,
                                               label='input_first')

        elif batch_processing:
            model.inputNode_first = nengo.Node(np.zeros(128 * 128))

        #eye ensemble
        eye_first = nengo.Ensemble(Ns,
                                   D,
                                   encoders=e_first,
                                   intercepts=Uniform(0.01, 0.1),
                                   radius=1,
                                   label='eye_first',
                                   seed=seed)
        nengo.Connection(model.inputNode_first,
                         eye_first,
                         transform=U_first[:, :D].T)

        #sensory ensemble
        sensory_first = nengo.Ensemble(Ns,
                                       D,
                                       encoders=e_first,
                                       intercepts=Uniform(0.01, .1),
                                       radius=1,
                                       label='sensory_first')

        conn_first = nengo.Connection(
            eye_first, sensory_first,
            function=lambda x: x)  #default: identity function

        if nengo_gui_on:
            with nengo.Simulator(network=model,
                                 progress_bar=False) as sim_first:
                weights_first = sim_first.data[conn_first].weights

            model.connections.remove(conn_first)

        elif not (nengo_gui_on):
            with nengo_dl.Simulator(
                    network=model, progress_bar=False,
                    device=device) as sim_first:  #, device=device
                weights_first = sim_first.data[conn_first].weights

            model.connections.remove(conn_first)

        Ncluster = 1  # number of neurons per cluster
        clusters = np.arange(0, Ns, Ncluster)

        #parameters for gamma distribution where k=shape, theta=scale and mean=k*theta -> theta=mean/k
        # wanted distribution: lowest value 51ms and mean value 141 ms (Lamme & Roelfsema (2000))
        k, mean = 2, 0.090
        theta = mean / k
        shift = 0.051

        weights_trans = 1.50
        rec_trans = 0.30

        noise_sd = 0.010

        if (True):
            # Build second simulation to fetch weights before making the large amount of connections between eye_first and sensory_first, place all
            # of second model inside this block statement to keep code close together
            if not batch_processing:
                model.inputNode_second = nengo.Node(input_func_second,
                                                    label='input_second')
                model.reactivate = nengo.Node(reactivate_func,
                                              label='reactivate')
            elif batch_processing:
                model.inputNode_second = nengo.Node(np.zeros(128 * 128))
                model.reactivate = nengo.Node(np.zeros(Nm))

            eye_second = nengo.Ensemble(Ns,
                                        D,
                                        encoders=e_second,
                                        intercepts=Uniform(0.01, 0.1),
                                        radius=1,
                                        label='eye_second',
                                        seed=seed)
            nengo.Connection(model.inputNode_second,
                             eye_second,
                             transform=U_second[:, :D].T)

            sensory_second = nengo.Ensemble(Ns,
                                            D,
                                            encoders=e_second,
                                            intercepts=Uniform(0.01, .1),
                                            radius=1,
                                            label='sensory_second')

            conn_second = nengo.Connection(
                eye_second, sensory_second,
                function=lambda x: x)  #default: identity function

            if nengo_gui_on:
                with nengo.Simulator(network=model,
                                     progress_bar=False) as sim_second:
                    weights_second = sim_second.data[conn_second].weights

                model.connections.remove(conn_second)

            elif not (nengo_gui_on):
                with nengo_dl.Simulator(
                        network=model, progress_bar=False,
                        device=device) as sim_second:  #device=device
                    weights_second = sim_second.data[conn_second].weights

                model.connections.remove(conn_second)

            synapse_second = np.random.gamma(k, theta, clusters.size) + shift

            for i in range(clusters.size):

                begin = clusters[i]
                end = (begin + Ncluster)

                nengo.Connection(eye_second.neurons[begin:end],
                                 sensory_second,
                                 transform=weights_second[:, begin:end] *
                                 weights_trans,
                                 synapse=synapse_second[i])

            nengo.Connection(sensory_second,
                             eye_second,
                             transform=rec_trans,
                             solver=nengo.solvers.LstsqL2(weights=True),
                             synapse=0.200)

            noise_second = nengo.processes.WhiteNoise(
                dist=Gaussian(0, noise_sd))

            memory_second = nengo.Ensemble(Nm,
                                           D,
                                           neuron_type=stpLIF(),
                                           intercepts=Uniform(0.01, 0.1),
                                           noise=noise_second,
                                           radius=1,
                                           label='memory_second')
            nengo.Connection(sensory_second, memory_second, transform=0.1)
            nengo.Connection(model.reactivate,
                             memory_second.neurons)  #potential reactivation

            nengo.Connection(memory_second,
                             memory_second,
                             transform=1,
                             learning_rule_type=STP(),
                             solver=nengo.solvers.LstsqL2(weights=True))

            comparison_second = nengo.Ensemble(Nd,
                                               dimensions=4,
                                               radius=math.sqrt(2),
                                               intercepts=Uniform(.01, 1),
                                               label='comparison_second')

            nengo.Connection(memory_second,
                             comparison_second[2:],
                             eval_points=compressed_im_second[0:-1],
                             function=sincos.T)
            nengo.Connection(sensory_second,
                             comparison_second[:2],
                             eval_points=compressed_im_second[0:-1],
                             function=sincos.T)

            decision_second = nengo.Ensemble(n_neurons=Nd,
                                             dimensions=1,
                                             radius=45,
                                             label='decision_second')
            nengo.Connection(comparison_second,
                             decision_second,
                             eval_points=ep,
                             scale_eval_points=False,
                             function=arctan_func)

        ### first ###

        synapse_first = np.random.gamma(k, theta, clusters.size) + shift

        for i in range(clusters.size):

            begin = clusters[i]
            end = (begin + Ncluster)

            nengo.Connection(eye_first.neurons[begin:end],
                             sensory_first,
                             transform=weights_first[:, begin:end] *
                             weights_trans,
                             synapse=synapse_first[i])

        nengo.Connection(sensory_first,
                         eye_first,
                         transform=rec_trans,
                         solver=nengo.solvers.LstsqL2(weights=True),
                         synapse=0.200)

        #memory ensemble
        noise_first = nengo.processes.WhiteNoise(dist=Gaussian(0, noise_sd))

        memory_first = nengo.Ensemble(Nm,
                                      D,
                                      neuron_type=stpLIF(),
                                      intercepts=Uniform(0.01, 0.1),
                                      noise=noise_first,
                                      radius=1,
                                      label='memory_first')
        nengo.Connection(sensory_first, memory_first, transform=0.1)  #.1)

        #recurrent STSP connection
        nengo.Connection(memory_first,
                         memory_first,
                         transform=1,
                         learning_rule_type=STP(),
                         solver=nengo.solvers.LstsqL2(weights=True))

        #comparison represents sin, cosine of theta of both sensory and memory ensemble
        comparison_first = nengo.Ensemble(Nc,
                                          dimensions=4,
                                          radius=math.sqrt(2),
                                          intercepts=Uniform(.01, 1),
                                          label='comparison_first')
        nengo.Connection(sensory_first,
                         comparison_first[:2],
                         eval_points=compressed_im_first[0:-1],
                         function=sincos.T)
        nengo.Connection(memory_first,
                         comparison_first[2:],
                         eval_points=compressed_im_first[0:-1],
                         function=sincos.T)

        #decision represents the difference in theta decoded from the sensory and memory ensembles
        decision_first = nengo.Ensemble(n_neurons=Nd,
                                        dimensions=1,
                                        radius=45,
                                        label='decision_first')
        nengo.Connection(comparison_first,
                         decision_first,
                         eval_points=ep,
                         scale_eval_points=False,
                         function=arctan_func)

        #decode for gui
        if nengo_gui_on:
            model.sensory_decode = spa.State(D,
                                             vocab=vocab_angles,
                                             subdimensions=12,
                                             label='sensory_decode')
            for ens in model.sensory_decode.all_ensembles:
                ens.neuron_type = nengo.Direct()
            nengo.Connection(sensory_first,
                             model.sensory_decode.input,
                             synapse=None)

            model.memory_decode = spa.State(D,
                                            vocab=vocab_angles,
                                            subdimensions=12,
                                            label='memory_decode')
            for ens in model.memory_decode.all_ensembles:
                ens.neuron_type = nengo.Direct()
            nengo.Connection(memory_first,
                             model.memory_decode.input,
                             synapse=None)

        #probes
        if not (nengo_gui_on):
            if store_representations:  #sim 1 trials 1-100

                model.p_mem_first = nengo.Probe(memory_first, synapse=0.05)
                model.p_mem_second = nengo.Probe(memory_second, synapse=0.05)

            if store_spikes_and_resources:  #sim 1 trial 1
                model.p_spikes_mem_first = nengo.Probe(memory_first.neurons,
                                                       'spikes')
                model.p_res_first = nengo.Probe(memory_first.neurons,
                                                'resources')
                model.p_cal_first = nengo.Probe(memory_first.neurons,
                                                'calcium')

                model.p_spikes_mem_second = nengo.Probe(
                    memory_second.neurons, 'spikes')
                model.p_res_second = nengo.Probe(memory_second.neurons,
                                                 'resources')
                model.p_cal_second = nengo.Probe(memory_second.neurons,
                                                 'calcium')

            if store_decisions:  #sim 2
                model.p_dec_first = nengo.Probe(decision_first, synapse=0.01)
                model.p_dec_second = nengo.Probe(decision_second, synapse=0.01)

            if store_memory:
                model.p_mem_first_raw = nengo.Probe(memory_first.neurons,
                                                    'output',
                                                    synapse=0.05)
                model.p_mem_second_raw = nengo.Probe(memory_second.neurons,
                                                     'output',
                                                     synapse=0.05)

    return model
Beispiel #5
0
 def __init__(self, dist=Gaussian(mean=0, std=1), seed=None):
     super(BrownNoise, self).__init__(synapse=LinearFilter([1], [1, 0]),
                                      synapse_kwargs=dict(method='euler'),
                                      dist=dist,
                                      seed=seed)
Beispiel #6
0
 def __init__(self, dist=Gaussian(mean=0, std=1), scale=True, seed=None):
     super(WhiteNoise, self).__init__(seed=seed)
     self.dist = dist
     self.scale = scale