I'm trying to generate spikes from image for spike neural networks. I do this with rate coding, where the probability of generating a spike corresponds to the pixel value. I test my function with 28x28 pixel image and the execution time was ~ 0.65 sec. If I'll generate spike using MNIST datset, it is gonna take about 10 hours to do, what is quite bad. How can I boost my function?
Here's the code I wrote:
def spike_generator(image_list, total_time, delay_time, frequency, seed=42):
'''
Function generates spike patterns based on the input data array.
Probability of spiking corresponds to the pixel value.
Parameters:
image_list: list of numpy arrays containing input data for each neuron
total_time (float): total duration for spike generation in seconds
delay_time (float): time delay between spikes for each neuron in seconds
frequency (int): frequency of spike generation in Hz
seed (Optional(int), default = 42): a numerical value that
generates a new set or repeats pseudo-random numbers
Returns:
generated_spikes: numpy array containing spike patterns
for each neuron over the specified total_time duration
'''
np.random.seed(seed)
generated_spikes = np.zeros((image_list.shape[0], image_list.shape[1], total_time * frequency))
for z in range(image_list.shape[0]):
data_norm = (image_list[z] - image_list[z].min()) / (image_list[z].max() - image_list[z].min())
for i in range(image_list.shape[1]):
while f <= total_time * frequency:
if np.random.choice([0, 1], p=[1 - data_norm[i], data_norm[i]]) == 1:
generated_spikes[z][i][f] = 1
f = f + mt.ceil(frequency * delay_time)
return generated_spikes
I've tried to use numpy.vectorize, but this didn't reduce execution time.