Skip to content

Instantly share code, notes, and snippets.

@hed0rah
Created April 2, 2026 17:33
Show Gist options
  • Select an option

  • Save hed0rah/a78280d8e248d18209b52467a3c65aa0 to your computer and use it in GitHub Desktop.

Select an option

Save hed0rah/a78280d8e248d18209b52467a3c65aa0 to your computer and use it in GitHub Desktop.
Minimal Spiking Neural Net example
import numpy as np
# --- Parameters ---
num_neurons = 5
timesteps = 100
threshold = 1.0
decay = 0.95
learning_rate = 0.01
# --- State ---
membrane = np.zeros(num_neurons)
spikes = np.zeros(num_neurons)
weights = np.random.rand(num_neurons, num_neurons) * 0.5
# External input (random spikes)
input_signal = (np.random.rand(timesteps, num_neurons) > 0.8).astype(float)
# Spike timing tracker
last_spike_time = np.full(num_neurons, -np.inf)
def stdp(pre, post, t):
"""Simple STDP rule"""
dt = last_spike_time[post] - last_spike_time[pre]
return np.exp(-abs(dt)) * (1 if dt > 0 else -1)
# --- Simulation ---
for t in range(timesteps):
# Input current
input_current = input_signal[t]
# Update membrane potentials
membrane = membrane * decay + input_current + np.dot(weights, spikes)
# Generate spikes
new_spikes = (membrane >= threshold).astype(float)
# Reset spiking neurons
membrane[new_spikes == 1] = 0
# STDP learning
for i in range(num_neurons):
for j in range(num_neurons):
if spikes[i] and new_spikes[j]:
weights[i, j] += learning_rate * stdp(i, j, t)
# Track spike times
for i in range(num_neurons):
if new_spikes[i]:
last_spike_time[i] = t
spikes = new_spikes
print(f"t={t}, spikes={spikes}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment