Esempio n. 1
0
TARGET_NUM = OUTPUT_NEURON_NUM
NON_TARGET_NUM = OUTPUT_NEURON_NUM * (NUM_CLASSES - 1)

SPIKE_RATE_DIV = PRESENT_TIMESTEPS / 1000

# Values for initialisation of parameters in different models
if_init = {"V": 0.0}
poisson_init = {"rate": 1.0}

# g_init = np.random.choice(2, (NUM_INPUT, NUM_CLASSES * OUTPUT_NEURON_NUM))

fusi_init = {
    "X":
    init_var("Uniform", {
        "min": FUSI_PARAMS["Xmin"],
        "max": FUSI_PARAMS["Xmax"]
    }),
    "last_tpre":
    0.0,
    "g":
    0.0
}
fusi_post_init = {"C": 2.0}

TEACHER_STRENGTH = 1.0
TEACHER_UNSTIM_STRENGTH = 0.0

INH_V = 50
INPUT_UNSTIM = 2
INPUT_STIM = 50
import numpy as np
import matplotlib.pyplot as plt
from pygenn import genn_wrapper, genn_model

model = genn_model.GeNNModel("float", "tutorial2_pygenn")
model.dT = 1.0

izk_params = {"a": 0.02, "b": 0.2, "c": -65.0, "d": 8.0}
izk_init = {"V": -65.0,
            "U": genn_model.init_var("Uniform", {"min": 0.0, "max": 20.0})}

stim_params = {"amp": 4.0}

exc_syn_init = {"g": 0.05}
inh_syn_init = {"g": -0.25}

fixed_prob = {"prob": 0.1}

exc = model.add_neuron_population("Exc", 8000, "Izhikevich", izk_params, izk_init)
inh = model.add_neuron_population("Inh", 2000, "Izhikevich", izk_params, izk_init)

model.add_current_source("ExcStim", "DC", "Exc", stim_params, {})
model.add_current_source("InhStim", "DC", "Inh", stim_params, {})

model.add_synapse_population("Exc_Exc", "SPARSE_GLOBALG", genn_wrapper.NO_DELAY,
    exc, exc,
    "StaticPulse", {}, exc_syn_init, {}, {},
    "DeltaCurr", {}, {},
    genn_model.init_connectivity("FixedProbabilityNoAutapse", fixed_prob))

model.add_synapse_population("Exc_Inh", "SPARSE_GLOBALG", genn_wrapper.NO_DELAY,
Esempio n. 3
0
    "Vreset": v_reset_e,
    "Vthres": v_thres_e,
    "RefracPeriod": refrac_period_e,
    "tauTheta": tau_theta_e
}
lif_i_params = {
    "Tau": tau_i,
    "Erest": e_rest_i,
    "Vreset": v_reset_i,
    "Vthres": v_thres_i,
    "RefracPeriod": refrac_period_i
}

lif_e_init = {
    "V": genn_model.init_var("Uniform", {
        "min": v_reset_e,
        "max": v_thres_e
    }),
    "RefracTime": 0.0,
    "SpikeNumber": 0,
    "theta": 0.0
}
lif_i_init = {
    "V": genn_model.init_var("Uniform", {
        "min": v_reset_i,
        "max": v_thres_i
    }),
    "RefracTime": 0.0,
    "SpikeNumber": 0
}

stdp_init = {
                 "Vthresh": -50.0, "tauRefrac": 5.0, "tauRise": TAU_RISE_MS, 
                 "tauDecay": TAU_DECAY_MS, "tauAvgErr": TAU_AVG_ERR_MS}
output_init_vars = {"V": -60.0, "refracTime": 0.0, "errRise": 0.0, 
                    "errTilda": 0.0, "errDecay": 0.0, "avgSqrErr": 0.0,
                    "startSpike": target_neuron_start_times, "endSpike": target_neuron_end_times}

# ----------------------------------------------------------------------------
# Synapse initialisation
# ----------------------------------------------------------------------------
superspike_params = {"tauRise": TAU_RISE_MS, "tauDecay": TAU_DECAY_MS, "beta": 1000.0}
superspike_pre_init_vars = {"z": 0.0, "zTilda": 0.0}
superspike_post_init_vars = {"sigmaPrime": 0.0}

input_hidden_weight_dist_params = {"mean": 0.0, "sd": W0 / np.sqrt(float(NUM_INPUT)),
                                   "min": W_MIN, "max": W_MAX}
input_hidden_init_vars = {"w": genn_model.init_var("NormalClipped", input_hidden_weight_dist_params),
                          "e": 0.0, "lambda": 0.0, "m": 0.0}

hidden_output_weight_dist_params = {"mean": 0.0, "sd": W0 / np.sqrt(float(NUM_HIDDEN)),
                                    "min": W_MIN, "max": W_MAX}
hidden_output_init_vars = {"w": genn_model.init_var("NormalClipped", hidden_output_weight_dist_params),
                           "e": 0.0, "lambda": 0.0, "m": 0.0}     
       
# ----------------------------------------------------------------------------
# Custom update initialisation
# ----------------------------------------------------------------------------
r_max_prop_params = {"updateTime": UPDATE_TIME_MS, "tauRMS": TAU_RMS_MS, 
                     "epsilon": EPSILON, "wMin": W_MIN, "wMax": W_MAX}

# ----------------------------------------------------------------------------
# Model description
Esempio n. 5
0
         create_dpf_class(lambda pars, dt: exp(-pars[-1] / pars[2]))())
    ])

SUPERSPIKE_PARAMS = {
    "t_rise": t_rise,
    "t_decay": t_decay,
    "tau_rms": tau_rms,
    "wmax": wmax,
    "wmin": wmin,
    "epsilon": epsilon,
    "update_t": update_time_ms
}

superspike_init = {
    "w": init_var("Uniform", {
        "min": -0.001,
        "max": 0.001
    }),
    "e": 0.0,
    "lambda": 0.0,
    "upsilon": 0.0,
    "m": 0.0,
    "r0": r0
}
""" Feedback weights model for out2hid connections """

feedback_wts_model = create_custom_weight_update_class(
    "feedback",
    var_name_types=[("g", "scalar")],
    synapse_dynamics_code="$(addToInSyn, $(g) * $(err_tilda_pre));")

feedback_wts_init = {"g": init_var("Uniform", {"min": -0.001, "max": 0.001})}
Esempio n. 6
0
    var_name_types=[("magnitude", "scalar")],
    injection_code="$(injectCurrent, $(magnitude));")

# ----------------------------------------------------------------------------
# Build model
# ----------------------------------------------------------------------------
# Create GeNN model
model = GeNNModel("float", "tutorial_1")
model.dT = TIMESTEP

# Initial values for initialisation
if_init = {"V": 0.0, "SpikeCount": 0}
stdp_init = {
    "g":
    init_var("Uniform", {
        "min": STDP_PARAMS["gmin"],
        "max": STDP_PARAMS["gmax"]
    })
}

neurons_count = [784, 128, NUM_CLASSES]
neuron_layers = []

# Create neuron layers
for i in range(len(neurons_count)):
    neuron_layers.append(
        model.add_neuron_population("neuron%u" % (i), neurons_count[i],
                                    if_model, IF_PARAMS, if_init))

weights_0_1 = np.load("weights_0_1.npy")

synapses = []
    "Vrest": -74.0,
    "Vreset": -60.0,
    "Vthresh": -54.0,
    "Ioffset": 0.0,
    "TauRefrac": 1.0
}

lif_init = {"V": -60.0, "RefracTime": 0.0}

poisson_params = {"rate": 15.0}

poisson_init = {"timeStepToSpike": 0.0}

post_syn_params = {"tau": 5.0}

stdp_init = {"g": genn_model.init_var("Uniform", {"min": 0.0, "max": G_MAX})}
stdp_params = {
    "tauPlus": 20.0,
    "tauMinus": 20.0,
    "aPlus": A_PLUS,
    "aMinus": A_MINUS,
    "wMin": 0.0,
    "wMax": G_MAX
}
stdp_pre_init = {"preTrace": 0.0}
stdp_post_init = {"postTrace": 0.0}

# Create neuron populations
additive_pop = model.add_neuron_population("additive", 1, "LIF", lif_params,
                                           lif_init)
multiplicative_pop = model.add_neuron_population("multiplicative", 1, "LIF",
Esempio n. 8
0
    def compile(self, mlg_model, name):
        conv_kh, conv_kw = self.conv_size
        conv_sh, conv_sw = self.conv_strides
        conv_ih, conv_iw, conv_ic = self.source().shape
        conv_oh, conv_ow, conv_oc = self.target().shape
        if self.conv_padding == PadMode.VALID:
            conv_padh = 0
            conv_padw = 0
        elif self.conv_padding == PadMode.SAME:
            conv_padh = _get_conv_same_padding(conv_ih, conv_kh, conv_sh)
            conv_padw = _get_conv_same_padding(conv_iw, conv_kw, conv_sw)

        wu_model = signed_static_pulse if self.source(
        ).neurons.signed_spikes else 'StaticPulse'

        if (self.connectivity_type == ConnectivityType.TOEPLITZ
                and conv_sh == 1 and conv_sw == 1):
            conn_init = init_toeplitz_connectivity(
                'Conv2D', {
                    'conv_kh': conv_kh,
                    'conv_kw': conv_kw,
                    'conv_ih': conv_ih,
                    'conv_iw': conv_iw,
                    'conv_ic': conv_ic,
                    'conv_oh': conv_oh,
                    'conv_ow': conv_ow,
                    'conv_oc': conv_oc
                })
            conn = 'TOEPLITZ_KERNELG'
            wu_var = {'g': self.weights.flatten()}
            wu_var_egp = {}
        else:
            conn_init = init_connectivity(
                'Conv2D', {
                    'conv_kh': conv_kh,
                    'conv_kw': conv_kw,
                    'conv_sh': conv_sh,
                    'conv_sw': conv_sw,
                    'conv_padh': conv_padh,
                    'conv_padw': conv_padw,
                    'conv_ih': conv_ih,
                    'conv_iw': conv_iw,
                    'conv_ic': conv_ic,
                    'conv_oh': conv_oh,
                    'conv_ow': conv_ow,
                    'conv_oc': conv_oc
                })

            if self.connectivity_type == ConnectivityType.SPARSE:
                conn = 'SPARSE_INDIVIDUALG'
                wu_var = {'g': init_var('Kernel', {})}
                wu_var_egp = {'g': {'kernel': self.weights.flatten()}}
            else:
                if self.connectivity_type == ConnectivityType.TOEPLITZ:
                    print(
                        "WARNING: falling back to procedural connectivity "
                        "for Conv2D connectivity with unsupported parameters")

                conn = 'PROCEDURAL_KERNELG'
                wu_var = {'g': self.weights.flatten()}
                wu_var_egp = {}

        super(Conv2DSynapses,
              self).compile(mlg_model, name, conn, wu_model, {}, wu_var, {},
                            {}, 'DeltaCurr', {}, {}, conn_init, wu_var_egp)
Esempio n. 9
0
fixed_prob = {"prob": PROBABILITY_CONNECTION}

lif_params = {
    "C": 1.0,
    "TauM": 20.0,
    "Vrest": -49.0,
    "Vreset": RESET_VOLTAGE,
    "Vthresh": THRESHOHOLD_VOLTAGE,
    "Ioffset": 0.0,
    "TauRefrac": 5.0
}

lif_init = {
    "V":
    genn_model.init_var("Uniform", {
        "min": RESET_VOLTAGE,
        "max": THRESHOHOLD_VOLTAGE
    }),
    "RefracTime":
    0.0
}

excitatory_synapse_init = {"g": EXCITATORY_WEIGHT}
inhibitory_synapse_init = {"g": INHIBITORY_WEIGHT}

excitatory_post_syn_params = {"tau": 5.0}
inhibitory_post_syn_params = {"tau": 10.0}

excitatory_pop = model.add_neuron_population("E", NUM_EXCITATORY, "LIF",
                                             lif_params, lif_init)
inhibitory_pop = model.add_neuron_population("I", NUM_INHIBITORY, "LIF",
                                             lif_params, lif_init)
Esempio n. 10
0
num_examples = training_images.shape[0]
layer_sizes = [784, 1500, 1500, 1500, 1000]
single_example_time = 5
train_timesteps = num_examples * single_example_time
max_input = 0.95

# Neuron group parameters
hidden_params = {"Tau": 0.95}
output_params = {"Tau": 0.95, "NeuronsPerLabel": 100}

# Neuron group initial values
output_hidden_init = {"V": 0.0, "U": 0.0, "PhiF": 0.0, "PsiH": 0.0}

# Weight update
forward_params = {"nu": 1E-03}
forward_init = {"g": genn_model.init_var("Uniform", {"min": -0.05, "max": 0.05})}
backward_init = {"g": genn_model.init_var("Uniform", {"min": -0.1, "max": 0.1})}

# ********************************************************************************
#                      Model Instances
# ********************************************************************************
model = genn_model.GeNNModel("float","mnist")
model.dT = dt
model.timing_enabled = True

# Add input layer (layer 0)
layers = [model.add_neuron_population("input_layer", layer_sizes[0], input_model, 
                                      {}, {})]

# Add hidden layers
for i, size in enumerate(layer_sizes[1:-1]):

# ----------------------------------------------------------------------------
# Network creation
# ----------------------------------------------------------------------------
model = genn_model.GeNNModel("float", "potjans_microcircuit")
model.dT = DT_MS
model._model.set_merge_postsynaptic_models(True)
model._model.set_default_narrow_sparse_ind_enabled(True)
model.timing_enabled = MEASURE_TIMING
model.default_var_location = genn_wrapper.VarLocation_DEVICE
model.default_sparse_connectivity_location = genn_wrapper.VarLocation_DEVICE

lif_init = {
    "V": genn_model.init_var("Normal", {
        "mean": -58.0,
        "sd": 5.0
    }),
    "RefracTime": 0.0
}
poisson_init = {"current": 0.0}

exp_curr_params = {"tau": 0.5}

quantile = 0.9999
normal_quantile_cdf = norm.ppf(quantile)
max_delay = {
    pop: MEAN_DELAY[pop] + (DELAY_SD[pop] * normal_quantile_cdf)
    for pop in POPULATION_NAMES
}
print("Max excitatory delay:%fms , max inhibitory delay:%fms" %
      (max_delay["E"], max_delay["I"]))
Esempio n. 12
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
Esempio n. 13
0
        const scalar dt = $(t) - $(sT_post);
        $(postTrace) = $(postTrace) * exp(-dt / $(tauMinus)) + 1.0;
        """,
    is_pre_spike_time_required=True,
    is_post_spike_time_required=True)

# ----------------------------------------------------------------------------
# Build model
# ----------------------------------------------------------------------------
# Create GeNN model
model = GeNNModel("float", "tutorial_1")
model.dT = TIMESTEP

post_syn_params = {"tau": 5.0}

stdp_init = {"g": init_var("Uniform", {"min": 0.0, "max": G_MAX})}
stdp_params = {
    "tauPlus": 5.0,
    "tauMinus": 20.0,
    "aPlus": A_PLUS,
    "aMinus": A_MINUS,
    "wMin": 0.0,
    "wMax": G_MAX
}
stdp_pre_init = {"preTrace": 0.0}
stdp_post_init = {"postTrace": 0.0}

# Load weights
weights = []
while True:
    filename = "weights_%u_%u.npy" % (len(weights), len(weights) + 1)
Esempio n. 14
0
# Neuron group initial values
lif_e_init = {
    "V": v_rest_e - 40.0,
    "RefracTime": 0.0,
    "SpikeNumber": 0,
    "theta": 10.0
}
lif_i_init = {"V": v_rest_i - 40.0, "RefracTime": 0.0}

poisson_init = {"timeStepToSpike": 0.0, "frequency": 0.0}

post_syn_e_params = {"tau": 1.0, "E": 0.0}
post_syn_i_params = {"tau": 2.0, "E": -100.0}

stdp_init = {"g": genn_model.init_var("Uniform", {"min": 0.003, "max": 0.3})}
stdp_params = {
    "tauMinus": 20.0,
    "gMax": 1.0,
    "Xtar": 0.4,
    "mu": 0.2,
    "eta": 0.0001
}
stdp_pre_init = {"Xpre": 0.0}

static_e_init = {"g": 17.0}
static_i_init = {
    "g": genn_model.init_var(lateral_inhibition, {"weight": -10.0})
}

# ********************************************************************************