Beispiel #1
0
    def build_genn_current_source(self, native_params):
        # Create model using unmodified defs
        genn_model = create_custom_current_source_class(
            self.genn_currentsource_name, **self.currentsource_defs.definitions)

        # Get spike times
        step_times = native_params["stepTimes"]

        # Create empty numpy arrays to hold start and end spikes indices
        start_step = np.empty(shape=native_params.shape, dtype=np.float32)
        end_step = np.empty(shape=native_params.shape, dtype=np.float32)

        # Calculate indices for each sequence
        cum_size = 0
        for i, seq in enumerate(step_times):
            start_step[i] = cum_size
            cum_size += len(seq.value)
            end_step[i] = cum_size

        # Build initialisation dictionary
        cs_ini = {"startStep": start_step,
                  "endStep": end_step,
                  "applyIinj": np.zeros(shape=native_params.shape, dtype=np.uint8)}

        # Return with model
        return genn_model, {}, cs_ini
                "gMin": 0.1,
                "index": float(i),
            },
            stdp_var_init,
            {},
            {},
            "DeltaCurr",
            {},
            {},
        ))

# Define current source

current_source = create_custom_current_source_class(
    "current_source",
    var_name_types=[("magnitude", "scalar")],
    injection_code="$(injectCurrent, $(magnitude));",
)

# Create current input

current_input = model.add_current_source("input_current", current_source,
                                         inp_layer, {}, {"magnitude": 0.0})

# Set simulation parameters

timesteps_per_sample = (
    1000.0  # No. of timesteps one speech signal in the dataset is presented for
)
resolution = 1
    start_stimuli[0] = 0
    start_stimuli[1:] = end_stimuli[0:-1]
    
    return start_stimuli, end_stimuli

# ----------------------------------------------------------------------------
# Custom models
# ----------------------------------------------------------------------------
stim_noise_model = genn_model.create_custom_current_source_class(
    "stim_noise",

    param_names=["n", "stimMagnitude"],
    var_name_types=[("startStim", "unsigned int"), ("endStim", "unsigned int", VarAccess_READ_ONLY)],
    extra_global_params=[("stimTimes", "scalar*")],
    injection_code=
        """
        scalar current = ($(gennrand_uniform) * $(n) * 2.0) - $(n);
        if($(startStim) != $(endStim) && $(t) >= $(stimTimes)[$(startStim)]) {
           current += $(stimMagnitude);
           $(startStim)++;
        }
        $(injectCurrent, current);
        """)

# ----------------------------------------------------------------------------
# Stimuli generation
# ----------------------------------------------------------------------------
# Get standard model parameters
params = get_params(build_model=True, measure_timing=False, use_genn_recording=True)
params["seed"] = 1234
if "seed" in params:
Beispiel #4
0
        $(Isyn) += $(x);
        """)

poisson_alpha_model = genn_model.create_custom_current_source_class(
    "poisson_alpha",
    param_names=["weight", "tauSyn", "rate"],
    var_name_types=[("current", "scalar"), ("current2", "scalar")],
    derived_params=[(
        "ExpDecay",
        genn_model.create_dpf_class(lambda pars, dt: np.exp(-dt / pars[1]))()),
                    ("ExpMinusLambda",
                     genn_model.create_dpf_class(
                         lambda pars, dt: np.exp(-(pars[2] / 1000.0) * dt))()),
                    ("Init",
                     genn_model.create_dpf_class(lambda pars, dt: pars[0] *
                                                 (np.exp(1) / pars[1]))())],
    injection_code="""
        scalar p = 1.0f;
        unsigned int numPoissonSpikes = 0;
        do {
            numPoissonSpikes++;
            p *= $(gennrand_uniform);
        } while (p > $(ExpMinusLambda));

        $(current) += $(Init) * (scalar)(numPoissonSpikes - 1);
        $(injectCurrent, $(current2));
        $(current2) = (DT * $(ExpDecay) * $(current)) + ($(ExpDecay) * $(current2));
        $(current) *= $(ExpDecay);
        """)

stdp_model = genn_model.create_custom_weight_update_class(
    "stdp",
Beispiel #5
0
import matplotlib.pyplot as plt

from pygenn import genn_model, genn_wrapper
from pygenn.genn_wrapper.Models import VarAccess_READ_ONLY
from time import perf_counter

from common import (izhikevich_dopamine_model, izhikevich_stdp_model,
                    build_model, get_params, plot, convert_spikes)

# ----------------------------------------------------------------------------
# Custom models
# ----------------------------------------------------------------------------
stim_noise_model = genn_model.create_custom_current_source_class(
    "stim_noise",
    param_names=["n"],
    var_name_types=[("iExt", "scalar", VarAccess_READ_ONLY)],
    injection_code="""
        $(injectCurrent, $(iExt) + ($(gennrand_uniform) * $(n) * 2.0) - $(n));
        """)

# ----------------------------------------------------------------------------
# Stimuli generation
# ----------------------------------------------------------------------------
# Get standard model parameters
params = get_params(build_model=True,
                    measure_timing=False,
                    use_genn_recording=False)
params["seed"] = 1234
if "seed" in params:
    np.random.seed(params["seed"])
Beispiel #6
0
    def convert(self, tf_model, scaled_tf_weights=None):
        supported_layers = (tf.keras.layers.Dense, tf.keras.layers.Flatten,
                            tf.keras.layers.Conv2D,
                            tf.keras.layers.AveragePooling2D,
                            tf.keras.layers.Dropout)

        # Check model compatibility
        if not isinstance(tf_model, tf.keras.models.Sequential):
            raise NotImplementedError(
                'Implementation for type {} models not found'.format(
                    type(tf_model)))

        for layer in tf_model.layers[:-1]:
            if not isinstance(layer, supported_layers):
                raise NotImplementedError(
                    '{} layers are not supported'.format(layer))
            elif isinstance(layer, tf.keras.layers.Dense):
                if layer.activation != tf.keras.activations.relu:
                    print(layer.activation)
                    raise NotImplementedError(
                        'Only ReLU activation function is supported')
                if layer.use_bias == True:
                    raise NotImplementedError(
                        'TensorFlow model should be trained without bias tensors'
                    )

        # create custom classes
        if_model = genn_model.create_custom_neuron_class(
            "if_model",
            param_names=["Vres", "Vthr", "Cm"],
            var_name_types=[("Vmem", "scalar"),
                            ("SpikeNumber", "unsigned int")],
            sim_code="""
            $(Vmem) += $(Isyn)*(DT / $(Cm));
            """,
            reset_code="""
            $(Vmem) = $(Vres); 
            $(SpikeNumber) += 1;
            """,
            threshold_condition_code="$(Vmem) >= $(Vthr)")

        cs_model = genn_model.create_custom_current_source_class(
            "cs_model",
            var_name_types=[("magnitude", "scalar")],
            injection_code="""
            $(injectCurrent, $(magnitude));
            """)

        # Params and init
        dense_if_params = {
            "Vres": self.Vres,
            "Vthr": self.Vthr,
            "Cm": self.dCm
        }

        sparse_if_params = {
            "Vres": self.Vres,
            "Vthr": self.Vthr,
            "Cm": self.sCm
        }

        if_init = {
            "Vmem":
            genn_model.init_var("Uniform", {
                "min": self.Vres,
                "max": self.Vthr
            }),
            "SpikeNumber":
            0
        }

        cs_init = {"magnitude": 10.0}

        # Fetch augmented weight matrices
        gw_inds, gw_vals, n_units = self.create_weight_matrices(
            tf_model, scaled_tf_weights)

        # Define model and populations
        self.g_model = genn_model.GeNNModel("float", "g_model")
        self.neuron_pops = []
        self.syn_pops = []

        for i, (inds, vals, n) in enumerate(zip(gw_inds, gw_vals, n_units),
                                            start=1):
            if i == 1:
                # Presynaptic neuron
                self.neuron_pops.append(
                    self.g_model.add_neuron_population("if" + str(i - 1),
                                                       n_units[i - 1],
                                                       if_model,
                                                       sparse_if_params,
                                                       if_init))

            if inds is None:
                # Postsynaptic neuron
                self.neuron_pops.append(
                    self.g_model.add_neuron_population("if" + str(i),
                                                       n_units[i], if_model,
                                                       dense_if_params,
                                                       if_init))

                # Synapse
                self.syn_pops.append(
                    self.g_model.add_synapse_population(
                        "syn" + str(i - 1) + str(i), "DENSE_INDIVIDUALG",
                        genn_wrapper.NO_DELAY, self.neuron_pops[-2],
                        self.neuron_pops[-1], "StaticPulse", {}, {'g': vals},
                        {}, {}, "DeltaCurr", {}, {}))

            else:
                self.neuron_pops.append(
                    self.g_model.add_neuron_population("if" + str(i),
                                                       n_units[i], if_model,
                                                       sparse_if_params,
                                                       if_init))

                self.syn_pops.append(
                    self.g_model.add_synapse_population(
                        "syn" + str(i - 1) + str(i), "SPARSE_INDIVIDUALG",
                        genn_wrapper.NO_DELAY, self.neuron_pops[-2],
                        self.neuron_pops[-1], "StaticPulse", {}, {'g': vals},
                        {}, {}, "DeltaCurr", {}, {}))

                self.syn_pops[-1].set_sparse_connections(inds[0], inds[1])

        self.current_source = self.g_model.add_current_source(
            "cs", cs_model, "if0", {}, cs_init)

        self.g_model.dT = self.timestep
        self.g_model.build()
        self.g_model.load()

        return self.g_model, self.neuron_pops, self.current_source