evolutionary-policy-optimization 0.2.22__tar.gz → 0.3.1__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.
- {evolutionary_policy_optimization-0.2.22 → evolutionary_policy_optimization-0.3.1}/PKG-INFO +5 -5
- {evolutionary_policy_optimization-0.2.22 → evolutionary_policy_optimization-0.3.1}/README.md +3 -2
- evolutionary_policy_optimization-0.3.1/evolutionary_policy_optimization/__init__.py +3 -0
- {evolutionary_policy_optimization-0.2.22 → evolutionary_policy_optimization-0.3.1}/evolutionary_policy_optimization/distributed.py +4 -5
- evolutionary_policy_optimization-0.3.1/evolutionary_policy_optimization/env_wrappers.py +71 -0
- {evolutionary_policy_optimization-0.2.22 → evolutionary_policy_optimization-0.3.1}/evolutionary_policy_optimization/epo.py +491 -158
- {evolutionary_policy_optimization-0.2.22 → evolutionary_policy_optimization-0.3.1}/evolutionary_policy_optimization/experimental.py +5 -5
- evolutionary_policy_optimization-0.3.1/evolutionary_policy_optimization/mock_env.py +128 -0
- {evolutionary_policy_optimization-0.2.22 → evolutionary_policy_optimization-0.3.1}/pyproject.toml +2 -3
- evolutionary_policy_optimization-0.2.22/evolutionary_policy_optimization/__init__.py +0 -15
- evolutionary_policy_optimization-0.2.22/evolutionary_policy_optimization/env_wrappers.py +0 -36
- evolutionary_policy_optimization-0.2.22/evolutionary_policy_optimization/mock_env.py +0 -55
- {evolutionary_policy_optimization-0.2.22 → evolutionary_policy_optimization-0.3.1}/.gitignore +0 -0
- {evolutionary_policy_optimization-0.2.22 → evolutionary_policy_optimization-0.3.1}/LICENSE +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: evolutionary-policy-optimization
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.1
|
|
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
|
|
@@ -35,12 +35,11 @@ Classifier: Programming Language :: Python :: 3.8
|
|
|
35
35
|
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
36
36
|
Requires-Python: >=3.9
|
|
37
37
|
Requires-Dist: accelerate>=1.6.0
|
|
38
|
-
Requires-Dist: adam-atan2-pytorch
|
|
39
38
|
Requires-Dist: assoc-scan>=0.0.2
|
|
40
39
|
Requires-Dist: einops>=0.8.1
|
|
41
40
|
Requires-Dist: einx>=0.3.0
|
|
42
41
|
Requires-Dist: ema-pytorch>=0.7.7
|
|
43
|
-
Requires-Dist: hl-gauss-pytorch>=0.
|
|
42
|
+
Requires-Dist: hl-gauss-pytorch>=0.2.8
|
|
44
43
|
Requires-Dist: torch>=2.2
|
|
45
44
|
Requires-Dist: tqdm
|
|
46
45
|
Requires-Dist: x-mlps-pytorch>=0.3.0
|
|
@@ -99,7 +98,8 @@ critic = Critic(dim_state = 32, dim = 256, mlp_depth = 3, dim_latent = 32)
|
|
|
99
98
|
|
|
100
99
|
latent = latent_pool(latent_id = 2)
|
|
101
100
|
|
|
102
|
-
|
|
101
|
+
action_distr = actor(state, latent)
|
|
102
|
+
actions = action_distr.sample()
|
|
103
103
|
value = critic(state, latent)
|
|
104
104
|
|
|
105
105
|
# interact with environment and receive rewards, termination etc
|
|
@@ -145,7 +145,7 @@ epo = EPO(
|
|
|
145
145
|
|
|
146
146
|
env = Env((512,))
|
|
147
147
|
|
|
148
|
-
epo(
|
|
148
|
+
epo(env, num_learning_cycles = 5)
|
|
149
149
|
|
|
150
150
|
# saving and loading
|
|
151
151
|
|
{evolutionary_policy_optimization-0.2.22 → evolutionary_policy_optimization-0.3.1}/README.md
RENAMED
|
@@ -37,7 +37,8 @@ critic = Critic(dim_state = 32, dim = 256, mlp_depth = 3, dim_latent = 32)
|
|
|
37
37
|
|
|
38
38
|
latent = latent_pool(latent_id = 2)
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
action_distr = actor(state, latent)
|
|
41
|
+
actions = action_distr.sample()
|
|
41
42
|
value = critic(state, latent)
|
|
42
43
|
|
|
43
44
|
# interact with environment and receive rewards, termination etc
|
|
@@ -83,7 +84,7 @@ epo = EPO(
|
|
|
83
84
|
|
|
84
85
|
env = Env((512,))
|
|
85
86
|
|
|
86
|
-
epo(
|
|
87
|
+
epo(env, num_learning_cycles = 5)
|
|
87
88
|
|
|
88
89
|
# saving and loading
|
|
89
90
|
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
from evolutionary_policy_optimization.env_wrappers import GymnasiumEnvWrapper, rescale_from_to
|
|
2
|
+
from evolutionary_policy_optimization.epo import EPO, MLP, Actor, Agent, BetaActionDistr, CategoricalActionDistr, Critic, LatentGenePool, create_agent
|
|
3
|
+
from evolutionary_policy_optimization.mock_env import Env, VectorEnv
|
|
@@ -1,12 +1,11 @@
|
|
|
1
|
+
import einx
|
|
1
2
|
import torch
|
|
2
|
-
|
|
3
|
+
import torch.distributed as dist
|
|
3
4
|
import torch.nn.functional as F
|
|
5
|
+
from einops import rearrange
|
|
6
|
+
from torch import nn
|
|
4
7
|
from torch.autograd import Function
|
|
5
8
|
|
|
6
|
-
import torch.distributed as dist
|
|
7
|
-
|
|
8
|
-
import einx
|
|
9
|
-
from einops import rearrange
|
|
10
9
|
|
|
11
10
|
def exists(val):
|
|
12
11
|
return val is not None
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
from math import prod
|
|
2
|
+
|
|
3
|
+
import torch
|
|
4
|
+
from torch.nn import Module
|
|
5
|
+
|
|
6
|
+
from evolutionary_policy_optimization.epo import Agent, create_agent, exists
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def rescale_from_to(x, from_range = (0., 1.), to_range = (-1., 1.)):
|
|
10
|
+
# e.g. beta actions on (0, 1) -> the env's action bounds
|
|
11
|
+
|
|
12
|
+
from_low, from_high = from_range
|
|
13
|
+
to_low, to_high = to_range
|
|
14
|
+
|
|
15
|
+
if torch.is_tensor(x):
|
|
16
|
+
dd_kwargs = dict(device = x.device, dtype = x.dtype)
|
|
17
|
+
from_low, from_high, to_low, to_high = [torch.as_tensor(t, **dd_kwargs) for t in (from_low, from_high, to_low, to_high)]
|
|
18
|
+
|
|
19
|
+
return to_low + (to_high - to_low) * (x - from_low) / (from_high - from_low)
|
|
20
|
+
|
|
21
|
+
class GymnasiumEnvWrapper(Module):
|
|
22
|
+
def __init__(
|
|
23
|
+
self,
|
|
24
|
+
env,
|
|
25
|
+
rescale_to = None # e.g. (-2., 2.) for pendulum's torque bounds
|
|
26
|
+
):
|
|
27
|
+
super().__init__()
|
|
28
|
+
self.env = env
|
|
29
|
+
|
|
30
|
+
if not exists(rescale_to) and not hasattr(env.action_space, 'n'):
|
|
31
|
+
rescale_to = (env.action_space.low, env.action_space.high)
|
|
32
|
+
|
|
33
|
+
self.rescale_to = rescale_to
|
|
34
|
+
|
|
35
|
+
def reset(self, *args, **kwargs):
|
|
36
|
+
return self.env.reset(*args, **kwargs)
|
|
37
|
+
|
|
38
|
+
def step(self, actions, *args, **kwargs):
|
|
39
|
+
# beta lives on (0, 1) - rescale to the env's bounds at the interface
|
|
40
|
+
|
|
41
|
+
if exists(self.rescale_to):
|
|
42
|
+
actions = rescale_from_to(actions, to_range = self.rescale_to)
|
|
43
|
+
|
|
44
|
+
return self.env.step(actions, *args, **kwargs)
|
|
45
|
+
|
|
46
|
+
def close(self, *args, **kwargs):
|
|
47
|
+
return self.env.close(*args, **kwargs)
|
|
48
|
+
|
|
49
|
+
def to_agent_hparams(self):
|
|
50
|
+
action_space = self.env.action_space
|
|
51
|
+
is_continuous = not hasattr(action_space, 'n')
|
|
52
|
+
|
|
53
|
+
num_actions = action_space.n if not is_continuous else prod(action_space.shape)
|
|
54
|
+
|
|
55
|
+
return dict(
|
|
56
|
+
dim_state = self.env.observation_space.shape[0],
|
|
57
|
+
actor_num_actions = num_actions,
|
|
58
|
+
action_is_continuous = is_continuous
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
def to_epo_agent(
|
|
62
|
+
self,
|
|
63
|
+
*args,
|
|
64
|
+
**kwargs
|
|
65
|
+
) -> Agent:
|
|
66
|
+
|
|
67
|
+
return create_agent(
|
|
68
|
+
*args,
|
|
69
|
+
**self.to_agent_hparams(),
|
|
70
|
+
**kwargs
|
|
71
|
+
)
|