From 9485b90a411a2b85879ed04eecee5f5b5f38c226 Mon Sep 17 00:00:00 2001 From: Antonin RAFFIN Date: Wed, 18 Mar 2020 15:11:19 +0100 Subject: [PATCH] Sync predict with SB and add version file --- docs/misc/changelog.rst | 2 ++ setup.py | 6 +++++- tests/test_predict.py | 4 ++-- tests/test_save_load.py | 4 ++-- torchy_baselines/__init__.py | 7 ++++++- torchy_baselines/common/base_class.py | 10 ++++------ torchy_baselines/common/evaluation.py | 10 +++++----- torchy_baselines/version.txt | 1 + 8 files changed, 27 insertions(+), 17 deletions(-) create mode 100644 torchy_baselines/version.txt diff --git a/docs/misc/changelog.rst b/docs/misc/changelog.rst index 5c8f55c..9332168 100644 --- a/docs/misc/changelog.rst +++ b/docs/misc/changelog.rst @@ -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: ^^^^^^^^^^^^^^ diff --git a/setup.py b/setup.py index 21fedaa..ee0d0d6 100644 --- a/setup.py +++ b/setup.py @@ -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 diff --git a/tests/test_predict.py b/tests/test_predict.py index e68954f..2ffab6b 100644 --- a/tests/test_predict.py +++ b/tests/test_predict.py @@ -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] diff --git a/tests/test_save_load.py b/tests/test_save_load.py index bdec3ed..9a0dad8 100644 --- a/tests/test_save_load.py +++ b/tests/test_save_load.py @@ -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 diff --git a/torchy_baselines/__init__.py b/torchy_baselines/__init__.py index b228acb..acf5912 100644 --- a/torchy_baselines/__init__.py +++ b/torchy_baselines/__init__.py @@ -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() diff --git a/torchy_baselines/common/base_class.py b/torchy_baselines/common/base_class.py index eabe92e..cd9eb77 100644 --- a/torchy_baselines/common/base_class.py +++ b/torchy_baselines/common/base_class.py @@ -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) diff --git a/torchy_baselines/common/evaluation.py b/torchy_baselines/common/evaluation.py index ff9fa13..704057c 100644 --- a/torchy_baselines/common/evaluation.py +++ b/torchy_baselines/common/evaluation.py @@ -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: diff --git a/torchy_baselines/version.txt b/torchy_baselines/version.txt new file mode 100644 index 0000000..3a4036f --- /dev/null +++ b/torchy_baselines/version.txt @@ -0,0 +1 @@ +0.2.5