File size: 10,650 Bytes
9f8cf99 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | import torch
import torch.nn as nn
import torch.nn.functional as F
from typing import Any, Tuple, Optional
import numpy as np
from .titans import TitansMemory
from .replay_buffer import ReplayBuffer, Experience
class BaseAgent:
"""Base class for RL agents."""
def __init__(self):
pass
def act(self, state: Any) -> Any:
raise NotImplementedError
def update(self, *args, **kwargs):
raise NotImplementedError
class NestedLearningAgent(BaseAgent):
"""
Nested Learning Agent for QEC with experience replay and proper RL.
Implements nested optimization with:
- Inner loop: Fast updates to decoder weights using policy gradients
- Outer loop: Slow updates to memory/hyperparameters
- Experience replay buffer for sample efficiency
- Actor-critic architecture for stable learning
"""
def __init__(
self,
state_dim: int,
action_dim: int,
hidden_dim: int = 64,
replay_capacity: int = 10000,
gamma: float = 0.99,
gae_lambda: float = 0.95,
):
super().__init__()
self.state_dim = state_dim
self.action_dim = action_dim
self.gamma = gamma
self.gae_lambda = gae_lambda
# TITANS memory for long-term syndrome history
self.memory_module = TitansMemory(input_dim=state_dim, hidden_dim=hidden_dim)
# Actor (policy network)
self.actor = nn.Sequential(
nn.Linear(hidden_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, action_dim)
)
# Critic (value network) for advantage estimation
self.critic = nn.Sequential(
nn.Linear(hidden_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, 1)
)
# Inner loop optimizers (fast)
self.actor_optimizer = torch.optim.Adam(self.actor.parameters(), lr=1e-3)
self.critic_optimizer = torch.optim.Adam(self.critic.parameters(), lr=1e-3)
# Outer loop optimizer (slow)
self.outer_optimizer = torch.optim.Adam(self.memory_module.parameters(), lr=1e-4)
# Experience replay buffer
self.replay_buffer = ReplayBuffer(capacity=replay_capacity, prioritized=True)
# Training statistics
self.training_step = 0
self.episode_rewards = []
def act(self, state: torch.Tensor, deterministic: bool = False) -> Tuple[int, torch.Tensor]:
"""Select action using current policy.
Returns:
Tuple of (action_index, log_probability)
"""
with torch.no_grad():
if state.dim() == 1:
state = state.unsqueeze(0)
context = self.memory_module(state)
logits = self.actor(context)
probs = F.softmax(logits, dim=-1)
if deterministic:
action = torch.argmax(probs, dim=-1)
else:
action = torch.multinomial(probs, 1).squeeze(-1)
# Compute log probability
log_prob = F.log_softmax(logits, dim=-1)
action_log_prob = log_prob.gather(-1, action.unsqueeze(-1)).squeeze(-1)
return action.item(), action_log_prob
def compute_advantages(
self,
rewards: torch.Tensor,
values: torch.Tensor,
dones: torch.Tensor,
) -> Tuple[torch.Tensor, torch.Tensor]:
"""Compute GAE advantages and returns.
Args:
rewards: Tensor of rewards (batch_size,)
values: Tensor of value estimates (batch_size,)
dones: Tensor of done flags (batch_size,)
Returns:
Tuple of (advantages, returns)
"""
batch_size = rewards.shape[0]
advantages = torch.zeros_like(rewards)
returns = torch.zeros_like(rewards)
# Compute GAE advantages
next_advantage = 0
for t in reversed(range(batch_size)):
if t == batch_size - 1:
next_value = 0
else:
next_value = values[t + 1]
delta = rewards[t] + self.gamma * next_value * (1 - dones[t]) - values[t]
advantages[t] = delta + self.gamma * self.gae_lambda * next_advantage * (1 - dones[t])
next_advantage = advantages[t]
returns = advantages + values
return advantages, returns
def inner_loop(
self,
batch_size: int = 32,
clip_epsilon: float = 0.2,
value_coef: float = 0.5,
entropy_coef: float = 0.01,
) -> Tuple[float, float, float]:
"""Policy gradient update using experience replay.
Implements PPO-style clipped surrogate objective with GAE advantages.
Returns:
Tuple of (actor_loss, critic_loss, entropy)
"""
if not self.replay_buffer.is_ready(batch_size):
return 0.0, 0.0, 0.0
# Sample from replay buffer
states, actions, rewards, next_states, dones, weights, indices = self.replay_buffer.sample(batch_size)
# Prepare states for TITANS (add sequence dimension if needed)
if states.dim() == 2:
states = states.unsqueeze(1)
next_states = next_states.unsqueeze(1)
# Get memory context
context = self.memory_module(states.squeeze(1) if states.dim() == 3 else states)
# Compute current policy logits and values
logits = self.actor(context)
values = self.critic(context).squeeze(-1)
# Compute log probabilities of actions
log_probs = F.log_softmax(logits, dim=-1)
action_log_probs = log_probs.gather(-1, actions.unsqueeze(-1)).squeeze(-1)
# Compute entropy for exploration
probs = F.softmax(logits, dim=-1)
entropy = -(probs * log_probs).sum(dim=-1).mean()
# Compute advantages using GAE
advantages, returns = self.compute_advantages(rewards, values, dones)
# Normalize advantages
advantages = (advantages - advantages.mean()) / (advantages.std() + 1e-8)
# PPO-style clipped surrogate loss (without the ratio for simplicity, using vanilla PG)
policy_loss = -(action_log_probs * advantages * weights).mean()
# Value loss
value_loss = F.mse_loss(values, returns, reduction='none')
value_loss = (value_loss * weights).mean()
# Combined loss
actor_loss = policy_loss - entropy_coef * entropy
critic_loss = value_coef * value_loss
# Update actor
self.actor_optimizer.zero_grad()
actor_loss.backward(retain_graph=True)
torch.nn.utils.clip_grad_norm_(self.actor.parameters(), 0.5)
self.actor_optimizer.step()
# Update critic
self.critic_optimizer.zero_grad()
critic_loss.backward()
torch.nn.utils.clip_grad_norm_(self.critic.parameters(), 0.5)
self.critic_optimizer.step()
# Update replay buffer priorities (using TD errors as priorities)
with torch.no_grad():
td_errors = torch.abs(returns - values).cpu().numpy()
self.replay_buffer.update_priorities(indices, td_errors)
self.training_step += 1
return policy_loss.item(), value_loss.item(), entropy.item()
def outer_loop(self, states: torch.Tensor, performance_metric: float) -> float:
"""Slow update: Update memory representations and hyperparameters.
Uses meta-learning signal (performance_metric) to adapt the memory module.
"""
if states.dim() == 2:
states = states.unsqueeze(1)
self.outer_optimizer.zero_grad()
# Get memory context
context = self.memory_module(states.squeeze(1) if states.dim() == 3 else states)
# Meta-learning loss: encourage representations that predict performance
# Use auxiliary task: predict the performance metric from context
performance_prediction = self.critic(context).squeeze(-1).mean()
# Loss encourages accurate performance prediction
meta_loss = F.mse_loss(performance_prediction, torch.tensor(performance_metric))
# Add regularization to encourage diverse representations
context_variance = context.var(dim=0).mean()
diversity_bonus = -0.01 * context_variance # Encourage variance
total_loss = meta_loss + diversity_bonus
total_loss.backward()
# Gradient clipping for stability
torch.nn.utils.clip_grad_norm_(self.memory_module.parameters(), 0.5)
self.outer_optimizer.step()
return total_loss.item()
def store_experience(
self,
state: torch.Tensor,
action: int,
reward: float,
next_state: torch.Tensor,
done: bool,
priority: Optional[float] = None,
) -> None:
"""Store experience in replay buffer with optional priority."""
experience = Experience(
state=state if state.dim() > 0 else state.unsqueeze(0),
action=action,
reward=reward,
next_state=next_state if next_state.dim() > 0 else next_state.unsqueeze(0),
done=done,
)
self.replay_buffer.push(experience, priority)
def get_stats(self) -> dict:
"""Return training statistics."""
return {
"training_step": self.training_step,
"buffer_size": len(self.replay_buffer),
"episode_rewards": self.episode_rewards[-10:] if self.episode_rewards else [],
}
def save_checkpoint(self, path: str) -> None:
"""Save agent state to checkpoint."""
torch.save({
"memory_module": self.memory_module.state_dict(),
"actor": self.actor.state_dict(),
"critic": self.critic.state_dict(),
"actor_optimizer": self.actor_optimizer.state_dict(),
"critic_optimizer": self.critic_optimizer.state_dict(),
"outer_optimizer": self.outer_optimizer.state_dict(),
"training_step": self.training_step,
}, path)
def load_checkpoint(self, path: str) -> None:
"""Load agent state from checkpoint."""
checkpoint = torch.load(path, map_location="cpu")
self.memory_module.load_state_dict(checkpoint["memory_module"])
self.actor.load_state_dict(checkpoint["actor"])
self.critic.load_state_dict(checkpoint["critic"])
self.actor_optimizer.load_state_dict(checkpoint["actor_optimizer"])
self.critic_optimizer.load_state_dict(checkpoint["critic_optimizer"])
self.outer_optimizer.load_state_dict(checkpoint["outer_optimizer"])
self.training_step = checkpoint.get("training_step", 0)
|