Float division zero

it runs but i get an error float division zero. Can anyone help please?

from brian2 import *
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error

# Define the LIF equations
eqs_lif = '''
dv/dt = (1/C) * (I - gL * (v - EL + eps)) : volt
I : amp
C : farad
gL : siemens
EL : volt
eps : volt  # Small constant value to prevent division by zero

# Set the parameters for the LIF equations
C = 1*uF
gL = 0.3*mS
EL = -54.4*mV
eps = 1e-9 * volt  # Small constant value to prevent division by zero

# Generate random input data
input_data = np.random.random((1000, 44))
output_data = np.random.random((1000, 1))

# Split the data into training and validation sets
input_train, input_val, output_train, output_val = train_test_split(
    input_data, output_data, test_size=0.2, random_state=1

# Define the Brian model for the neural network
num_inputs = 44
num_hidden1 = 100
num_hidden2 = 50
num_hidden3 = 25
num_output = 1


# Create input and output variables
input_var = TimedArray(input_train * nA, dt=1*ms)
output_var = output_train.squeeze() * volt

# Create the LIF neuron group
neurons = NeuronGroup(num_inputs, eqs_lif, threshold='v > -40*mV', reset='v = EL',
                      namespace={'C': C, 'gL': gL, 'EL': EL})

# Define the network architecture
hidden_layer1 = NeuronGroup(num_hidden1, eqs_lif, threshold='v > -40*mV', reset='v = EL',
                            namespace={'C': C, 'gL': gL, 'EL': EL, 'eps': eps})
hidden_layer2 = NeuronGroup(num_hidden2, eqs_lif, threshold='v > -40*mV', reset='v = EL',
                            namespace={'C': C, 'gL': gL, 'EL': EL, 'eps': eps})
hidden_layer3 = NeuronGroup(num_hidden3, eqs_lif, threshold='v > -40*mV', reset='v = EL',
                            namespace={'C': C, 'gL': gL, 'EL': EL, 'eps': eps})
output_layer = NeuronGroup(num_output, eqs_lif, threshold='v > -40*mV', reset='v = EL',
                           namespace={'C': C, 'gL': gL, 'EL': EL, 'eps': eps})

# Define the connections between the layers
synapse_input_hidden1 = Synapses(neurons, hidden_layer1, 'w : volt', on_pre='v_post += w')
synapse_hidden1_hidden2 = Synapses(hidden_layer1, hidden_layer2, 'w : volt', on_pre='v_post += w')
synapse_hidden2_hidden3 = Synapses(hidden_layer2, hidden_layer3, 'w : volt', on_pre='v_post += w')
synapse_hidden3_output = Synapses(hidden_layer3, output_layer, 'w : volt', on_pre='v_post += w')

# Set the initial weights

# Set the LIF equations for the synapses
synapse_input_hidden1.w = 'j*mV'
synapse_hidden1_hidden2.w = 'j*mV'
synapse_hidden2_hidden3.w = 'j*mV'
synapse_hidden3_output.w = 'j*mV'

# Define the monitors to record variables
mon_neurons = SpikeMonitor(neurons)
mon_output = StateMonitor(output_layer, 'v', record=True)

# Run the simulation
runtime = 1000 * ms

# Extract the output values
output_values = mon_output.v.squeeze()

# Calculate mean absolute error
mae = mean_absolute_error(output_train, output_values)

# Print the mean absolute error
print(f"Epoch 1: MAE = {mae:.4f}")

# Evaluate the model on the validation set
input_var_val = TimedArray(input_val * nA, dt=1*ms)
neurons.I = input_var_val
output_values_val = mon_output.v.squeeze()
val_mae = mean_absolute_error(output_val, output_values_val)

# Print the validation mean absolute error
print(f"Epoch 1: Val MAE = {val_mae:.4f}")

Hi @sakattack85. When you run the code, you get warnings like this:

WARNING    'C' is an internal variable of group 'neurongroup_1', but also exists in the following namespaces: group-specific ,run. The internal variable will be used. [brian2.groups.group.Group.resolve.resolution_conflict]

This means that you have provided C (and EL) via the namespace argument and as external variables, but they are also defined in your model equations as a neuron-specific parameter. As the warning says, it will ignore the external variables and namespace in this case, and use the neuron-specific parameter instead. Since you never set these parameters (by using neurons.C = …), these values are all 0, leading to the division by zero. The easiest solution in your case is to simply not define these variables as part of the equations:

eqs_lif = '''
dv/dt = (1/C) * (I - gL * (v - EL)) : volt
I : amp

In this case, where you call run from the same environment where you define the external variables C, EL, etc., you also do not need to use the namespace argument, Brian will automatically pick up the values from the environment:

neurons = NeuronGroup(num_inputs, eqs_lif, threshold='v > -40*mV', reset='v = EL',)

Also note that your code refers to the TimedArray incorrectly – is this maybe taken from some old code that used Brian 1 (see conversion notes for TimedArray: Inputs (Brian 1 –> 2 conversion) — Brian 2 2.5.1 documentation)? In Brian 2, you refer to the TimedArray directly in the equations, e.g. like this:

input_values = TimedArray(...)
eqs_lif = '''
dv/dt = (1/C) * (input_values(t, i) - gL * (v - EL)) : volt