Practical Bayesian Inference for Spiking Neural Networks in Speech Recognition. Enables uncertainty-aware speech processing with SNNs, combining Bayesian deep learning with spiking dynamics for robust speech recognition under noisy conditions. Activation: Bayesian SNN, speech recognition, uncertainty quantification, variational inference, spiking speech, robust speech, noisy speech, SNN uncertainty.
Scanned 9/11/2026
Install to Claude Code
npx -y skills add hiyenwong/ai_collection --skill practical-bayesian-speech-snns --agent claude-codeInstalls into .claude/skills of the current project.
Are you the author of Practical Bayesian Speech Snns?
Add the live security badge to your README — it updates automatically with every re-scan.
[](https://www.skillsdirectory.com/skills/hiyenwong-practical-bayesian-speech-snns)More formats (shields.io, HTML) on the badges page.
---
name: practical-bayesian-speech-snns
description: "Practical Bayesian Inference for Spiking Neural Networks in Speech Recognition. Enables uncertainty-aware speech processing with SNNs, combining Bayesian deep learning with spiking dynamics for robust speech recognition under noisy conditions. Activation: Bayesian SNN, speech recognition, uncertainty quantification, variational inference, spiking speech, robust speech, noisy speech, SNN uncertainty."
---
# Practical Bayesian Inference for Speech Spiking Neural Networks
Bayesian inference methodology for Spiking Neural Networks applied to speech recognition, enabling uncertainty quantification and robust performance under noisy conditions.
## Problem Statement
Standard SNNs for speech recognition provide point estimates without uncertainty measures, making them unreliable in real-world noisy environments where confidence calibration is critical.
### Core Challenge
- No uncertainty quantification in deterministic SNNs
- Poor robustness to environmental noise and speaker variability
- Cannot distinguish between aleatoric (data) and epistemic (model) uncertainty
- Critical for safety-critical speech applications
## Bayesian SNN Framework
### Core Innovation
Apply Bayesian inference to spiking neural networks for speech recognition, providing:
1. **Uncertainty quantification**: Confidence estimates for predictions
2. **Robustness**: Better performance under distribution shift
3. **Regularization**: Natural regularization from Bayesian priors
### Key Mathematical Formulation
#### Posterior over Weights
$$
p(w | \mathcal{D}) = \frac{p(\mathcal{D} | w) p(w)}{p(\mathcal{D})}
$$
Where:
- $w$: SNN weights
- $\mathcal{D}$: Training data
- $p(w)$: Prior distribution
- $p(\mathcal{D} | w)$: Likelihood
#### Variational Inference Approximation
$$
q_\theta(w) \approx p(w | \mathcal{D})
$$
Minimize KL divergence:
$$
\theta^* = \arg\min_\theta \text{KL}[q_\theta(w) || p(w | \mathcal{D})]
$$
### Mean-Field Variational Family
$$
q_\theta(w) = \mathcal{N}(w; \mu, \sigma^2 I)
$$
Reparameterization trick:
$$
w = \mu + \sigma \odot \epsilon, \quad \epsilon \sim \mathcal{N}(0, I)
$$
## Implementation
### Step 1: Bayesian Linear Layer for SNN
```python
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.distributions import Normal
class BayesianLinear(nn.Module):
"""Bayesian linear layer for variational inference."""
def __init__(self, in_features, out_features, prior_std=1.0):
super().__init__()
# Variational parameters
self.mu_weight = nn.Parameter(torch.Tensor(out_features, in_features))
self.log_sigma_weight = nn.Parameter(torch.Tensor(out_features, in_features))
# Prior
self.register_buffer('prior_std', torch.tensor(prior_std))
# Initialize
nn.init.kaiming_normal_(self.mu_weight, a=0, mode='fan_in')
nn.init.constant_(self.log_sigma_weight, -3)
def forward(self, x, num_samples=1):
if self.training:
# Sample weights using reparameterization trick
sigma = torch.exp(self.log_sigma_weight)
epsilon = torch.randn_like(sigma)
weights = self.mu_weight + sigma * epsilon
else:
# Use mean during inference
weights = self.mu_weight
return F.linear(x, weights)
def kl_divergence(self):
"""Compute KL divergence from prior."""
sigma = torch.exp(self.log_sigma_weight)
# KL(q || p) for Gaussians
kl = 0.5 * (
(sigma / self.prior_std) ** 2 +
(self.mu_weight / self.prior_std) ** 2 -
1 -
2 * torch.log(sigma / self.prior_std)
)
return kl.sum()
class BayesianLIF(nn.Module):
"""Bayesian Leaky Integrate-and-Fire neuron."""
def __init__(self, in_features, out_features, threshold=1.0, tau=1.0):
super().__init__()
self.threshold = threshold
self.tau = tau
self.linear = BayesianLinear(in_features, out_features)
def forward(self, input_spikes, time_steps):
"""Forward pass with LIF dynamics."""
batch = input_spikes.size(0)
out_features = self.linear.mu_weight.size(0)
voltages = torch.zeros(batch, out_features, device=input_spikes.device)
spikes_out = []
for t in range(time_steps):
# Get input current
current = self.linear(input_spikes[:, t])
# LIF dynamics
voltages = voltages + (current - voltages) / self.tau
# Generate spikes
spike = (voltages >= self.threshold).float()
spikes_out.append(spike)
# Reset
voltages = voltages * (1 - spike)
return torch.stack(spikes_out, dim=1)
```
### Step 2: Speech Feature Extraction for SNN
```python
class SpeechSpikeEncoder(nn.Module):
"""Encode speech features to spike trains."""
def __init__(self, n_mels=64, threshold_method='percentile'):
super().__init__()
self.n_mels = n_mels
self.threshold_method = threshold_method
def forward(self, mel_spectrogram, time_bins=100):
"""
Convert mel spectrogram to spike trains.
mel_spectrogram: (batch, n_mels, time)
"""
batch, n_mels, time = mel_spectrogram.shape
if self.threshold_method == 'percentile':
# Threshold at 90th percentile
thresholds = torch.quantile(
mel_spectrogram, 0.9, dim=-1, keepdim=True
)
else:
thresholds = mel_spectrogram.mean(dim=-1, keepdim=True) + \
mel_spectrogram.std(dim=-1, keepdim=True)
# Generate spikes
spikes = (mel_spectrogram > thresholds).float()
return spikes
```
### Step 3: Complete Bayesian Speech SNN
```python
class BayesianSpeechSNN(nn.Module):
"""Bayesian SNN for speech recognition."""
def __init__(self, n_mels=64, hidden_dim=128, num_classes=30,
time_steps=100):
super().__init__()
self.time_steps = time_steps
self.num_mc_samples = 10 # Monte Carlo samples for uncertainty
# Spike encoder
self.encoder = SpeechSpikeEncoder(n_mels)
# Bayesian SNN layers
self.bayesian_lif1 = BayesianLIF(n_mels, hidden_dim)
self.bayesian_lif2 = BayesianLIF(hidden_dim, hidden_dim // 2)
# Bayesian classifier
self.bayesian_fc = BayesianLinear(hidden_dim // 2, num_classes)
def forward(self, mel_spectrogram):
"""Forward pass with Monte Carlo sampling."""
# Encode to spikes
spikes = self.encoder(mel_spectrogram)
if self.training:
# Single forward pass during training
hidden1 = self.bayesian_lif1(spikes, self.time_steps)
hidden2 = self.bayesian_lif2(hidden1, self.time_steps)
# Spike counting for classification
spike_counts = hidden2.sum(dim=1)
logits = self.bayesian_fc(spike_counts)
return logits
else:
# Monte Carlo sampling during inference
logits_samples = []
for _ in range(self.num_mc_samples):
hidden1 = self.bayesian_lif1(spikes, self.time_steps)
hidden2 = self.bayesian_lif2(hidden1, self.time_steps)
spike_counts = hidden2.sum(dim=1)
logits = self.bayesian_fc(spike_counts)
logits_samples.append(logits)
return torch.stack(logits_samples) # (mc_samples, batch, classes)
def kl_loss(self):
"""Sum of KL divergences from all Bayesian layers."""
kl = self.bayesian_lif1.linear.kl_divergence()
kl += self.bayesian_lif2.linear.kl_divergence()
kl += self.bayesian_fc.kl_divergence()
return kl
def predict_with_uncertainty(self, mel_spectrogram):
"""Get prediction with uncertainty estimates."""
self.eval()
with torch.no_grad():
logits = self.forward(mel_spectrogram) # (mc, batch, classes)
# Mean prediction
mean_logits = logits.mean(dim=0)
pred = mean_logits.argmax(dim=-1)
# Epistemic uncertainty (variance across MC samples)
epistemic = logits.var(dim=0).mean(dim=-1)
# Aleatoric uncertainty (entropy of mean prediction)
probs = F.softmax(mean_logits, dim=-1)
aleatoric = -(probs * torch.log(probs + 1e-8)).sum(dim=-1)
return pred, epistemic, aleatoric
```
### Step 4: Training with ELBO Loss
```python
def train_bayesian_speech_snn(model, dataloader, optimizer,
num_epochs=50, beta=1e-4):
"""Train Bayesian Speech SNN with ELBO loss."""
criterion = nn.CrossEntropyLoss()
for epoch in range(num_epochs):
model.train()
total_loss = 0
correct = 0
total = 0
for mel_specs, labels in dataloader:
optimizer.zero_grad()
# Forward pass
logits = model(mel_specs)
# Negative log-likelihood
nll_loss = criterion(logits, labels)
# KL divergence
kl_loss = model.kl_loss()
# ELBO: E[log p(y|x,w)] - beta * KL(q(w)||p(w))
loss = nll_loss + beta * kl_loss
loss.backward()
optimizer.step()
total_loss += loss.item()
_, predicted = logits.max(1)
total += labels.size(0)
correct += predicted.eq(labels).sum().item()
accuracy = 100. * correct / total
print(f"Epoch {epoch}: Loss={total_loss/len(dataloader):.4f}, Acc={accuracy:.2f}%")
def evaluate_with_uncertainty(model, test_dataloader):
"""Evaluate model and analyze uncertainty."""
model.eval()
all_predictions = []
all_labels = []
all_epistemic = []
all_aleatoric = []
with torch.no_grad():
for mel_specs, labels in test_dataloader:
pred, epistemic, aleatoric = model.predict_with_uncertainty(mel_specs)
all_predictions.extend(pred.cpu().numpy())
all_labels.extend(labels.cpu().numpy())
all_epistemic.extend(epistemic.cpu().numpy())
all_aleatoric.extend(aleatoric.cpu().numpy())
# Analyze uncertainty
import numpy as np
all_predictions = np.array(all_predictions)
all_labels = np.array(all_labels)
all_epistemic = np.array(all_epistemic)
all_aleatoric = np.array(all_aleatoric)
# Uncertainty for correct vs incorrect predictions
correct_mask = all_predictions == all_labels
print(f"Epistemic (correct): {all_epistemic[correct_mask].mean():.4f}")
print(f"Epistemic (wrong): {all_epistemic[~correct_mask].mean():.4f}")
print(f"Aleatoric (correct): {all_aleatoric[correct_mask].mean():.4f}")
print(f"Aleatoric (wrong): {all_aleatoric[~correct_mask].mean():.4f}")
```
## Benefits
| Feature | Standard SNN | Bayesian SNN |
|---------|-------------|--------------|
| Point Estimates | Yes | Yes |
| Uncertainty | No | Yes (epistemic + aleatoric) |
| Noise Robustness | Low | High |
| Out-of-distribution Detection | No | Yes |
| Confidence Calibration | Poor | Good |
## Activation Keywords
- Bayesian SNN
- speech recognition
- uncertainty quantification
- variational inference
- spiking speech
- robust speech
- noisy speech
- SNN uncertainty
- Monte Carlo dropout
- epistemic uncertainty
- aleatoric uncertainty
- ELBO loss
## Related Papers
- **arXiv:2604.08624**: "Practical Bayesian Inference for Speech Spiking Neural Networks"
## Pitfalls
1. **KL weight tuning**: Beta needs careful tuning - too high causes underfitting, too low ignores prior
2. **MC sampling cost**: Multiple forward passes during inference - use fewer samples for speed
3. **Gradient estimation**: Reparameterization trick needed for stable gradients
4. **Memory overhead**: Storing mu and sigma doubles parameter count - use weight sharing
## Tools Used
- `execute_code`: For implementing and testing Bayesian SNN components
- `write_file`: For saving model configurations and uncertainty analysis
- `search_files`: For finding speech datasets (LibriSpeech, TIMIT)Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.
No comments yet. Be the first to comment!