-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaeon.py
More file actions
1388 lines (1163 loc) · 53.8 KB
/
aeon.py
File metadata and controls
1388 lines (1163 loc) · 53.8 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
from torch.distributions import MultivariateNormal, Categorical
from torch.utils.data import DataLoader, Dataset
from collections import defaultdict
import os
import pickle
from typing import Union, Dict, Any, Optional, Tuple, List, Callable
from contextlib import nullcontext
# Utility module for efficient implementation of advanced features
class AeonUtils:
@staticmethod
def setup_device(cuda_device: Union[int, List[int]] = 0,
memory_fraction: float = 0.9,
enable_mixed_precision: bool = True) -> Tuple[torch.device, Dict]:
"""Configure CUDA devices with memory management and precision settings"""
if torch.cuda.is_available():
# Set visible devices if specified as list
if isinstance(cuda_device, list):
os.environ["CUDA_VISIBLE_DEVICES"] = ",".join(map(str, cuda_device))
device = torch.device("cuda:0") # Use first in list as primary
multi_gpu = len(cuda_device) > 1
else:
device = torch.device(f"cuda:{cuda_device}")
multi_gpu = False
# Memory management
for gpu_id in range(torch.cuda.device_count()):
torch.cuda.set_per_process_memory_fraction(memory_fraction, gpu_id)
# Reserve memory to avoid fragmentation
torch.cuda.empty_cache()
# Precision settings
amp_settings = {
"enabled": enable_mixed_precision,
"dtype": torch.float16 if enable_mixed_precision else torch.float32,
"device_type": "cuda"
}
else:
device = torch.device("cpu")
multi_gpu = False
amp_settings = {"enabled": False}
return device, {"multi_gpu": multi_gpu, "amp_settings": amp_settings}
@staticmethod
def create_checkpoint(model: nn.Module, optimizer: optim.Optimizer,
stats: Dict, path: str, extra_data: Dict = None):
"""Create unified checkpoint with model, optimizer and training state"""
checkpoint = {
"model_state": model.state_dict(),
"optimizer_state": optimizer.state_dict(),
"stats": stats
}
if extra_data:
checkpoint.update(extra_data)
torch.save(checkpoint, path)
@staticmethod
def load_checkpoint(path: str, model: nn.Module = None,
optimizer: optim.Optimizer = None) -> Dict:
"""Load checkpoint and restore model/optimizer states if provided"""
checkpoint = torch.load(path, map_location=lambda storage, loc: storage)
if model is not None:
model.load_state_dict(checkpoint["model_state"])
if optimizer is not None:
optimizer.load_state_dict(checkpoint["optimizer_state"])
return checkpoint
@staticmethod
def get_action_distribution(action_type: str, params: Dict) -> torch.distributions.Distribution:
"""Factory method to create appropriate action distribution"""
if action_type == "continuous":
return MultivariateNormal(params["mean"], torch.diag_embed(params["std"].pow(2)))
elif action_type == "discrete":
return Categorical(logits=params["logits"])
elif action_type == "mixed":
# For environments with both continuous and discrete actions
continuous_dist = MultivariateNormal(
params["mean"], torch.diag_embed(params["std"].pow(2))
)
discrete_dist = Categorical(logits=params["logits"])
return (continuous_dist, discrete_dist)
else:
raise ValueError(f"Unknown action type: {action_type}")
# Modify AdaptiveActorCritic to support discrete actions
class AdaptiveActorCritic(nn.Module):
def __init__(self, obs_dim, act_dim, hidden_size=512,
num_layers=3, activation='gelu', action_type='continuous',
discrete_dims=None):
super().__init__()
self.activation = getattr(nn, activation.upper())()
self.action_type = action_type
# Shared trunk with layer norm
self.shared_trunk = nn.ModuleList()
for _ in range(num_layers):
self.shared_trunk.append(nn.Linear(obs_dim if _ == 0 else hidden_size, hidden_size))
self.shared_trunk.append(nn.LayerNorm(hidden_size))
self.shared_trunk.append(self.activation)
# Adaptive gating mechanism
self.gate = nn.Sequential(
nn.Linear(hidden_size, hidden_size),
nn.Sigmoid()
)
# Policy head based on action type
if action_type == 'continuous':
self.actor = nn.Linear(hidden_size, act_dim)
self.log_std = nn.Parameter(torch.zeros(act_dim))
elif action_type == 'discrete':
self.actor = nn.Linear(hidden_size, act_dim)
elif action_type == 'mixed':
# For environments with both continuous and discrete parts
assert discrete_dims is not None, "Must specify discrete_dims for mixed action type"
continuous_dim = act_dim - sum(discrete_dims)
self.actor_continuous = nn.Linear(hidden_size, continuous_dim)
self.log_std = nn.Parameter(torch.zeros(continuous_dim))
self.actor_discrete = nn.ModuleList([
nn.Linear(hidden_size, dim) for dim in discrete_dims
])
# Value head with ensemble
self.value_heads = nn.ModuleList(
[nn.Linear(hidden_size, 1) for _ in range(3)]
)
# Orthogonal initialization
self.apply(self._init_weights)
def _init_weights(self, module):
if isinstance(module, nn.Linear):
nn.init.orthogonal_(module.weight, gain=np.sqrt(2))
nn.init.constant_(module.bias, 0.0)
def forward(self, x):
for layer in self.shared_trunk:
x = layer(x)
gate = self.gate(x)
x = x * gate
# Policy outputs based on action type
if self.action_type == 'continuous':
mean = self.actor(x)
std = torch.exp(self.log_std.clamp(-20, 2))
action_params = {"mean": mean, "std": std}
elif self.action_type == 'discrete':
logits = self.actor(x)
action_params = {"logits": logits}
elif self.action_type == 'mixed':
continuous_mean = self.actor_continuous(x)
std = torch.exp(self.log_std.clamp(-20, 2))
discrete_logits = [head(x) for head in self.actor_discrete]
action_params = {
"mean": continuous_mean,
"std": std,
"logits": discrete_logits
}
# Value ensemble
values = [head(x) for head in self.value_heads]
value = torch.stack(values).mean(0)
value_std = torch.stack(values).std(0)
return action_params, value, value_std
# Update initialization of AdvancedPPO to support efficient options
class AdvancedPPO:
def __init__(self, env, config):
self.env = env
self.obs_dim = env.observation_space.shape[0]
# Handle different action space types
self.action_type = config.get('action_type', 'continuous')
if self.action_type == 'continuous':
self.act_dim = env.action_space.shape[0]
self.discrete_dims = None
elif self.action_type == 'discrete':
self.act_dim = env.action_space.n
self.discrete_dims = None
elif self.action_type == 'mixed':
# For environments with MultiDiscrete or hybrid spaces
self.discrete_dims = config.get('discrete_dims', [])
continuous_dim = config.get('continuous_dim', 0)
self.act_dim = continuous_dim + sum(self.discrete_dims)
# Setup device and precision options
gpu_config = config.get('gpu_config', {})
self.device, gpu_settings = AeonUtils.setup_device(
cuda_device=gpu_config.get('cuda_device', 0),
memory_fraction=gpu_config.get('memory_fraction', 0.9),
enable_mixed_precision=gpu_config.get('mixed_precision', True)
)
self.multi_gpu = gpu_settings['multi_gpu']
self.amp_settings = gpu_settings['amp_settings']
# Hyperparameters
self.clip_range = config.get('clip_range', 0.2)
self.entropy_coef = config.get('entropy_coef', 0.01)
self.kl_target = config.get('kl_target', 0.01)
self.adaptive_lr = config.get('adaptive_lr', True)
self.gamma = config.get('gamma', 0.999)
self.gae_lambda = config.get('gae_lambda', 0.98)
self.batch_size = config.get('batch_size', 512)
self.epochs = config.get('epochs', 15)
# Create networks
self.policy = AdaptiveActorCritic(
self.obs_dim, self.act_dim,
action_type=self.action_type,
discrete_dims=self.discrete_dims
).to(self.device)
self.old_policy = AdaptiveActorCritic(
self.obs_dim, self.act_dim,
action_type=self.action_type,
discrete_dims=self.discrete_dims
).to(self.device)
# Multi-GPU support if available
if self.multi_gpu:
# Create wrapped model for distributed computing
self.policy = nn.DataParallel(self.policy)
self.old_policy = nn.DataParallel(self.old_policy)
self.old_policy.load_state_dict(self.policy.state_dict())
# Optimizer with lookahead
self.optimizer = optim.AdamW(self.policy.parameters(),
lr=config.get('lr', 3e-4),
weight_decay=config.get('wd', 1e-6))
self.lookahead = Lookahead(self.optimizer, k=5, alpha=0.5)
# Learning rate scheduler
self.lr_scheduler = optim.lr_scheduler.ReduceLROnPlateau(
self.optimizer, mode='max', factor=0.5, patience=5,
verbose=True, min_lr=1e-5
) if config.get('use_lr_scheduler', False) else None
# Normalization
self.obs_mean = torch.zeros(self.obs_dim).to(self.device)
self.obs_var = torch.ones(self.obs_dim).to(self.device)
self.return_mean = 0.0
self.return_var = 1.0
# Adaptive parameters
self.clip_range_scheduler = AdaptiveClipper(
init_value=0.2,
kl_target=0.01,
rate=1.5
)
# Checkpointing
self.checkpoint_dir = config.get('checkpoint_dir', './checkpoints')
os.makedirs(self.checkpoint_dir, exist_ok=True)
self.checkpoint_frequency = config.get('checkpoint_frequency', 20)
# Stats tracking
self.training_stats = {
'rewards': [],
'episode_lengths': [],
'value_losses': [],
'policy_losses': [],
'entropies': [],
'kl_divs': [],
'learning_rates': []
}
def adaptive_normalize(self, x, mean, var):
return (x - mean) / torch.sqrt(var + 1e-8)
def compute_gae(self, rewards, values, dones, next_value=None):
# Implementation with TD-lambda return estimation
gae = 0
returns = []
# If next_value is not provided, assume terminal state
if next_value is None:
next_value = 0.0
# Work backwards from the last step
for t in reversed(range(len(rewards))):
# Calculate delta (TD error)
delta = rewards[t] + self.gamma * next_value * (1 - dones[t]) - values[t]
# Calculate GAE
gae = delta + self.gamma * self.gae_lambda * (1 - dones[t]) * gae
# Store return
returns.insert(0, gae + values[t])
# Update next value
next_value = values[t]
return torch.tensor(returns)
def update_normalization(self, batch):
# Welford's algorithm for online stats
observations = batch["observations"]
returns = batch["returns"]
# Update observation statistics
batch_size = observations.shape[0]
delta = observations - self.obs_mean
self.obs_mean = self.obs_mean + delta.mean(0)
delta2 = observations - self.obs_mean
self.obs_var = self.obs_var + ((delta * delta2).mean(0) - self.obs_var) / (batch_size + 1)
# Update return statistics
if isinstance(returns, torch.Tensor):
returns = returns.flatten().cpu().numpy()
batch_return_mean = np.mean(returns)
batch_return_var = np.var(returns)
# Exponential moving average for returns
alpha = 0.005 # Slow update for stability
self.return_mean = (1 - alpha) * self.return_mean + alpha * batch_return_mean
self.return_var = (1 - alpha) * self.return_var + alpha * batch_return_var
def update(self, rollout):
# Advanced update with:
# - Value function clipping
# - Adaptive KL penalty
# - Entropy annealing
# - Prioritized experience replay
# Unpack rollout data
states = torch.FloatTensor(rollout['states']).to(self.device)
actions = torch.FloatTensor(rollout['actions']).to(self.device)
rewards = torch.FloatTensor(rollout['rewards']).to(self.device)
dones = torch.FloatTensor(rollout['dones']).to(self.device)
old_values = torch.FloatTensor(rollout['values']).to(self.device)
old_log_probs = torch.FloatTensor(rollout['log_probs']).to(self.device)
# Normalize states
norm_states = self.adaptive_normalize(states, self.obs_mean, self.obs_var)
# Compute returns and advantages
with torch.no_grad():
# Get values from old policy
_, values, _ = self.old_policy(norm_states)
# Compute GAE
returns = self.compute_gae(rewards, values.squeeze(-1), dones)
# Normalize returns
norm_returns = (returns - self.return_mean) / (np.sqrt(self.return_var) + 1e-8)
# Compute advantages
advantages = returns - values.squeeze(-1)
# Normalize advantages
advantages = (advantages - advantages.mean()) / (advantages.std() + 1e-8)
# Create dataset and dataloader
dataset = ExperienceDataset(
observations=norm_states,
actions=actions,
advantages=advantages,
returns=norm_returns,
old_values=old_values,
old_log_probs=old_log_probs
)
loader = DataLoader(dataset, batch_size=self.batch_size, shuffle=True)
# Track metrics
kl_divs = []
value_losses = []
policy_losses = []
entropy_terms = []
# PPO Update loop
for epoch in range(self.epochs):
for batch in loader:
# Unpack batch
b_obs = batch['observations']
b_acts = batch['actions']
b_advs = batch['advantages']
b_rets = batch['returns']
b_old_values = batch['old_values']
b_old_log_probs = batch['old_log_probs']
# Get current policy outputs
action_params, value, value_std = self.policy(b_obs)
# Create normal distribution
dist = AeonUtils.get_action_distribution(self.action_type, action_params)
# Get log probabilities
log_probs = dist.log_prob(b_acts)
# Calculate KL divergence
old_dist = AeonUtils.get_action_distribution(self.action_type, self.old_policy(b_obs)[0])
kl_div = torch.mean(torch.distributions.kl.kl_divergence(old_dist, dist))
# Early stopping based on KL
if kl_div > 4 * self.kl_target:
break
# Ratio for PPO
ratio = torch.exp(log_probs - b_old_log_probs)
# Policy loss with clipping
clip_range = self.clip_range_scheduler.current_value
policy_loss1 = -b_advs * ratio
policy_loss2 = -b_advs * torch.clamp(ratio, 1 - clip_range, 1 + clip_range)
policy_loss = torch.mean(torch.max(policy_loss1, policy_loss2))
# Value loss with clipping
value_clipped = b_old_values + torch.clamp(
value - b_old_values, -clip_range, clip_range
)
value_loss1 = (value - b_rets).pow(2)
value_loss2 = (value_clipped - b_rets).pow(2)
value_loss = 0.5 * torch.mean(torch.max(value_loss1, value_loss2))
# Entropy bonus
entropy = dist.entropy().mean()
# Total loss
loss = policy_loss + 0.5 * value_loss - self.entropy_coef * entropy
# Update policy
self.optimizer.zero_grad()
loss.backward()
# Clip gradients
torch.nn.utils.clip_grad_norm_(self.policy.parameters(), 0.5)
self.optimizer.step()
# Track metrics
kl_divs.append(kl_div.item())
value_losses.append(value_loss.item())
policy_losses.append(policy_loss.item())
entropy_terms.append(entropy.item())
# Apply lookahead update
self.lookahead.step()
# Update old policy
self.old_policy.load_state_dict(self.policy.state_dict())
# Update clip range based on average KL
if len(kl_divs) > 0:
mean_kl = np.mean(kl_divs)
self.clip_range_scheduler.update(mean_kl)
# Return metrics
metrics = {
"policy_loss": np.mean(policy_losses),
"value_loss": np.mean(value_losses),
"entropy": np.mean(entropy_terms),
"kl_div": np.mean(kl_divs),
"clip_range": self.clip_range_scheduler.current_value
}
return metrics
def train(self, total_timesteps):
# Training loop with:
# - Vectorized environments
# - Dynamic batch sizing
# - Automated curriculum learning
# Resume from checkpoint if specified
timesteps_so_far = 0
episodes = 0
best_reward = -float('inf')
# Create a buffer for storing rollout data
rollout_buffer = {
'states': [],
'actions': [],
'rewards': [],
'dones': [],
'values': [],
'log_probs': []
}
# Initialize progress tracking
episode_rewards = []
episode_lengths = []
successes = []
# For curriculum learning
difficulty_level = 0
success_rate_threshold = 0.8
success_window = 10
# Enable AMP context manager if using mixed precision
amp_context = torch.cuda.amp.autocast if self.amp_settings.get("enabled", False) else nullcontext
# Main training loop
while timesteps_so_far < total_timesteps:
# Maybe adjust environment difficulty based on recent performance
if len(successes) >= success_window:
recent_success_rate = sum(successes[-success_window:]) / success_window
if recent_success_rate > success_rate_threshold:
difficulty_level += 1
print(f"Increasing difficulty to level {difficulty_level}")
# Here you would change environment parameters based on difficulty
# self.env.set_difficulty(difficulty_level)
successes = [] # Reset success tracking after difficulty change
# Reset environment
state, _ = self.env.reset()
done = False
episode_reward = 0
episode_length = 0
# Episode loop
while not done:
# Normalize state
norm_state = self.adaptive_normalize(
torch.FloatTensor(state).to(self.device),
self.obs_mean,
self.obs_var
)
# Get action from policy using mixed precision if enabled
with torch.no_grad(), amp_context():
action_params, value, _ = self.policy(norm_state.unsqueeze(0))
# Create distribution using utility function
dist = AeonUtils.get_action_distribution(self.action_type, action_params)
# Sample action
action = dist.sample()
log_prob = dist.log_prob(action)
# Clip action to environment bounds if needed
clipped_action = action.squeeze(0).cpu().numpy()
if self.action_type == 'continuous' or self.action_type == 'mixed':
clipped_action = np.clip(
clipped_action,
self.env.action_space.low,
self.env.action_space.high
)
# Step environment
next_state, reward, done, truncated, info = self.env.step(clipped_action)
done = done or truncated
# Store in rollout buffer
rollout_buffer['states'].append(state)
rollout_buffer['actions'].append(clipped_action)
rollout_buffer['rewards'].append(reward)
rollout_buffer['dones'].append(float(done))
rollout_buffer['values'].append(value.squeeze().item())
rollout_buffer['log_probs'].append(log_prob.squeeze().item())
# Update counters
timesteps_so_far += 1
episode_reward += reward
episode_length += 1
# Check if we need to update
if len(rollout_buffer['states']) >= self.batch_size or done:
# Convert buffer to numpy arrays
for k, v in rollout_buffer.items():
rollout_buffer[k] = np.array(v)
# Update policy
metrics = self.update(rollout_buffer)
# Store metrics for tracking
for k, v in metrics.items():
if k in self.training_stats:
self.training_stats[k].append(v)
# Clear buffer
for k in rollout_buffer.keys():
rollout_buffer[k] = []
# Move to next state
state = next_state
# Checkpoint saving
if self.checkpoint_frequency > 0 and timesteps_so_far % (self.checkpoint_frequency * self.batch_size) == 0:
self.save_checkpoint(timesteps_so_far)
# Break if we've reached the total timesteps
if timesteps_so_far >= total_timesteps:
break
# Episode complete
episodes += 1
episode_rewards.append(episode_reward)
episode_lengths.append(episode_length)
# Update training stats
self.training_stats['rewards'].append(episode_reward)
self.training_stats['episode_lengths'].append(episode_length)
self.training_stats['learning_rates'].append(self.optimizer.param_groups[0]['lr'])
# Track success for curriculum learning (example: success if reward > threshold)
success = episode_reward > 100 # Adjust threshold based on your task
successes.append(float(success))
# Dynamic batch sizing based on episode length variance
if episodes > 10:
std_episode_length = np.std(episode_lengths[-10:])
# Increase batch size if episodes are consistent length
if std_episode_length < 0.1 * np.mean(episode_lengths[-10:]):
self.batch_size = min(4096, self.batch_size * 1.5)
# Decrease batch size if episodes vary a lot
elif std_episode_length > 0.3 * np.mean(episode_lengths[-10:]):
self.batch_size = max(64, int(self.batch_size * 0.8))
# Track best model
if episode_reward > best_reward:
best_reward = episode_reward
# Save best model
self.save_checkpoint(timesteps_so_far)
# Learning rate scheduling if enabled
if self.lr_scheduler is not None and episodes % 10 == 0:
# Use average reward over last 10 episodes for scheduling
self.lr_scheduler.step(np.mean(episode_rewards[-10:]))
# Print progress
if episodes % 10 == 0:
avg_reward = np.mean(episode_rewards[-10:])
avg_length = np.mean(episode_lengths[-10:])
print(f"Episode {episodes}, Timesteps: {timesteps_so_far}")
print(f"Average reward: {avg_reward:.2f}, Average length: {avg_length:.1f}")
print(f"Current batch size: {self.batch_size}, LR: {self.optimizer.param_groups[0]['lr']:.2e}")
print(f"Clip range: {self.clip_range_scheduler.current_value:.3f}")
# Final checkpoint
final_checkpoint = self.save_checkpoint(timesteps_so_far)
print(f"Training complete. Final checkpoint saved to {final_checkpoint}")
return {
"episodes": episodes,
"timesteps": timesteps_so_far,
"best_reward": best_reward,
"final_avg_reward": np.mean(episode_rewards[-10:]),
"checkpoint_path": final_checkpoint,
"training_stats": self.training_stats
}
# Add checkpoint methods
def save_checkpoint(self, timestep):
"""Save training checkpoint"""
checkpoint_path = os.path.join(self.checkpoint_dir, f"checkpoint_{timestep}.pt")
AeonUtils.create_checkpoint(
model=self.policy,
optimizer=self.optimizer,
stats=self.training_stats,
path=checkpoint_path,
extra_data={
'timestep': timestep,
'obs_mean': self.obs_mean,
'obs_var': self.obs_var,
'return_mean': self.return_mean,
'return_var': self.return_var,
'clip_range': self.clip_range_scheduler.current_value
}
)
return checkpoint_path
def load_checkpoint(self, path):
"""Load training checkpoint"""
checkpoint = AeonUtils.load_checkpoint(
path=path,
model=self.policy,
optimizer=self.optimizer
)
# Copy weights to old policy
self.old_policy.load_state_dict(self.policy.state_dict())
# Restore normalization statistics
self.obs_mean = checkpoint.get('obs_mean', self.obs_mean)
self.obs_var = checkpoint.get('obs_var', self.obs_var)
self.return_mean = checkpoint.get('return_mean', self.return_mean)
self.return_var = checkpoint.get('return_var', self.return_var)
# Restore adaptive clipper
self.clip_range_scheduler.current_value = checkpoint.get(
'clip_range', self.clip_range_scheduler.current_value
)
# Restore stats
self.training_stats = checkpoint.get('stats', self.training_stats)
return checkpoint.get('timestep', 0)
def get_action(self, state, deterministic=False):
"""
Get action from policy for a single state.
Args:
state: Environment state (numpy array)
deterministic: If True, return deterministic action (mean)
If False, sample from distribution (default)
Returns:
action: Action to take in the environment (numpy array)
"""
# Convert state to tensor and normalize
with torch.no_grad():
state_tensor = torch.FloatTensor(state).to(self.device)
norm_state = self.adaptive_normalize(
state_tensor,
self.obs_mean,
self.obs_var
)
# Get policy distribution
action_params, _, _ = self.policy(norm_state.unsqueeze(0))
# Get action based on action type and deterministic flag
if deterministic:
if self.action_type == 'continuous':
action = action_params["mean"]
elif self.action_type == 'discrete':
action = torch.argmax(action_params["logits"], dim=-1)
elif self.action_type == 'mixed':
continuous_action = action_params["mean"]
discrete_actions = [torch.argmax(logits, dim=-1) for logits in action_params["logits"]]
action = torch.cat([continuous_action] + discrete_actions, dim=-1)
else:
# Sample from distribution
dist = AeonUtils.get_action_distribution(self.action_type, action_params)
if self.action_type == 'mixed':
# Handle mixed action spaces
continuous_dist, discrete_dist = dist
continuous_action = continuous_dist.sample()
discrete_action = discrete_dist.sample()
action = torch.cat([continuous_action, discrete_action], dim=-1)
else:
action = dist.sample()
# Clip continuous actions to environment bounds if needed
if self.action_type == 'continuous' or self.action_type == 'mixed':
if hasattr(self.env.action_space, 'low') and hasattr(self.env.action_space, 'high'):
action_np = action.squeeze(0).cpu().numpy()
action_np = np.clip(
action_np,
self.env.action_space.low,
self.env.action_space.high
)
return action_np
# Return action as numpy array
return action.squeeze(0).cpu().numpy()
class Lookahead:
# Lookahead optimizer implementation
def __init__(self, optimizer, k=5, alpha=0.5):
"""
Lookahead optimizer wrapper for enhanced optimization stability
Args:
optimizer: Base optimizer (e.g., Adam, SGD)
k: Number of steps before parameter sync (default: 5)
alpha: Slow weights step size (default: 0.5)
"""
self.optimizer = optimizer
self.k = k
self.alpha = alpha
self.param_groups = self.optimizer.param_groups
self.state = defaultdict(dict)
self.fast_state = self.optimizer.state
# Initialize slow parameter copies
for group in self.param_groups:
for p in group['params']:
param_state = self.state[p]
param_state['slow_param'] = torch.zeros_like(p.data)
param_state['slow_param'].copy_(p.data)
param_state['step'] = 0
def step(self):
"""
Perform one optimization step with lookahead
"""
loss = None
# Count number of lookahead steps
for group in self.param_groups:
for p in group['params']:
param_state = self.state[p]
param_state['step'] += 1
# Synchronize parameters every k steps
if param_state['step'] % self.k == 0:
# Get slow parameters
slow_param = param_state['slow_param']
# Update slow weights
slow_param.data.add_(
self.alpha * (p.data - slow_param.data)
)
# Copy slow parameters to fast parameters
p.data.copy_(slow_param.data)
return loss
def state_dict(self):
"""
Return the optimizer state dict
"""
fast_state_dict = self.optimizer.state_dict()
slow_state = {
(id(k) if isinstance(k, torch.Tensor) else k): v
for k, v in self.state.items()
}
fast_state = fast_state_dict['state']
param_groups = fast_state_dict['param_groups']
return {
'fast_state': fast_state,
'slow_state': slow_state,
'param_groups': param_groups
}
def load_state_dict(self, state_dict):
"""
Load optimizer state dict
"""
fast_state_dict = {
'state': state_dict['fast_state'],
'param_groups': state_dict['param_groups']
}
self.optimizer.load_state_dict(fast_state_dict)
# Build a dict with id keys for applying slow state
slow_state_dict = {
k: v for k, v in state_dict['slow_state'].items()
}
for k, v in slow_state_dict.items():
self.state[k] = v
class AdaptiveClipper:
# Automatic clip range adjustment based on KL divergence
def __init__(self, init_value=0.2, kl_target=0.01, rate=1.5, min_value=0.05, max_value=0.5):
"""
Adaptively adjust PPO clip range based on KL divergence
Args:
init_value (float): Initial clip range (default: 0.2)
kl_target (float): Target KL divergence (default: 0.01)
rate (float): Adjustment rate (default: 1.5)
min_value (float): Minimum clip range (default: 0.05)
max_value (float): Maximum clip range (default: 0.5)
"""
self.current_value = init_value
self.init_value = init_value
self.kl_target = kl_target
self.rate = rate
self.min_value = min_value
self.max_value = max_value
def update(self, current_kl):
"""
Update clip range based on the current KL divergence
Args:
current_kl (float): Current KL divergence
"""
# If KL is too high, decrease clip range to be more conservative
if current_kl > 2.0 * self.kl_target:
self.current_value = max(self.min_value, self.current_value / self.rate)
# If KL is too low, increase clip range to explore more
elif current_kl < 0.5 * self.kl_target:
self.current_value = min(self.max_value, self.current_value * self.rate)
return self.current_value
def reset(self):
"""
Reset clip range to initial value
"""
self.current_value = self.init_value
class ExperienceDataset(Dataset):
# Prioritized experience replay buffer
def __init__(self, observations, actions, advantages, returns, old_values, old_log_probs, priorities=None):
"""
Dataset for PPO experience replay with optional prioritization
Args:
observations: Normalized observation tensors
actions: Action tensors
advantages: Advantage estimates
returns: Return estimates
old_values: Values from the old policy
old_log_probs: Log probabilities from the old policy
priorities: Optional priority weights for sampling
"""
self.observations = observations
self.actions = actions
self.advantages = advantages
self.returns = returns
self.old_values = old_values
self.old_log_probs = old_log_probs
# Set up prioritization if provided
if priorities is None:
# Default to uniform sampling
self.priorities = torch.ones_like(advantages)
else:
# Use provided priorities
self.priorities = priorities
# Normalize priorities for proper sampling
self.priorities = self.priorities / self.priorities.sum()
# Store size
self.size = observations.shape[0]
def __len__(self):
return self.size
def __getitem__(self, idx):
return {
'observations': self.observations[idx],
'actions': self.actions[idx],
'advantages': self.advantages[idx],
'returns': self.returns[idx],
'old_values': self.old_values[idx],
'old_log_probs': self.old_log_probs[idx],
'priorities': self.priorities[idx]
}
def update_priorities(self, indices, new_priorities):
"""
Update priorities for specific samples
Args:
indices: Indices of samples to update
new_priorities: New priority values
"""
self.priorities[indices] = new_priorities
# Renormalize
self.priorities = self.priorities / self.priorities.sum()
def get_prioritized_sampler(self, batch_size):
"""
Create a sampler that uses the priorities for sampling
Args:
batch_size: Batch size for sampling
Returns:
sampler: A sampler for the DataLoader
"""
from torch.utils.data import WeightedRandomSampler
# Create a weighted sampler
sampler = WeightedRandomSampler(
weights=self.priorities,
num_samples=batch_size,
replacement=True
)
return sampler
class CuriosityModule(nn.Module):
# Intrinsic motivation through forward dynamics
def __init__(self, obs_dim, act_dim, hidden_size=256, feature_dim=128):
"""
Curiosity-driven exploration module implementing ICM (Intrinsic Curiosity Module)
Args:
obs_dim: Observation dimension
act_dim: Action dimension
hidden_size: Hidden layer size for networks
feature_dim: Dimension of the feature encoding
"""
super().__init__()
# Feature encoder (state -> feature space)
self.feature_encoder = nn.Sequential(
nn.Linear(obs_dim, hidden_size),
nn.LayerNorm(hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, feature_dim)
)
# Forward dynamics model (feature, action -> next feature)
self.forward_dynamics = nn.Sequential(
nn.Linear(feature_dim + act_dim, hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, feature_dim)
)
# Inverse dynamics model (feature, next_feature -> action)
self.inverse_dynamics = nn.Sequential(
nn.Linear(feature_dim * 2, hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, act_dim)
)
# Initialize weights
self.apply(self._init_weights)