|
| 1 | +//! Spiking Neural Network for ultra-low-power event detection |
| 2 | +//! |
| 3 | +//! Implements event-driven neural computation for: |
| 4 | +//! - Wake word detection |
| 5 | +//! - Context switching detection |
| 6 | +//! - Proactive assistance triggers |
| 7 | +//! |
| 8 | +//! SNNs use discrete spikes instead of continuous activations, |
| 9 | +//! enabling very low power consumption on appropriate hardware. |
| 10 | +
|
| 11 | +#![forbid(unsafe_code)] |
| 12 | + |
| 13 | +use serde::{Deserialize, Serialize}; |
| 14 | + |
| 15 | +/// Leaky Integrate-and-Fire neuron model |
| 16 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 17 | +pub struct LIFNeuron { |
| 18 | + /// Membrane potential |
| 19 | + pub potential: f32, |
| 20 | + /// Resting potential |
| 21 | + pub rest_potential: f32, |
| 22 | + /// Threshold for firing |
| 23 | + pub threshold: f32, |
| 24 | + /// Membrane time constant |
| 25 | + pub tau: f32, |
| 26 | + /// Refractory period counter |
| 27 | + pub refractory: u32, |
| 28 | +} |
| 29 | + |
| 30 | +impl LIFNeuron { |
| 31 | + /// Create a new LIF neuron |
| 32 | + pub fn new(threshold: f32, tau: f32) -> Self { |
| 33 | + Self { |
| 34 | + potential: 0.0, |
| 35 | + rest_potential: 0.0, |
| 36 | + threshold, |
| 37 | + tau, |
| 38 | + refractory: 0, |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /// Update neuron state and check for spike |
| 43 | + /// |
| 44 | + /// # Arguments |
| 45 | + /// |
| 46 | + /// * `input_current` - Incoming current from synapses |
| 47 | + /// * `dt` - Time step (typically 1ms) |
| 48 | + /// |
| 49 | + /// # Returns |
| 50 | + /// |
| 51 | + /// `true` if neuron spiked, `false` otherwise |
| 52 | + pub fn update(&mut self, input_current: f32, dt: f32) -> bool { |
| 53 | + // Refractory period |
| 54 | + if self.refractory > 0 { |
| 55 | + self.refractory -= 1; |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + // Leaky integration: dV/dt = -(V - V_rest)/tau + I |
| 60 | + let dv = (-(self.potential - self.rest_potential) / self.tau + input_current) * dt; |
| 61 | + self.potential += dv; |
| 62 | + |
| 63 | + // Check for spike |
| 64 | + if self.potential >= self.threshold { |
| 65 | + self.potential = self.rest_potential; |
| 66 | + self.refractory = 5; // 5ms refractory period |
| 67 | + true |
| 68 | + } else { |
| 69 | + false |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /// Reset neuron to resting state |
| 74 | + pub fn reset(&mut self) { |
| 75 | + self.potential = self.rest_potential; |
| 76 | + self.refractory = 0; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// Simple Spiking Neural Network |
| 81 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 82 | +pub struct SpikingNetwork { |
| 83 | + /// Input layer neurons |
| 84 | + input_neurons: Vec<LIFNeuron>, |
| 85 | + /// Hidden layer neurons |
| 86 | + hidden_neurons: Vec<LIFNeuron>, |
| 87 | + /// Output layer neurons |
| 88 | + output_neurons: Vec<LIFNeuron>, |
| 89 | + /// Synaptic weights (input → hidden) |
| 90 | + weights_ih: Vec<Vec<f32>>, |
| 91 | + /// Synaptic weights (hidden → output) |
| 92 | + weights_ho: Vec<Vec<f32>>, |
| 93 | + /// Spike history (for analysis) |
| 94 | + spike_counts: Vec<usize>, |
| 95 | +} |
| 96 | + |
| 97 | +impl SpikingNetwork { |
| 98 | + /// Create a new spiking neural network |
| 99 | + /// |
| 100 | + /// # Arguments |
| 101 | + /// |
| 102 | + /// * `n_input` - Number of input neurons |
| 103 | + /// * `n_hidden` - Number of hidden neurons |
| 104 | + /// * `n_output` - Number of output neurons |
| 105 | + pub fn new(n_input: usize, n_hidden: usize, n_output: usize) -> Self { |
| 106 | + let input_neurons = (0..n_input) |
| 107 | + .map(|_| LIFNeuron::new(1.0, 10.0)) |
| 108 | + .collect(); |
| 109 | + |
| 110 | + let hidden_neurons = (0..n_hidden) |
| 111 | + .map(|_| LIFNeuron::new(1.0, 10.0)) |
| 112 | + .collect(); |
| 113 | + |
| 114 | + let output_neurons = (0..n_output) |
| 115 | + .map(|_| LIFNeuron::new(1.0, 10.0)) |
| 116 | + .collect(); |
| 117 | + |
| 118 | + // Random sparse weights |
| 119 | + let mut seed = 789u64; |
| 120 | + let mut weights_ih = vec![vec![0.0; n_input]; n_hidden]; |
| 121 | + for row in &mut weights_ih { |
| 122 | + for w in row { |
| 123 | + seed = seed.wrapping_mul(1103515245).wrapping_add(12345); |
| 124 | + let rand = ((seed / 65536) % 32768) as f32 / 32768.0; |
| 125 | + if rand < 0.2 { |
| 126 | + // 20% connectivity |
| 127 | + *w = (rand - 0.5) * 0.5; |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + let mut weights_ho = vec![vec![0.0; n_hidden]; n_output]; |
| 133 | + for row in &mut weights_ho { |
| 134 | + for w in row { |
| 135 | + seed = seed.wrapping_mul(1103515245).wrapping_add(12345); |
| 136 | + let rand = ((seed / 65536) % 32768) as f32 / 32768.0; |
| 137 | + if rand < 0.2 { |
| 138 | + *w = (rand - 0.5) * 0.5; |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + Self { |
| 144 | + input_neurons, |
| 145 | + hidden_neurons, |
| 146 | + output_neurons, |
| 147 | + weights_ih, |
| 148 | + weights_ho, |
| 149 | + spike_counts: vec![0; n_output], |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /// Process one time step |
| 154 | + /// |
| 155 | + /// # Arguments |
| 156 | + /// |
| 157 | + /// * `input_spikes` - Binary array indicating which input neurons spike |
| 158 | + /// * `dt` - Time step (typically 1ms) |
| 159 | + /// |
| 160 | + /// # Returns |
| 161 | + /// |
| 162 | + /// Vector of output spike indicators |
| 163 | + pub fn step(&mut self, input_spikes: &[bool], dt: f32) -> Vec<bool> { |
| 164 | + assert_eq!(input_spikes.len(), self.input_neurons.len()); |
| 165 | + |
| 166 | + // Update input layer |
| 167 | + for (i, neuron) in self.input_neurons.iter_mut().enumerate() { |
| 168 | + if input_spikes[i] { |
| 169 | + neuron.update(2.0, dt); |
| 170 | + } else { |
| 171 | + neuron.update(0.0, dt); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + // Compute hidden layer currents |
| 176 | + let mut hidden_currents = vec![0.0; self.hidden_neurons.len()]; |
| 177 | + for (i, neuron) in self.input_neurons.iter().enumerate() { |
| 178 | + if neuron.potential > 0.5 { |
| 179 | + // Approximate spike |
| 180 | + for (h, current) in hidden_currents.iter_mut().enumerate() { |
| 181 | + *current += self.weights_ih[h][i]; |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + // Update hidden layer |
| 187 | + for (neuron, ¤t) in self.hidden_neurons.iter_mut().zip(&hidden_currents) { |
| 188 | + neuron.update(current, dt); |
| 189 | + } |
| 190 | + |
| 191 | + // Compute output layer currents |
| 192 | + let mut output_currents = vec![0.0; self.output_neurons.len()]; |
| 193 | + for (h, neuron) in self.hidden_neurons.iter().enumerate() { |
| 194 | + if neuron.potential > 0.5 { |
| 195 | + for (o, current) in output_currents.iter_mut().enumerate() { |
| 196 | + *current += self.weights_ho[o][h]; |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + // Update output layer |
| 202 | + let mut output_spikes = vec![false; self.output_neurons.len()]; |
| 203 | + for (i, (neuron, ¤t)) in self |
| 204 | + .output_neurons |
| 205 | + .iter_mut() |
| 206 | + .zip(&output_currents) |
| 207 | + .enumerate() |
| 208 | + { |
| 209 | + if neuron.update(current, dt) { |
| 210 | + output_spikes[i] = true; |
| 211 | + self.spike_counts[i] += 1; |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + output_spikes |
| 216 | + } |
| 217 | + |
| 218 | + /// Reset all neurons |
| 219 | + pub fn reset(&mut self) { |
| 220 | + for neuron in &mut self.input_neurons { |
| 221 | + neuron.reset(); |
| 222 | + } |
| 223 | + for neuron in &mut self.hidden_neurons { |
| 224 | + neuron.reset(); |
| 225 | + } |
| 226 | + for neuron in &mut self.output_neurons { |
| 227 | + neuron.reset(); |
| 228 | + } |
| 229 | + self.spike_counts.fill(0); |
| 230 | + } |
| 231 | + |
| 232 | + /// Get spike counts for output neurons |
| 233 | + pub fn spike_counts(&self) -> &[usize] { |
| 234 | + &self.spike_counts |
| 235 | + } |
| 236 | +} |
| 237 | + |
| 238 | +#[cfg(test)] |
| 239 | +mod tests { |
| 240 | + use super::*; |
| 241 | + |
| 242 | + #[test] |
| 243 | + fn test_lif_neuron_creation() { |
| 244 | + let neuron = LIFNeuron::new(1.0, 10.0); |
| 245 | + assert_eq!(neuron.potential, 0.0); |
| 246 | + assert_eq!(neuron.threshold, 1.0); |
| 247 | + } |
| 248 | + |
| 249 | + #[test] |
| 250 | + fn test_lif_neuron_spike() { |
| 251 | + let mut neuron = LIFNeuron::new(1.0, 10.0); |
| 252 | + |
| 253 | + // Strong input should cause spike |
| 254 | + let spiked = neuron.update(5.0, 1.0); |
| 255 | + assert!(spiked || neuron.potential > 0.8); |
| 256 | + } |
| 257 | + |
| 258 | + #[test] |
| 259 | + fn test_lif_neuron_refractory() { |
| 260 | + let mut neuron = LIFNeuron::new(0.5, 10.0); |
| 261 | + |
| 262 | + // Cause a spike |
| 263 | + neuron.update(10.0, 1.0); |
| 264 | + |
| 265 | + // During refractory period, no spike |
| 266 | + let spiked = neuron.update(10.0, 1.0); |
| 267 | + assert!(!spiked); |
| 268 | + } |
| 269 | + |
| 270 | + #[test] |
| 271 | + fn test_lif_neuron_reset() { |
| 272 | + let mut neuron = LIFNeuron::new(1.0, 10.0); |
| 273 | + neuron.update(2.0, 1.0); |
| 274 | + assert!(neuron.potential != 0.0); |
| 275 | + |
| 276 | + neuron.reset(); |
| 277 | + assert_eq!(neuron.potential, 0.0); |
| 278 | + } |
| 279 | + |
| 280 | + #[test] |
| 281 | + fn test_spiking_network_creation() { |
| 282 | + let snn = SpikingNetwork::new(10, 20, 3); |
| 283 | + assert_eq!(snn.input_neurons.len(), 10); |
| 284 | + assert_eq!(snn.hidden_neurons.len(), 20); |
| 285 | + assert_eq!(snn.output_neurons.len(), 3); |
| 286 | + } |
| 287 | + |
| 288 | + #[test] |
| 289 | + fn test_spiking_network_step() { |
| 290 | + let mut snn = SpikingNetwork::new(10, 20, 3); |
| 291 | + let input = vec![true, false, true, false, false, false, false, false, false, false]; |
| 292 | + |
| 293 | + let output = snn.step(&input, 1.0); |
| 294 | + assert_eq!(output.len(), 3); |
| 295 | + } |
| 296 | + |
| 297 | + #[test] |
| 298 | + fn test_spiking_network_reset() { |
| 299 | + let mut snn = SpikingNetwork::new(10, 20, 3); |
| 300 | + let input = vec![true; 10]; |
| 301 | + |
| 302 | + // Run for a few steps |
| 303 | + for _ in 0..10 { |
| 304 | + snn.step(&input, 1.0); |
| 305 | + } |
| 306 | + |
| 307 | + assert!(snn.spike_counts().iter().any(|&c| c > 0)); |
| 308 | + |
| 309 | + snn.reset(); |
| 310 | + assert!(snn.spike_counts().iter().all(|&c| c == 0)); |
| 311 | + } |
| 312 | + |
| 313 | + #[test] |
| 314 | + fn test_spiking_network_serialization() { |
| 315 | + let snn = SpikingNetwork::new(10, 20, 3); |
| 316 | + let json = serde_json::to_string(&snn).unwrap(); |
| 317 | + let _deserialized: SpikingNetwork = serde_json::from_str(&json).unwrap(); |
| 318 | + } |
| 319 | +} |
0 commit comments