Пример #1
0
def build_basic_network(input_stimuli_rates, input_stimuli_duration,
                        class_stimuli_rates, class_stimuli_duration,
                        record, ioffset, train, sim):
    # Create main input and class populations
    input_populations = [create_input_population(CLASS_POP_SIZE, record, sim)
                         for _ in input_stimuli_rates]

    if isinstance(ioffset, list) or isinstance(ioffset, np.ndarray):
        assert len(ioffset) == len(class_stimuli_rates)
        class_populations = [create_class_population(CLASS_POP_SIZE, record, o, train, sim)
                             for i, o in enumerate(ioffset)]
    else:
        print ioffset
        class_populations = [create_class_population(CLASS_POP_SIZE, record, ioffset, train, sim)
                             for _ in class_stimuli_rates]

    # Create pre-synaptic stimuli populations
    pre_stimuli_connector = sim.OneToOneConnector()
    pre_stimuli_synapse = sim.StaticSynapse(weight=PRE_STIMULI_WEIGHT)
    for i, (rate, input_pop) in enumerate(zip(input_stimuli_rates, input_populations)):
        # Convert stimuli into spike times
        spike_times = generate_stimuli_spike_times(rate, input_stimuli_duration, CLASS_POP_SIZE)

        # Build spike source array with these times
        stim_pop = sim.Population(CLASS_POP_SIZE, sim.SpikeSourceArray(spike_times=spike_times),
                                  label="pre_stimuli_%u" % i)

        # Connect spike source to input
        sim.Projection(stim_pop, input_pop, pre_stimuli_connector, pre_stimuli_synapse, receptor_type="excitatory",
                       label="%s-%s" % (stim_pop.label, input_pop.label))

    # Create training spike source array populations
    post_stimuli_connector = sim.OneToOneConnector()
    post_stimuli_synapse = sim.StaticSynapse(weight=POST_STIMULI_WEIGHT)
    for i, (rate, class_pop) in enumerate(zip(class_stimuli_rates, class_populations)):
        # Convert stimuli into spike times
        spike_times = generate_stimuli_spike_times(rate, class_stimuli_duration, CLASS_POP_SIZE)

        # Build spike source array with these times
        stim_pop = sim.Population(CLASS_POP_SIZE, sim.SpikeSourceArray, {"spike_times": spike_times},
                                  label="post_stimuli_%u" % i)

        # Connect spike source to input
        sim.Projection(stim_pop, class_pop, post_stimuli_connector, post_stimuli_synapse, receptor_type="excitatory",
                       label="%s-%s" % (stim_pop.label, class_pop.label))

    # Return created populations
    return input_populations, class_populations
Пример #2
0
def create_input_population(size, record, sim):
    # Population parameters
    p = sim.Population(size, sim.IF_curr_exp(**cell_params))
    if record:
        # **YUCK** record spikes actually entirely ignores
        # sampling interval but throws exception if it is not set
        p.record("spikes", sampling_interval=100.0)

    return p
Пример #3
0
def create_class_population(size, record, ioffset, train, sim):
    params = deepcopy(cell_params)
    params["bias_enabled"] = False
    params["plasticity_enabled"] = train
    params["i_offset"] = ioffset

    # Population parameters
    p = sim.Population(size, bcpnn.IF_curr_exp(**params))
    if record:
        # **YUCK** record spikes actually entirely ignores
        # sampling interval but throws exception if it is not set
        p.record("spikes", sampling_interval=100.0)

    if train:
        p.record("bias", sampling_interval=100.0)

    return p
    def __init__(self, name, sim, rng,
                 num_excitatory, num_inhibitory, JE, JI,
                 e_cell_model, i_cell_model,
                 e_cell_params, i_cell_params,
                 e_cell_flush_time, e_cell_mean_firing_rate,
                 stim_spike_times, wta, background_weight, background_rate,
                 stim_weight, simtime,
                 record_bias, record_spikes, record_membrane):

        logger.info("Creating HCU:%s" % name)

        logger.debug("num excitatory:%u, num inhibitory:%u",
                     num_excitatory, num_inhibitory)

        # compute number of excitatory synapses on neuron
        num_excitatory_synapses = int(epsilon * num_excitatory)

        # Cache recording flags
        self.record_bias = record_bias
        self.record_spikes = record_spikes
        self.record_membrane = record_membrane
        self.wta = wta

        logger.debug("Membrane potentials uniformly distributed between %g mV and %g mV.", -80, U0)
        membrane_voltage_distribution = RandomDistribution("uniform", low=-80.0, high=U0, rng=rng)

        logger.debug("Creating excitatory population with %d neurons.", num_excitatory)
        self.e_cells = sim.Population(num_excitatory, e_cell_model(**e_cell_params),
                                      label="%s - e_cells" % name)
        self.e_cells.initialize(v=membrane_voltage_distribution)

        # Set e cell mean firing rate
        self.e_cells.spinnaker_config.mean_firing_rate = e_cell_mean_firing_rate

        # **HACK** issue #18 means that we end up with 1024 wide clusters
        # which needs a lot of 256-wide neuron and synapse cores
        self.e_cells.spinnaker_config.max_cluster_width = 512

        # Set flush time
        self.e_cells.spinnaker_config.flush_time = e_cell_flush_time

        # **YUCK** record spikes actually entirely ignores
        # sampling interval but throws exception if it is not set
        if self.record_spikes:
            self.e_cells.record("spikes", sampling_interval=1000.0)

        if self.record_bias:
            self.e_cells.record("bias", sampling_interval=1000.0)

        if self.record_membrane:
            self.e_cells.record("v", sampling_interval=1000.0)

        e_poisson = sim.Population(num_excitatory, sim.SpikeSourcePoisson(rate=background_rate, duration=simtime),
                                   label="%s - e_poisson" % name)

        logger.debug("Creating background->E AMPA connection weight %g nA.", background_weight)
        sim.Projection(e_poisson, self.e_cells,
                       sim.OneToOneConnector(),
                       sim.StaticSynapse(weight=background_weight, delay=delay),
                       receptor_type="excitatory")

        if self.wta:
            logger.debug("Creating inhibitory population with %d neurons.", num_inhibitory)
            self.i_cells = sim.Population(num_inhibitory, i_cell_model, i_cell_params,
                                          label="%s - i_cells" % name)
            self.i_cells.initialize(v=membrane_voltage_distribution)

            # Inhibitory cells generally fire at a low rate
            self.i_cells.spinnaker_config.mean_firing_rate = 5.0

            if self.record_spikes:
                self.i_cells.record("spikes")

            i_poisson = sim.Population(num_inhibitory, sim.SpikeSourcePoisson(rate=background_rate, duration=simtime),
                                       label="%s - i_poisson" % name)

            logger.debug("Creating I->E GABA connection with connection probability %g, weight %g nA and delay %g ms.", epsilon, JI, delay)
            I_to_E = sim.Projection(self.i_cells, self.e_cells,
                                    sim.FixedProbabilityConnector(p_connect=epsilon, rng=rng),
                                    sim.StaticSynapse(weight=JI, delay=delay),
                                    receptor_type="inhibitory")

            logger.debug("Creating E->I AMPA connection with connection probability %g, weight %g nA and delay %g ms.", epsilon, JE, delay)
            sim.Projection(self.e_cells, self.i_cells,
                           sim.FixedProbabilityConnector(p_connect=epsilon, rng=rng),
                           sim.StaticSynapse(weight=JE, delay=delay),
                           receptor_type="excitatory")

            logger.debug("Creating I->I GABA connection with connection probability %g, weight %g nA and delay %g ms.", epsilon, JI, delay)
            sim.Projection(self.i_cells, self.i_cells,
                           sim.FixedProbabilityConnector(p_connect=epsilon, rng=rng),
                           sim.StaticSynapse(weight=JI, delay=delay),
                           receptor_type="inhibitory")

            logger.debug("Creating background->I AMPA connection weight %g nA.", background_weight)
            sim.Projection(i_poisson, self.i_cells,
                           sim.OneToOneConnector(),
                           sim.StaticSynapse(weight=background_weight, delay=delay),
                           receptor_type="excitatory")

        # Create a spike source capable of stimulating entirely excitatory population
        stim_spike_source = sim.Population(num_excitatory, sim.SpikeSourceArray(spike_times=stim_spike_times))

        # Connect one-to-one to excitatory neurons
        sim.Projection(stim_spike_source, self.e_cells,
                       sim.OneToOneConnector(),
                       sim.StaticSynapse(weight=stim_weight, delay=delay),
                       receptor_type="excitatory")
    logger.addHandler(logging.StreamHandler())

    #setup_kwargs = { "spinnaker_hostname" : "192.168.240.253" }
    setup_kwargs = {"spalloc_num_boards": 1}
else:
    import pyNN.nest as sim

    setup_kwargs = {"spike_precision": "on_grid"}

# setup simulator
sim.setup(timestep=1.0, min_delay=1.0, max_delay=8.0, **setup_kwargs)

# Create population of neurons
num_neurons = cost_image.shape[0] * cost_image.shape[1]
neurons = sim.Population(num_neurons,
                         sim.IF_curr_exp(tau_refrac=30),
                         label="pop")

# Record spikes
neurons.record("spikes")

# If we're modulating delay
if delay_modulation:
    # Convert 0-255 cost to a delay from 1-7 and use a
    # weight large enough to cause an immediate spike
    delay_func = lambda c: 1.0 + (c * (7.0 / 255.0))
    weight_func = lambda c: instant_spike_weight
else:
    delay_func = lambda c: 1.0
    weight_func = lambda c: instant_spike_weight - (29.0 * (c / float(255)))