This repository implements a biologically-inspired Ring Attractor Neural Network that performs angular velocity integration, mimicking how certain brain circuits (like head direction cells) track angular position.
Watch how the network learns three essential circuits:
- W₀ (Self-Maintaining): Keeps the activity bump stable when the animal is stationary, maintaining the current heading direction without any sensory or motor input
- W_L (Left Turn): Activated by left turn motor signals, rotates the bump counterclockwise to track leftward movement
- W_R (Right Turn): Activated by right turn motor signals, rotates the bump clockwise to track rightward movement
The Ring Attractor Network maintains a localized "bump" of neural activity on a ring of neurons, where the position of this bump represents an angular value (0-2π). By integrating angular velocity inputs over time, the network learns to rotate this bump to track the true angular position.
The network follows the leaky integrator dynamics:
r += (-r + F(J0·r + J1*Wo·r + L*Wl·r + R*Wr·r)) * dt/tau
Where:
r: Neural activity vector (ring of neurons)J0: Global inhibition matrixWo: Maintenance weights (preserves bump shape)Wl,Wr: Left/right rotation weight matricesL,R: Rotation signals derived from angular velocityF(): Activation function (tanh, ReLU, or GELU)
- Ring Topology: Neurons arranged in a circle, each with a preferred direction (0-2π)
- Activity Bump: Localized peak of activity representing current angle
- Rotation Mechanism: Asymmetric connections shift the bump based on velocity
- Gain Networks: Small MLPs that dynamically modulate rotation strength
-
generate_av_integration_data.py: Efficient data generation for training/testing- Creates synthetic angular velocity signals
- Integrates velocities to compute ground truth angles
- Supports batched, vectorized generation on GPU
-
train_ring_attractor.py: Main model and training scriptLeakyRingAttractor: Core neural network model- Training loop with cosine similarity loss
- Evaluation and visualization functions
python train_ring_attractor.pyThis will:
- Generate training data on-the-fly (angular velocity and targeted integrated angle)
- Train the ring attractor network (take velocity input, and output estimated angle)
- Visualize weight matrices (begining and end) and performance
or
python train_ring_attractor_save_movie.pyAdditionally, this will: Save the plot of weights of each 10 training steps (epoch) and compile them into a weight evolution movie (requires ffmpeg)
from generate_av_integration_data import AVIntegrationDataset
# Create dataset
dataset = AVIntegrationDataset(
num_samples=10000,
seq_len=120,
zero_padding_start_ratio=0.1,
zero_ratios_in_rest=[0.2, 0.5, 0.8],
max_av=0.1 * np.pi,
device='cuda'
)
# Generate batch
av_signals, angles = dataset.generate_batch(batch_size=128)- Random: Random weight initialization (default for training)
- Perfect: Ideal ring attractor weights (also can learn from there)
- Debug Perfect: Fixed ideal weights with no learning (for testing)
num_neurons: Number of neurons in the ring (default: 120)tau: Time constant for neural dynamicsdt: Integration time stepalpha: usually tau/dt, but can be set internally by user to decide how smooth neural dynamics are: r = r * (1 - alpha) + recurrent_input * alphaactivation: Activation function ('tanh', 'relu', 'gelu')hidden_gain_neurons: Hidden units in gain modulation networks
- Head Direction Loss: Measures angle prediction loss against ground-truth
- Bump Amplitude Loss: Ensures stable total activity (prevents vanishing/exploding)
Two methods to extract angle from neural activity:
- Population Vector: Weighted circular average of neuron activities
- Argmax: Location of peak activity
The training script generates:
- Weight matrix visualizations (initial and trained)
- A movie of how three weight matrix evolves (weights for bump maintaining, turn left, turn right)
- Performance plots showing:
- Network activity heatmap
- Bump activity profiles
- Input angular velocity
- True vs. decoded angles
This model is inspired by:
- Ring attractor dynamics in fruit fly navigation circuits
- Head direction cells in the mammalian brain
