Free SKILL.md scraped from GitHub. Clone the repo or copy the file directly into your Claude Code skills directory.
npx versuz@latest install hiyenwong-ai-collection-collection-skills-bayesian-speech-snngit clone https://github.com/hiyenwong/ai_collection.gitcp ai_collection/SKILL.MD ~/.claude/skills/hiyenwong-ai-collection-collection-skills-bayesian-speech-snn/SKILL.md---
name: bayesian-speech-snn
description: "Bayesian inference methodology for Spiking Neural Networks in speech processing. Uses IVON (Improved Variational Online Newton) to smooth the angular predictive landscape caused by threshold-based spike generation. Demonstrates improved NLL and Brier scores on Heidelberg Digits and Speech Commands datasets. Activation: bayesian SNN, speech SNN, surrogate gradient, IVON, loss landscape smoothing, uncertainty quantification, variational inference."
---
# Bayesian Inference for Speech Spiking Neural Networks
Methodology for applying Bayesian learning to Spiking Neural Networks (SNNs) to address the irregular predictive landscape caused by threshold-based spike generation, specifically for speech processing tasks.
## Problem Statement
SNNs are naturally suited for speech processing due to their temporal dynamics, but their threshold-based spike generation creates an **angular, irregular predictive landscape** that hampers optimization and generalization.
### Core Challenge
- Deterministic SNN training produces jagged loss landscapes
- Sharp thresholds create non-smooth gradients
- Poor uncertainty quantification in predictions
## Methodology: Bayesian SNN with IVON
### IVON (Improved Variational Online Newton)
Efficient variational Bayesian approach that:
1. **Smooths the predictive landscape** via weight posterior distributions
2. **Provides uncertainty quantification** through predictive variance
3. **Maintains computational efficiency** via online updates
### Key Insight
Bayesian weight distributions average over multiple smooth approximations of the threshold function, producing a globally smoother predictive landscape than deterministic point estimates.
## Implementation
### Step 1: Define Bayesian SNN Architecture
```python
import torch
import torch.nn as nn
from torch.distributions import Normal
class BayesianSNN(nn.Module):
"""Bayesian Spiking Neural Network for speech processing."""
def __init__(self, input_dim, hidden_dim, output_dim, num_steps):
super().__init__()
self.num_steps = num_steps
# Bayesian weights (mean + log-variance)
self.w_mean = nn.Parameter(torch.randn(hidden_dim, input_dim) * 0.1)
self.w_logvar = nn.Parameter(torch.zeros(hidden_dim, input_dim))
self.b_mean = nn.Parameter(torch.zeros(hidden_dim))
self.b_logvar = nn.Parameter(torch.zeros(hidden_dim))
self.w_out_mean = nn.Parameter(torch.randn(output_dim, hidden_dim) * 0.1)
self.w_out_logvar = nn.Parameter(torch.zeros(output_dim, hidden_dim))
# Surrogate gradient function
self.surrogate = torch.nn.Sigmoid()
self.spike_threshold = 1.0
self.membrane_decay = 0.9
def reparameterize(self, mean, logvar):
"""Reparameterization trick for Bayesian sampling."""
std = torch.exp(0.5 * logvar)
eps = torch.randn_like(std)
return mean + eps * std
def forward(self, x, training=True):
"""Forward pass with spiking dynamics."""
batch_size = x.size(0)
hidden = torch.zeros(batch_size, self.hidden_dim)
membrane = torch.zeros(batch_size, self.hidden_dim)
all_spikes = []
for t in range(self.num_steps):
# Sample Bayesian weights during training
if training:
w = self.reparameterize(self.w_mean, self.w_logvar)
b = self.reparameterize(self.b_mean, self.b_logvar)
else:
w = self.w_mean
b = self.b_mean
# Update membrane potential
input_current = torch.matmul(hidden, w.t()) + b
membrane = self.membrane_decay * membrane + input_current
# Generate spikes with surrogate gradient
spike_prob = self.surrogate(membrane - self.spike_threshold)
spikes = (membrane > self.spike_threshold).float()
membrane = membrane * (1 - spikes) # Reset after spike
all_spikes.append(spikes)
# Output layer
spike_sum = sum(all_spikes)
if training:
w_out = self.reparameterize(self.w_out_mean, self.w_out_logvar)
else:
w_out = self.w_out_mean
output = torch.matmul(spike_sum, w_out.t())
return output
```
### Step 2: IVON Training Loop
```python
def train_bayesian_snn(model, dataloader, optimizer, num_epochs, alpha=0.1):
"""Train Bayesian SNN with IVON."""
for epoch in range(num_epochs):
model.train()
total_loss = 0
for batch_x, batch_y in dataloader:
# Forward pass with Bayesian sampling
output = model(batch_x, training=True)
# Negative log-likelihood loss
nll_loss = F.cross_entropy(output, batch_y)
# KL divergence for Bayesian regularization
kl_loss = compute_kl_divergence(model)
# Combined loss
loss = nll_loss + alpha * kl_loss
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_loss += loss.item()
print(f"Epoch {epoch}: NLL Loss = {nll_loss:.4f}, KL Loss = {kl_loss:.4f}")
def compute_kl_divergence(model):
"""Compute KL divergence between posterior and prior."""
kl = 0
for param_name, param in model.named_parameters():
if 'logvar' in param_name:
mean_param = param_name.replace('logvar', 'mean')
mean = getattr(model, mean_param)
logvar = param
kl += 0.5 * torch.sum(torch.exp(logvar) + mean**2 - 1 - logvar)
return kl
```
### Step 3: Evaluation with Uncertainty
```python
def evaluate_with_uncertainty(model, dataloader):
"""Evaluate with Bayesian uncertainty quantification."""
model.eval()
correct = 0
total = 0
brier_scores = []
with torch.no_grad():
for batch_x, batch_y in dataloader:
# Multiple forward passes for uncertainty
outputs = []
for _ in range(10): # Monte Carlo samples
outputs.append(model(batch_x, training=False))
# Average prediction
avg_output = torch.stack(outputs).mean(dim=0)
pred = avg_output.argmax(dim=1)
correct += (pred == batch_y).sum().item()
total += batch_y.size(0)
# Brier score for probabilistic predictions
probs = F.softmax(avg_output, dim=1)
one_hot = F.one_hot(batch_y, num_classes=avg_output.size(1)).float()
brier = ((probs - one_hot) ** 2).mean().item()
brier_scores.append(brier)
accuracy = correct / total
avg_brier = sum(brier_scores) / len(brier_scores)
return accuracy, avg_brier
```
## Key Results
| Metric | Deterministic SNN | Bayesian SNN (IVON) |
|--------|-------------------|---------------------|
| NLL (Heidelberg Digits) | Higher | **Lower** |
| Brier Score | Higher | **Lower** |
| Loss Landscape | Angular, irregular | **Smooth, regular** |
## Activation Keywords
- bayesian SNN
- speech SNN
- surrogate gradient
- IVON
- loss landscape smoothing
- uncertainty quantification
- variational inference
- Heidelberg Digits
- Speech Commands
- predictive landscape
## Related Papers
- **arXiv:2604.08624**: "Practical Bayesian Inference for Speech SNNs: Uncertainty and Loss-Landscape Smoothing" by Yesmine Abdennadher, Philip N. Garner
## Pitfalls
1. **Computational overhead**: Bayesian sampling increases training time - use MC dropout as approximation
2. **Prior sensitivity**: Choice of prior significantly affects results - use empirical Bayes for hyperparameter tuning
3. **Surrogate gradient compatibility**: Ensure surrogate function is differentiable and smooth
## Tools Used
- `execute_code`: For running SNN training and evaluation experiments
- `write_file`: For saving trained models and results
- `read_file`: For loading speech datasets and configurations