Sync predict with SB and add version file

This commit is contained in:
Antonin RAFFIN 2020-03-18 15:11:19 +01:00
parent 7f7b288dce
commit 9485b90a41
8 changed files with 27 additions and 17 deletions

View file

@ -11,6 +11,7 @@ Breaking Changes:
^^^^^^^^^^^^^^^^^
- Removed default seed
- Bump dependencies (PyTorch and Gym)
- ``predict()`` now returns a tuple to match Stable-Baselines behavior
New Features:
^^^^^^^^^^^^^
@ -32,6 +33,7 @@ Others:
- More typing
- Add test for ``expln``
- Renamed ``learning_rate`` to ``lr_schedule``
- Add ``version.txt``
Documentation:
^^^^^^^^^^^^^^

View file

@ -1,7 +1,11 @@
import os
import sys
import subprocess
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',
packages=[package for package in find_packages()
@ -48,7 +52,7 @@ setup(name='torchy_baselines',
license="MIT",
long_description="",
long_description_content_type='text/markdown',
version="0.2.4",
version=__version__,
)
# python setup.py sdist

View file

@ -34,10 +34,10 @@ def test_predict(model_class, env_id):
vec_env = DummyVecEnv([lambda: gym.make(env_id), lambda: gym.make(env_id)])
obs = env.reset()
action = model.predict(obs)
action, _ = model.predict(obs)
assert action.shape == env.action_space.shape
assert env.action_space.contains(action)
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]

View file

@ -54,7 +54,7 @@ def test_save_load(model_class):
params = new_params
# get selected actions
selected_actions = model.predict(observations, deterministic=True)
selected_actions, _ = model.predict(observations, deterministic=True)
# Check
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."
# 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)
# check if learn still works

View file

@ -1,7 +1,12 @@
import os
from torchy_baselines.a2c import A2C
from torchy_baselines.cem_rl import CEMRL
from torchy_baselines.ppo import PPO
from torchy_baselines.sac import SAC
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()

View file

@ -369,7 +369,7 @@ class BaseRLModel(ABC):
def predict(self, observation: np.ndarray,
state: 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
@ -377,7 +377,7 @@ class BaseRLModel(ABC):
: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 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:
# state = self.initial_state
@ -409,9 +409,7 @@ class BaseRLModel(ABC):
raise ValueError("Error: The environment must be vectorized when using recurrent policies.")
clipped_actions = clipped_actions[0]
# TODO: switch to stable baselines API
# return clipped_actions, state
return clipped_actions
return clipped_actions, state
@classmethod
def load(cls, load_path: str, env: Optional[GymEnv] = None, **kwargs):
@ -896,7 +894,7 @@ class OffPolicyRLModel(BaseRLModel):
else:
# 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
unscaled_action = self.predict(obs, deterministic=False)
unscaled_action, _ = self.predict(obs, deterministic=False)
# Rescale the action from [low, high] to [-1, 1]
scaled_action = self.scale_action(unscaled_action)

View file

@ -8,11 +8,11 @@ def evaluate_policy(model, env, n_eval_episodes=10, deterministic=True,
render=False, callback=None, reward_threshold=None,
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.
: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.
:param n_eval_episodes: (int) Number of episode to evaluate the agent
: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
will be returned instead of the mean.
: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):
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 = [], []
for _ in range(n_eval_episodes):
obs = env.reset()
done = False
done, state = False, None
episode_reward = 0.0
episode_length = 0
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)
episode_reward += reward
if callback is not None:

View file

@ -0,0 +1 @@
0.2.5