evolutionary-policy-optimization 0.4.1__tar.gz → 0.4.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: evolutionary-policy-optimization
3
- Version: 0.4.1
3
+ Version: 0.4.2
4
4
  Summary: EPO - Pytorch
5
5
  Project-URL: Homepage, https://pypi.org/project/evolutionary-policy-optimization/
6
6
  Project-URL: Repository, https://github.com/lucidrains/evolutionary-policy-optimization
@@ -44,6 +44,7 @@ Requires-Dist: mean-conc-beta>=0.2.7
44
44
  Requires-Dist: torch>=2.2
45
45
  Requires-Dist: tqdm
46
46
  Requires-Dist: x-mlps-pytorch>=0.3.0
47
+ Requires-Dist: x-ppo>=0.0.16
47
48
  Provides-Extra: examples
48
49
  Requires-Dist: fire; extra == 'examples'
49
50
  Requires-Dist: gymnasium[box2d]>=1.0.0; extra == 'examples'
@@ -29,6 +29,7 @@ from torch.utils._pytree import tree_map
29
29
  from torch.utils.data import DataLoader, TensorDataset
30
30
  from tqdm import tqdm
31
31
  from x_mlps_pytorch import AttnResidualNormedMLP
32
+ from x_ppo import hpo_actor_loss, ppo_actor_loss, spo_actor_loss
32
33
 
33
34
  from evolutionary_policy_optimization.distributed import all_gather, get_world_and_rank, is_distributed, maybe_barrier, maybe_sync_seed
34
35
 
@@ -1863,39 +1864,31 @@ def actor_loss(
1863
1864
  advantages, # Float[b]
1864
1865
  eps_clip = 0.2,
1865
1866
  entropy_weight = .01,
1866
- eps = 1e-5,
1867
1867
  norm_advantages = True,
1868
- use_spo = False
1868
+ use_spo = False,
1869
+ use_hpo = False
1869
1870
  ):
1870
- batch = advantages.shape[0]
1871
-
1872
1871
  log_probs = sum_to_batch(distr.log_prob(actions))
1873
1872
  entropy = sum_to_batch(distr.entropy())
1874
1873
 
1875
- ratio = (log_probs - old_log_probs).exp()
1876
-
1877
- if norm_advantages:
1878
- advantages = F.layer_norm(advantages, (batch,), eps = eps)
1879
-
1880
- if use_spo:
1881
- # simple policy optimization - line 14 Algorithm 1 https://arxiv.org/abs/2401.16025v9
1882
-
1883
- actor_loss = - (
1884
- ratio * advantages -
1885
- advantages.abs() / (2 * eps_clip) * (ratio - 1.).square()
1886
- )
1874
+ if use_hpo:
1875
+ loss_fn = hpo_actor_loss
1876
+ elif use_spo:
1877
+ loss_fn = spo_actor_loss
1887
1878
  else:
1888
- # classic clipped surrogate loss from ppo
1889
-
1890
- clipped_ratio = ratio.clamp(min = 1. - eps_clip, max = 1. + eps_clip)
1891
-
1892
- actor_loss = -torch.min(clipped_ratio * advantages, ratio * advantages)
1893
-
1894
- # add entropy loss for exploration
1879
+ loss_fn = ppo_actor_loss
1880
+
1881
+ loss = loss_fn(
1882
+ log_probs,
1883
+ old_log_probs,
1884
+ advantages,
1885
+ eps_clip = eps_clip,
1886
+ normalize_advantages = norm_advantages,
1887
+ )
1895
1888
 
1896
- entropy_aux_loss = -entropy_weight * entropy
1889
+ # entropy bonus
1897
1890
 
1898
- return (actor_loss + entropy_aux_loss).mean()
1891
+ return (loss - entropy_weight * entropy).mean()
1899
1892
 
1900
1893
  # agent contains the actor, critic, and the latent genetic pool
1901
1894
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "evolutionary-policy-optimization"
3
- version = "0.4.1"
3
+ version = "0.4.2"
4
4
  description = "EPO - Pytorch"
5
5
  authors = [
6
6
  { name = "Phil Wang", email = "lucidrains@gmail.com" }
@@ -34,6 +34,7 @@ dependencies = [
34
34
  'torch>=2.2',
35
35
  'tqdm',
36
36
  "x-mlps-pytorch>=0.3.0",
37
+ "x-ppo>=0.0.16",
37
38
  ]
38
39
 
39
40
  [project.urls]