| name | cognitive-flexibility-task-structure |
| description | Neural network model of attention to task structure enabling cognitive flexibility. Studies how neural networks learn to attend to relevant task dimensions and switch between task rules. Combines attention mechanisms with structured task representations to model cognitive control. Use when building neural models of cognitive flexibility, studying task switching in neural networks, implementing attention-based rule learning, or analyzing prefrontal cortex-like computation in artificial networks. Triggers: cognitive flexibility, task structure, task switching, attention task, rule learning neural, cognitive control, prefrontal model. |
Attention to Task Structure for Cognitive Flexibility
Core Concept
Cognitive flexibility — the ability to switch between different task rules or mental sets — emerges from neural networks that learn to attend to relevant task structure. This skill provides methodology for building and analyzing such models.
Key Mechanisms
1. Task Structure Representation
Tasks are decomposed into structural components:
- Input dimensions: What features are available
- Rule set: Which mapping from inputs to outputs applies
- Context signal: External or internal cue indicating current rule
2. Attention-Based Rule Selection
The network learns a gating mechanism:
attention_weights = softmax(W_context · h_context + W_rule · h_rule)
output = Σ_i attention_weights[i] · f_i(input)
Where f_i represents different input-output mappings (rules).
3. Meta-Learning for Rule Acquisition
Rules are acquired through meta-learning:
θ_rule ← θ_rule - α · ∇_θ L_task(θ, context)
The network updates its rule-specific parameters based on task context, enabling rapid adaptation.
Implementation Architecture
Core Model
import torch
import torch.nn as nn
class CognitiveFlexibilityModel(nn.Module):
"""Neural network with attention-based task structure."""
def __init__(self, input_dim, hidden_dim, n_rules, output_dim):
super().__init__()
self.n_rules = n_rules
self.rule_modules = nn.ModuleList([
nn.Sequential(
nn.Linear(input_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, output_dim)
) for _ in range(n_rules)
])
self.context_encoder = nn.Linear(input_dim, hidden_dim)
self.rule_attention = nn.Linear(hidden_dim, n_rules)
def forward(self, x, context):
"""x: input, context: task context cue"""
ctx_repr = torch.relu(self.context_encoder(context))
attn_weights = torch.softmax(self.rule_attention(ctx_repr), dim=-1)
rule_outputs = torch.stack([
rule(x) for rule in self.rule_modules
], dim=-1)
output = torch.sum(rule_outputs * attn_weights.unsqueeze(), dim=-)
output, attn_weights
Training with Meta-Learning
def meta_train_step(model, task_batch, context, lr_inner=0.01):
"""Inner loop: adapt to a specific task rule."""
adapted = clone_model(model)
loss = compute_loss(adapted, task_batch, context)
adapted.fast_adapt(loss, lr=lr_inner)
meta_loss = compute_loss(adapted, task_batch, context)
return meta_loss
def task_switch_eval(model, old_context, new_context, test_data):
"""Evaluate ability to switch between task rules."""
_, old_attn = model(test_data.x, old_context)
_, new_attn = model(test_data.x, new_context)
switch_cost = torch.norm(old_attn - new_attn)
return switch_cost
Analysis Methods
Measuring Cognitive Flexibility
-
Switch Cost: Performance degradation when switching rules
switch_cost = accuracy(task_A after B) - accuracy(task_A after A)
-
Rule Abstraction: How well learned rules generalize to new inputs
abstraction_score = accuracy(new_inputs, known_rule)
-
Attention Alignment: Correlation between attention weights and ground-truth rule relevance
alignment = cosine_similarity(attn_weights, rule_relevance)
Visualization
def plot_attention_dynamics(model, context_sequence, input_stream):
"""Plot how attention shifts across rules over time."""
attn_weights = []
for ctx in context_sequence:
_, attn = model(input_stream, ctx)
attn_weights.append(attn.detach().cpu().numpy())
plt.imshow(np.array(attn_weights).T, aspect='auto')
plt.xlabel('Time Step')
plt.ylabel('Rule')
plt.title('Attention Dynamics Across Task Switches')
Experimental Paradigms
| Paradigm | Description | Key Metric |
|---|
| Task switching | Alternate between 2+ rules | Switch cost, reaction time |
| Rule abstraction | Apply learned rule to novel inputs | Generalization accuracy |
| Context learning | Learn context cues from data | Context inference accuracy |
| Interference | Competing rules active simultaneously | Conflict resolution rate |
Applications
- Cognitive modeling: Understanding prefrontal cortex function
- Continual learning: Networks that adapt to new tasks without forgetting
- Multi-task learning: Shared representations with task-specific routing
- Adaptive AI: Systems that flexibly switch strategies based on context