| name | brain-inspired-attention-mechanisms |
| description | Brain-inspired attention mechanisms for neural networks - incorporating biological attention systems including thalamocortical circuits, pulvinar-mediated attention, basal forebrain modulation, and predictive processing. Implements biologically plausible attention for computer vision, NLP, and multi-modal AI. Activation: brain attention, thalamic attention, pulvinar, predictive attention, biological attention, neuromorphic attention, cortico-thalamic, saliency-based attention. |
| tags | ["brain-inspired","attention-mechanisms","thalamocortical","predictive-processing","saliency","selective-attention","biological-plausibility"] |
Brain-Inspired Attention Mechanisms
Overview
Biological attention systems have evolved sophisticated mechanisms for selective information processing. This skill implements brain-inspired attention mechanisms that go beyond standard transformer self-attention, incorporating insights from thalamocortical circuits, predictive processing, and neuromodulatory systems.
Biological Attention Systems
1. Thalamocortical Circuit
Biological Architecture:
Sensory Input → Thalamus → Primary Cortex
↑ ↓
Gain Feedback
Control (Top-down)
↑
Higher-order
Thalamus
↑
Prefrontal
Cortex
Key Functions:
- Gating: Thalamus gates sensory input to cortex
- Modulation: Higher-order thalamus controls cortical gain
- Routing: Information routing to appropriate cortical areas
2. Pulvinar-Mediated Attention
Pulvinar (Posterior Thalamus):
- Coordinates activity across cortical areas
- Synchronizes relevant neural populations
- Suppresses irrelevant information
3. Basal Forebrain Modulation
Basal Forebrain → Acetylcholine (ACh) → Cortex
- Enhances signal-to-noise ratio
- Promotes plasticity
- Regulates arousal/attention state
Brain-Inspired Attention Implementation
1. Thalamic Gating Attention
import torch
import torch.nn as nn
import torch.nn.functional as F
class ThalamicGatingAttention(nn.Module):
"""
Attention mechanism inspired by thalamic gating.
The thalamus acts as a gate between sensory input and cortex,
controlled by top-down attention signals.
"""
def __init__(self, dim, num_heads=8, gate_bias=-3.0):
super().__init__()
self.dim = dim
self.num_heads = num_heads
self.head_dim = dim // num_heads
self.scale = self.head_dim ** -0.5
self.gate_threshold = nn.Parameter(torch.tensor(gate_bias))
self.q_proj = nn.Linear(dim, dim)
self.k_proj = nn.Linear(dim, dim)
self.v_proj = nn.Linear(dim, dim)
self.out_proj = nn.Linear(dim, dim)
self.gate_control = nn.Sequential(
nn.Linear(dim, dim // 4),
nn.ReLU(),
nn.Linear(dim // 4, num_heads),
nn.Sigmoid()
)
def forward(self, x, context=None, return_gates=False):
"""
Args:
x: Input (batch, seq_len, dim)
context: Top-down control signal (batch, dim)
Returns:
output: Attended features
gates: Gating values (optional)
"""
batch, seq_len, _ = x.shape
Q = .q_proj(x).view(batch, seq_len, .num_heads, .head_dim)
K = .k_proj(x).view(batch, seq_len, .num_heads, .head_dim)
V = .v_proj(x).view(batch, seq_len, .num_heads, .head_dim)
Q = Q.transpose(, )
K = K.transpose(, )
V = V.transpose(, )
attn_scores = torch.matmul(Q, K.transpose(-, -)) * .scale
context :
gate_control = .gate_control(context)
gate_control = gate_control.view(batch, .num_heads, , )
:
gate_control = torch.sigmoid(-.gate_threshold)
attn_weights = F.softmax(attn_scores, dim=-)
gated_attn = attn_weights * gate_control
gated_attn = gated_attn / (gated_attn.(dim=-, keepdim=) + )
output = torch.matmul(gated_attn, V)
output = output.transpose(, ).contiguous().view(batch, seq_len, .dim)
output = .out_proj(output)
return_gates:
output, gate_control.squeeze()
output
2. Pulvinar Synchronization Attention
class PulvinarSynchronizationAttention(nn.Module):
"""
Attention inspired by pulvinar-mediated inter-areal synchronization.
The pulvinar synchronizes activity across cortical areas,
enhancing communication between relevant neural populations.
"""
def __init__(self, dim, num_areas=4, sync_strength=0.5):
super().__init__()
self.dim = dim
self.num_areas = num_areas
self.area_dim = dim // num_areas
self.sync_strength = sync_strength
self.area_projections = nn.ModuleList([
nn.Linear(self.area_dim, self.area_dim)
for _ in range(num_areas)
])
self.pulvinar = nn.Sequential(
nn.Linear(dim, dim // 2),
nn.LayerNorm(dim // 2),
nn.ReLU(),
nn.Linear(dim // 2, num_areas * num_areas)
)
self.phase_encoder = nn.Linear(dim, dim)
def forward(self, x):
"""
Synchronize processing across multiple "cortical areas".
Args:
x: (batch, seq, dim)
Returns:
synchronized: (batch, seq, dim)
"""
batch, seq_len, _ = x.shape
areas = x.view(batch, seq_len, self.num_areas, .area_dim)
processed_areas = []
i, proj (.area_projections):
area_out = proj(areas[:, :, i, :])
processed_areas.append(area_out)
global_repr = x.mean(dim=)
sync_weights = .pulvinar(global_repr)
sync_weights = sync_weights.view(batch, .num_areas, .num_areas)
sync_weights = F.softmax(sync_weights, dim=-)
synchronized = []
i (.num_areas):
synced = (
sync_weights[:, i, j].view(batch, , ) * processed_areas[j]
j (.num_areas)
)
synchronized.append(synced)
output = torch.stack(synchronized, dim=).view(batch, seq_len, .dim)
phase = torch.sigmoid(.phase_encoder(x))
output = output * phase + x * ( - phase)
output
3. Basal Forebrain Modulated Attention
class BasalForebrainModulatedAttention(nn.Module):
"""
Attention with arousal/state modulation inspired by basal forebrain.
The basal forebrain provides neuromodulatory input (ACh) that:
- Enhances SNR in attended channels
- Promotes plasticity
- Regulates overall arousal
"""
def __init__(self, dim, num_states=3):
super().__init__()
self.dim = dim
self.num_states = num_states
self.arousal_estimator = nn.Sequential(
nn.Linear(dim, dim // 4),
nn.ReLU(),
nn.Linear(dim // 4, num_states)
)
self.state_gains = nn.Parameter(torch.ones(num_states, dim))
self.state_thresholds = nn.Parameter(torch.zeros(num_states, dim))
self.plasticity_gate = nn.Sequential(
nn.Linear(dim, dim),
nn.Sigmoid()
)
def forward(self, x, return_state=False):
"""
Modulate attention based on arousal state.
Args:
x: (batch, seq, dim)
Returns:
modulated: (batch, seq, dim)
arousal: (batch, num_states) - arousal probabilities
"""
batch, seq_len, _ = x.shape
global_x = x.mean(dim=1)
arousal_logits = self.arousal_estimator(global_x)
arousal_probs = F.softmax(arousal_logits, dim=-1)
weighted_gains = torch.matmul(
arousal_probs, .state_gains
).unsqueeze()
weighted_thresholds = torch.matmul(
arousal_probs, .state_thresholds
).unsqueeze()
modulated = x * ( + * torch.tanh(weighted_gains))
modulated = modulated - weighted_thresholds
plasticity = .plasticity_gate(global_x).unsqueeze()
.last_plasticity = plasticity
return_state:
modulated, arousal_probs
modulated
4. Predictive Processing Attention
class PredictiveProcessingAttention(nn.Module):
"""
Attention based on predictive processing/free energy principle.
Attention is directed to minimize prediction error,
similar to how the brain processes sensory input.
"""
def __init__(self, dim, num_levels=3, precision_learning=True):
super().__init__()
self.dim = dim
self.num_levels = num_levels
self.precision_learning = precision_learning
self.predictors = nn.ModuleList([
nn.Linear(dim, dim) for _ in range(num_levels)
])
self.precision = nn.ParameterList([
nn.Parameter(torch.ones(dim)) for _ in range(num_levels)
])
self.error_proj = nn.ModuleList([
nn.Linear(dim * 2, dim) for _ in range(num_levels)
])
self.prior_proj = nn.Linear(dim, dim)
def forward(self, x, prior=None):
"""
Process input through hierarchical predictive coding.
Args:
x: Sensory input (batch, seq, dim)
prior: Top-down prior (batch, dim)
Returns:
posterior: Updated beliefs
total_error: Prediction error (for loss)
"""
batch, seq_len, _ = x.shape
prior :
prior = torch.zeros(batch, .dim, device=x.device)
total_error =
current = x.mean(dim=)
level (.num_levels):
prediction = .predictors[level](prior)
error = current - prediction
weighted_error = error * torch.sigmoid(.precision[level])
posterior_input = torch.cat([current, weighted_error], dim=-)
posterior = .error_proj[level](posterior_input)
prior = posterior
current = posterior
total_error = total_error + (error ** ).mean()
output = posterior.unsqueeze().expand(-, seq_len, -)
output, total_error
():
output, prediction_error = .forward(x)
recon_loss = F.mse_loss(output, target)
complexity = (p.().mean() p .precision)
free_energy = recon_loss + prediction_error + * complexity
free_energy
Integrated Brain-Inspired Attention Block
class BrainInspiredAttentionBlock(nn.Module):
"""
Comprehensive attention block integrating multiple brain-inspired mechanisms.
"""
def __init__(self, dim, num_heads=8, num_areas=4):
super().__init__()
self.norm1 = nn.LayerNorm(dim)
self.norm2 = nn.LayerNorm(dim)
self.norm3 = nn.LayerNorm(dim)
self.thalamic_attn = ThalamicGatingAttention(dim, num_heads)
self.pulvinar_sync = PulvinarSynchronizationAttention(dim, num_areas)
self.basal_forebrain = BasalForebrainModulatedAttention(dim)
self.predictive = PredictiveProcessingAttention(dim)
self.ffn = nn.Sequential(
nn.Linear(dim, dim * 4),
nn.GELU(),
nn.Dropout(0.1),
nn.Linear(dim * 4, dim),
nn.Dropout(0.1)
)
self.integration_weights = nn.Parameter(torch.ones(4))
def forward(self, x, return_components=False):
"""
Apply brain-inspired attention mechanisms.
Args:
x: (batch, seq, dim)
Returns:
output: (batch, seq, dim)
"""
x_norm = self.norm1(x)
thalamic_out = .thalamic_attn(x_norm)
pulvinar_out = .pulvinar_sync(x_norm)
basal_out = .basal_forebrain(x_norm)
pred_out, pred_error = .predictive(x_norm)
weights = F.softmax(.integration_weights, dim=)
integrated = (
weights[] * thalamic_out +
weights[] * pulvinar_out +
weights[] * basal_out +
weights[] * pred_out
)
x = x + integrated
x = x + .ffn(.norm3(x))
return_components:
x, {
: thalamic_out,
: pulvinar_out,
: basal_out,
: pred_out,
: pred_error,
: weights
}
x
Vision-Specific: Saliency-Based Attention
class BiologicalSaliencyAttention(nn.Module):
"""
Saliency-based attention inspired by visual cortex and superior colliculus.
Combines:
- Bottom-up saliency (unusual features)
- Top-down goal modulation
- Inhibition of return
"""
def __init__(self, dim, spatial_size=(14, 14)):
super().__init__()
self.dim = dim
self.H, self.W = spatial_size
self.saliency_net = nn.Sequential(
nn.Conv2d(dim, dim // 2, 3, padding=1),
nn.ReLU(),
nn.Conv2d(dim // 2, dim // 4, 3, padding=1),
nn.ReLU(),
nn.Conv2d(dim // 4, 1, 1)
)
self.center_surround = nn.Conv2d(1, 1, 5, padding=2, bias=False)
with torch.no_grad():
self.center_surround.weight.data = self._create_dog_kernel()
self.goal_proj = nn.Linear(dim, self.H * self.W)
.register_buffer(, torch.ones(, , .H, .W))
.ior_decay =
():
x = torch.arange().() -
xx, yy = torch.meshgrid(x, x, indexing=)
center = torch.exp(-(xx** + yy**) / ( * **))
surround = torch.exp(-(xx** + yy**) / ( * **))
dog = center - * surround
dog.unsqueeze().unsqueeze()
():
batch = x.shape[]
saliency = .saliency_net(x)
saliency = torch.sigmoid(saliency)
saliency = .center_surround(saliency)
saliency = F.relu(saliency)
goal :
goal_map = .goal_proj(goal).view(batch, , .H, .W)
saliency = saliency + torch.sigmoid(goal_map)
saliency = saliency * .ior_mask
saliency_flat = saliency.view(batch, -)
saliency_flat = F.softmax(saliency_flat, dim=-)
saliency = saliency_flat.view(batch, , .H, .W)
torch.no_grad():
.ior_mask = .ior_mask * .ior_decay + ( - saliency) * ( - .ior_decay)
attended = x * saliency
attended, saliency.squeeze()
Training with Attention Monitoring
class BrainInspiredAttentionTrainer:
"""
Training framework with attention monitoring and regularization.
"""
def __init__(self, model, optimizer, lambda_arousal=0.01, lambda_diversity=0.1):
self.model = model
self.optimizer = optimizer
self.lambda_arousal = lambda_arousal
self.lambda_diversity = lambda_diversity
def train_step(self, x, y):
"""Training step with brain-inspired regularization."""
self.optimizer.zero_grad()
if hasattr(self.model, 'return_components'):
output, components = self.model(x, return_components=True)
else:
output = self.model(x)
components = {}
task_loss = F.cross_entropy(output, y)
if 'arousal' in components:
arousal = components['arousal']
target_arousal = torch.tensor([0.1, 0.8, 0.1], device=arousal.device)
arousal_loss = F.kl_div(arousal.log(), target_arousal.expand_as(arousal))
else:
arousal_loss = 0
components:
pred_error = components[]
pred_loss = pred_error * .lambda_arousal
:
pred_loss =
total_loss = task_loss + .lambda_arousal * arousal_loss + pred_loss
total_loss.backward()
.optimizer.step()
{
: task_loss.item(),
: total_loss.item(),
: (output.argmax(dim=) == y).().mean().item()
}
References
- Halassa, M. M., & Kastner, S. (2017). Thalamic functions in distributed cognitive control. Nature Neuroscience.
- Saalmann, Y. B., & Kastner, S. (2011). Cognitive and perceptual functions of the visual thalamus. Neuron.
- Friston, K. (2010). The free-energy principle: a unified brain theory? Nature Reviews Neuroscience.
- Buschman, T. J., & Miller, E. K. (2007). Top-down versus bottom-up control of attention in the prefrontal and posterior parietal cortices. Science.
- Itti, L., & Koch, C. (2001). Computational modelling of visual attention. Nature Reviews Neuroscience.
- Shipp, S. (2003). The functional logic of cortico-pulvinar connections. Philosophical Transactions of the Royal Society.
Activation Keywords
- brain attention
- thalamic attention
- pulvinar synchronization
- basal forebrain modulation
- predictive processing attention
- biological attention
- neuromorphic attention
- saliency attention
- cortico-thalamic attention
- free energy attention