mirror of
https://github.com/saymrwulf/stable-baselines3.git
synced 2026-11-18 09:54:10 +00:00
Sync predict with SB and add version file
This commit is contained in:
parent
7f7b288dce
commit
9485b90a41
8 changed files with 27 additions and 17 deletions
|
|
@ -11,6 +11,7 @@ Breaking Changes:
|
||||||
^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^
|
||||||
- Removed default seed
|
- Removed default seed
|
||||||
- Bump dependencies (PyTorch and Gym)
|
- Bump dependencies (PyTorch and Gym)
|
||||||
|
- ``predict()`` now returns a tuple to match Stable-Baselines behavior
|
||||||
|
|
||||||
New Features:
|
New Features:
|
||||||
^^^^^^^^^^^^^
|
^^^^^^^^^^^^^
|
||||||
|
|
@ -32,6 +33,7 @@ Others:
|
||||||
- More typing
|
- More typing
|
||||||
- Add test for ``expln``
|
- Add test for ``expln``
|
||||||
- Renamed ``learning_rate`` to ``lr_schedule``
|
- Renamed ``learning_rate`` to ``lr_schedule``
|
||||||
|
- Add ``version.txt``
|
||||||
|
|
||||||
Documentation:
|
Documentation:
|
||||||
^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
6
setup.py
6
setup.py
|
|
@ -1,7 +1,11 @@
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
import subprocess
|
import subprocess
|
||||||
from setuptools import setup, find_packages
|
from setuptools import setup, find_packages
|
||||||
|
|
||||||
|
with open(os.path.join('torchy_baselines', 'version.txt'), 'r') as file_handler:
|
||||||
|
__version__ = file_handler.read()
|
||||||
|
|
||||||
|
|
||||||
setup(name='torchy_baselines',
|
setup(name='torchy_baselines',
|
||||||
packages=[package for package in find_packages()
|
packages=[package for package in find_packages()
|
||||||
|
|
@ -48,7 +52,7 @@ setup(name='torchy_baselines',
|
||||||
license="MIT",
|
license="MIT",
|
||||||
long_description="",
|
long_description="",
|
||||||
long_description_content_type='text/markdown',
|
long_description_content_type='text/markdown',
|
||||||
version="0.2.4",
|
version=__version__,
|
||||||
)
|
)
|
||||||
|
|
||||||
# python setup.py sdist
|
# python setup.py sdist
|
||||||
|
|
|
||||||
|
|
@ -34,10 +34,10 @@ def test_predict(model_class, env_id):
|
||||||
vec_env = DummyVecEnv([lambda: gym.make(env_id), lambda: gym.make(env_id)])
|
vec_env = DummyVecEnv([lambda: gym.make(env_id), lambda: gym.make(env_id)])
|
||||||
|
|
||||||
obs = env.reset()
|
obs = env.reset()
|
||||||
action = model.predict(obs)
|
action, _ = model.predict(obs)
|
||||||
assert action.shape == env.action_space.shape
|
assert action.shape == env.action_space.shape
|
||||||
assert env.action_space.contains(action)
|
assert env.action_space.contains(action)
|
||||||
|
|
||||||
vec_env_obs = vec_env.reset()
|
vec_env_obs = vec_env.reset()
|
||||||
action = model.predict(vec_env_obs)
|
action, _ = model.predict(vec_env_obs)
|
||||||
assert action.shape[0] == vec_env_obs.shape[0]
|
assert action.shape[0] == vec_env_obs.shape[0]
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,7 @@ def test_save_load(model_class):
|
||||||
params = new_params
|
params = new_params
|
||||||
|
|
||||||
# get selected actions
|
# get selected actions
|
||||||
selected_actions = model.predict(observations, deterministic=True)
|
selected_actions, _ = model.predict(observations, deterministic=True)
|
||||||
|
|
||||||
# Check
|
# Check
|
||||||
model.save("test_save.zip")
|
model.save("test_save.zip")
|
||||||
|
|
@ -69,7 +69,7 @@ def test_save_load(model_class):
|
||||||
assert th.allclose(params[key], new_params[key]), "Model parameters not the same after save and load."
|
assert th.allclose(params[key], new_params[key]), "Model parameters not the same after save and load."
|
||||||
|
|
||||||
# check if model still selects the same actions
|
# check if model still selects the same actions
|
||||||
new_selected_actions = model.predict(observations, deterministic=True)
|
new_selected_actions, _ = model.predict(observations, deterministic=True)
|
||||||
assert np.allclose(selected_actions, new_selected_actions, 1e-4)
|
assert np.allclose(selected_actions, new_selected_actions, 1e-4)
|
||||||
|
|
||||||
# check if learn still works
|
# check if learn still works
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,12 @@
|
||||||
|
import os
|
||||||
|
|
||||||
from torchy_baselines.a2c import A2C
|
from torchy_baselines.a2c import A2C
|
||||||
from torchy_baselines.cem_rl import CEMRL
|
from torchy_baselines.cem_rl import CEMRL
|
||||||
from torchy_baselines.ppo import PPO
|
from torchy_baselines.ppo import PPO
|
||||||
from torchy_baselines.sac import SAC
|
from torchy_baselines.sac import SAC
|
||||||
from torchy_baselines.td3 import TD3
|
from torchy_baselines.td3 import TD3
|
||||||
|
|
||||||
__version__ = "0.2.4"
|
# Read version from file
|
||||||
|
version_file = os.path.join(os.path.dirname(__file__), 'version.txt')
|
||||||
|
with open(version_file, 'r') as file_handler:
|
||||||
|
__version__ = file_handler.read()
|
||||||
|
|
|
||||||
|
|
@ -369,7 +369,7 @@ class BaseRLModel(ABC):
|
||||||
def predict(self, observation: np.ndarray,
|
def predict(self, observation: np.ndarray,
|
||||||
state: Optional[np.ndarray] = None,
|
state: Optional[np.ndarray] = None,
|
||||||
mask: Optional[np.ndarray] = None,
|
mask: Optional[np.ndarray] = None,
|
||||||
deterministic: bool = False) -> np.ndarray:
|
deterministic: bool = False) -> Tuple[np.ndarray, Optional[np.ndarray]]:
|
||||||
"""
|
"""
|
||||||
Get the model's action(s) from an observation
|
Get the model's action(s) from an observation
|
||||||
|
|
||||||
|
|
@ -377,7 +377,7 @@ class BaseRLModel(ABC):
|
||||||
:param state: (Optional[np.ndarray]) The last states (can be None, used in recurrent policies)
|
:param state: (Optional[np.ndarray]) The last states (can be None, used in recurrent policies)
|
||||||
:param mask: (Optional[np.ndarray]) The last masks (can be None, used in recurrent policies)
|
:param mask: (Optional[np.ndarray]) The last masks (can be None, used in recurrent policies)
|
||||||
:param deterministic: (bool) Whether or not to return deterministic actions.
|
:param deterministic: (bool) Whether or not to return deterministic actions.
|
||||||
:return: (np.ndarray) the model's action and the next state (used in recurrent policies)
|
:return: (Tuple[np.ndarray, Optional[np.ndarray]]) the model's action and the next state (used in recurrent policies)
|
||||||
"""
|
"""
|
||||||
# if state is None:
|
# if state is None:
|
||||||
# state = self.initial_state
|
# state = self.initial_state
|
||||||
|
|
@ -409,9 +409,7 @@ class BaseRLModel(ABC):
|
||||||
raise ValueError("Error: The environment must be vectorized when using recurrent policies.")
|
raise ValueError("Error: The environment must be vectorized when using recurrent policies.")
|
||||||
clipped_actions = clipped_actions[0]
|
clipped_actions = clipped_actions[0]
|
||||||
|
|
||||||
# TODO: switch to stable baselines API
|
return clipped_actions, state
|
||||||
# return clipped_actions, state
|
|
||||||
return clipped_actions
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def load(cls, load_path: str, env: Optional[GymEnv] = None, **kwargs):
|
def load(cls, load_path: str, env: Optional[GymEnv] = None, **kwargs):
|
||||||
|
|
@ -896,7 +894,7 @@ class OffPolicyRLModel(BaseRLModel):
|
||||||
else:
|
else:
|
||||||
# Note: we assume that the policy uses tanh to scale the action
|
# Note: we assume that the policy uses tanh to scale the action
|
||||||
# We use non-deterministic action in the case of SAC, for TD3, it does not matter
|
# We use non-deterministic action in the case of SAC, for TD3, it does not matter
|
||||||
unscaled_action = self.predict(obs, deterministic=False)
|
unscaled_action, _ = self.predict(obs, deterministic=False)
|
||||||
|
|
||||||
# Rescale the action from [low, high] to [-1, 1]
|
# Rescale the action from [low, high] to [-1, 1]
|
||||||
scaled_action = self.scale_action(unscaled_action)
|
scaled_action = self.scale_action(unscaled_action)
|
||||||
|
|
|
||||||
|
|
@ -8,11 +8,11 @@ def evaluate_policy(model, env, n_eval_episodes=10, deterministic=True,
|
||||||
render=False, callback=None, reward_threshold=None,
|
render=False, callback=None, reward_threshold=None,
|
||||||
return_episode_rewards=False):
|
return_episode_rewards=False):
|
||||||
"""
|
"""
|
||||||
Runs policy for `n_eval_episodes` episodes and returns average reward.
|
Runs policy for ``n_eval_episodes`` episodes and returns average reward.
|
||||||
This is made to work only with one env.
|
This is made to work only with one env.
|
||||||
|
|
||||||
:param model: (BaseRLModel) The RL agent you want to evaluate.
|
:param model: (BaseRLModel) The RL agent you want to evaluate.
|
||||||
:param env: (gym.Env or VecEnv) The gym environment. In the case of a `VecEnv`
|
:param env: (gym.Env or VecEnv) The gym environment. In the case of a ``VecEnv``
|
||||||
this must contain only one environment.
|
this must contain only one environment.
|
||||||
:param n_eval_episodes: (int) Number of episode to evaluate the agent
|
:param n_eval_episodes: (int) Number of episode to evaluate the agent
|
||||||
:param deterministic: (bool) Whether to use deterministic or stochastic actions
|
:param deterministic: (bool) Whether to use deterministic or stochastic actions
|
||||||
|
|
@ -24,7 +24,7 @@ def evaluate_policy(model, env, n_eval_episodes=10, deterministic=True,
|
||||||
:param return_episode_rewards: (bool) If True, a list of reward per episode
|
:param return_episode_rewards: (bool) If True, a list of reward per episode
|
||||||
will be returned instead of the mean.
|
will be returned instead of the mean.
|
||||||
:return: (float, float) Mean reward per episode, std of reward per episode
|
:return: (float, float) Mean reward per episode, std of reward per episode
|
||||||
returns ([float], [int]) when `return_episode_rewards` is True
|
returns ([float], [int]) when ``return_episode_rewards`` is True
|
||||||
"""
|
"""
|
||||||
if isinstance(env, VecEnv):
|
if isinstance(env, VecEnv):
|
||||||
assert env.num_envs == 1, "You must pass only one environment when using this function"
|
assert env.num_envs == 1, "You must pass only one environment when using this function"
|
||||||
|
|
@ -32,11 +32,11 @@ def evaluate_policy(model, env, n_eval_episodes=10, deterministic=True,
|
||||||
episode_rewards, episode_lengths = [], []
|
episode_rewards, episode_lengths = [], []
|
||||||
for _ in range(n_eval_episodes):
|
for _ in range(n_eval_episodes):
|
||||||
obs = env.reset()
|
obs = env.reset()
|
||||||
done = False
|
done, state = False, None
|
||||||
episode_reward = 0.0
|
episode_reward = 0.0
|
||||||
episode_length = 0
|
episode_length = 0
|
||||||
while not done:
|
while not done:
|
||||||
action = model.predict(obs, deterministic=deterministic)
|
action, state = model.predict(obs, state=state, deterministic=deterministic)
|
||||||
obs, reward, done, _info = env.step(action)
|
obs, reward, done, _info = env.step(action)
|
||||||
episode_reward += reward
|
episode_reward += reward
|
||||||
if callback is not None:
|
if callback is not None:
|
||||||
|
|
|
||||||
1
torchy_baselines/version.txt
Normal file
1
torchy_baselines/version.txt
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
0.2.5
|
||||||
Loading…
Reference in a new issue