mirror of
https://github.com/saymrwulf/stable-baselines3.git
synced 2026-11-20 10:03:35 +00:00
Convert format to f-strings
This commit is contained in:
parent
37f9f13684
commit
88f07bafb6
10 changed files with 30 additions and 33 deletions
|
|
@ -57,7 +57,7 @@ class BaseRLModel(ABC):
|
||||||
|
|
||||||
self.device = th.device(device)
|
self.device = th.device(device)
|
||||||
if verbose > 0:
|
if verbose > 0:
|
||||||
print("Using {} device".format(self.device))
|
print(f"Using {self.device} device")
|
||||||
|
|
||||||
self.env = env
|
self.env = env
|
||||||
# get VecNormalize object if needed
|
# get VecNormalize object if needed
|
||||||
|
|
@ -317,9 +317,8 @@ class BaseRLModel(ABC):
|
||||||
data, params, opt_params = cls._load_from_file(load_path)
|
data, params, opt_params = cls._load_from_file(load_path)
|
||||||
|
|
||||||
if 'policy_kwargs' in kwargs and kwargs['policy_kwargs'] != data['policy_kwargs']:
|
if 'policy_kwargs' in kwargs and kwargs['policy_kwargs'] != data['policy_kwargs']:
|
||||||
raise ValueError("The specified policy kwargs do not equal the stored policy kwargs."
|
raise ValueError(f"The specified policy kwargs do not equal the stored policy kwargs."
|
||||||
"Stored kwargs: {}, specified kwargs: {}".format(data['policy_kwargs'],
|
"Stored kwargs: {data['policy_kwargs']}, specified kwargs: {kwargs['policy_kwargs']}")
|
||||||
kwargs['policy_kwargs']))
|
|
||||||
|
|
||||||
# check if observation space and action space are part of the saved parameters
|
# check if observation space and action space are part of the saved parameters
|
||||||
if ("observation_space" not in data or "action_space" not in data) and "env" not in data:
|
if ("observation_space" not in data or "action_space" not in data) and "env" not in data:
|
||||||
|
|
@ -357,7 +356,7 @@ class BaseRLModel(ABC):
|
||||||
if os.path.exists(load_path + ".zip"):
|
if os.path.exists(load_path + ".zip"):
|
||||||
load_path += ".zip"
|
load_path += ".zip"
|
||||||
else:
|
else:
|
||||||
raise ValueError("Error: the file {} could not be found".format(load_path))
|
raise ValueError(f"Error: the file {load_path} could not be found")
|
||||||
|
|
||||||
# Open the zip archive and load data
|
# Open the zip archive and load data
|
||||||
try:
|
try:
|
||||||
|
|
@ -404,7 +403,7 @@ class BaseRLModel(ABC):
|
||||||
opt_params[os.path.splitext(file_path)[0]] = th.load(file_content)
|
opt_params[os.path.splitext(file_path)[0]] = th.load(file_content)
|
||||||
except zipfile.BadZipFile:
|
except zipfile.BadZipFile:
|
||||||
# load_path wasn't a zip file
|
# load_path wasn't a zip file
|
||||||
raise ValueError("Error: the file {} wasn't a zip-file".format(load_path))
|
raise ValueError(f"Error: the file {load_path} wasn't a zip-file")
|
||||||
|
|
||||||
return data, params, opt_params
|
return data, params, opt_params
|
||||||
|
|
||||||
|
|
@ -720,7 +719,7 @@ class BaseRLModel(ABC):
|
||||||
sync_envs_normalization(self.env, eval_env)
|
sync_envs_normalization(self.env, eval_env)
|
||||||
mean_reward, std_reward = evaluate_policy(self, eval_env, n_eval_episodes, deterministic=deterministic)
|
mean_reward, std_reward = evaluate_policy(self, eval_env, n_eval_episodes, deterministic=deterministic)
|
||||||
if self.verbose > 0:
|
if self.verbose > 0:
|
||||||
print("Eval num_timesteps={}, "
|
print(f"Eval num_timesteps={self.num_timesteps}, "
|
||||||
"episode_reward={:.2f} +/- {:.2f}".format(self.num_timesteps, mean_reward, std_reward))
|
"episode_reward={mean_reward:.2f} +/- {std_reward:.2f}")
|
||||||
print("FPS: {:.2f}".format(self.num_timesteps / (time.time() - self.start_time)))
|
print(f"FPS: {self.num_timesteps / (time.time() - self.start_time):.2f}")
|
||||||
return timesteps_since_eval
|
return timesteps_since_eval
|
||||||
|
|
|
||||||
|
|
@ -467,6 +467,5 @@ def make_proba_distribution(action_space, use_sde=False, dist_kwargs=None):
|
||||||
# elif isinstance(action_space, spaces.MultiBinary):
|
# elif isinstance(action_space, spaces.MultiBinary):
|
||||||
# return BernoulliDistribution(action_space.n, **dist_kwargs)
|
# return BernoulliDistribution(action_space.n, **dist_kwargs)
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError("Error: probability distribution, not implemented for action space of type {}."
|
raise NotImplementedError(f"Error: probability distribution, not implemented for action space of type {type(action_space)}."
|
||||||
.format(type(action_space)) +
|
|
||||||
" Must be of type Gym Spaces: Box, Discrete, MultiDiscrete or MultiBinary.")
|
" Must be of type Gym Spaces: Box, Discrete, MultiDiscrete or MultiBinary.")
|
||||||
|
|
|
||||||
|
|
@ -47,8 +47,8 @@ def evaluate_policy(model, env, n_eval_episodes=10, deterministic=True,
|
||||||
mean_reward = np.mean(episode_rewards)
|
mean_reward = np.mean(episode_rewards)
|
||||||
std_reward = np.std(episode_rewards)
|
std_reward = np.std(episode_rewards)
|
||||||
if reward_threshold is not None:
|
if reward_threshold is not None:
|
||||||
assert mean_reward > reward_threshold, 'Mean reward below threshold: '\
|
assert mean_reward > reward_threshold, (f'Mean reward below threshold: '
|
||||||
'{:.2f} < {:.2f}'.format(mean_reward, reward_threshold)
|
'{mean_reward:.2f} < {reward_threshold:.2f}')
|
||||||
if return_episode_rewards:
|
if return_episode_rewards:
|
||||||
return episode_rewards, n_steps
|
return episode_rewards, n_steps
|
||||||
return mean_reward, std_reward
|
return mean_reward, std_reward
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,7 @@ class HumanOutputFormat(KVWriter, SeqWriter):
|
||||||
self.file = open(filename_or_file, 'wt')
|
self.file = open(filename_or_file, 'wt')
|
||||||
self.own_file = True
|
self.own_file = True
|
||||||
else:
|
else:
|
||||||
assert hasattr(filename_or_file, 'write'), 'Expected file or str, got {}'.format(filename_or_file)
|
assert hasattr(filename_or_file, 'write'), f'Expected file or str, got {filename_or_file}'
|
||||||
self.file = filename_or_file
|
self.file = filename_or_file
|
||||||
self.own_file = False
|
self.own_file = False
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ class NormalActionNoise(ActionNoise):
|
||||||
return np.random.normal(self._mu, self._sigma)
|
return np.random.normal(self._mu, self._sigma)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return 'NormalActionNoise(mu={}, sigma={})'.format(self._mu, self._sigma)
|
return f'NormalActionNoise(mu={self._mu}, sigma={self._sigma})'
|
||||||
|
|
||||||
|
|
||||||
class OrnsteinUhlenbeckActionNoise(ActionNoise):
|
class OrnsteinUhlenbeckActionNoise(ActionNoise):
|
||||||
|
|
@ -68,4 +68,4 @@ class OrnsteinUhlenbeckActionNoise(ActionNoise):
|
||||||
self.noise_prev = self.initial_noise if self.initial_noise is not None else np.zeros_like(self._mu)
|
self.noise_prev = self.initial_noise if self.initial_noise is not None else np.zeros_like(self._mu)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return 'OrnsteinUhlenbeckActionNoise(mu={}, sigma={})'.format(self._mu, self._sigma)
|
return f'OrnsteinUhlenbeckActionNoise(mu={self._mu}, sigma={self._sigma})'
|
||||||
|
|
|
||||||
|
|
@ -150,10 +150,10 @@ def get_policy_from_name(base_policy_type, name):
|
||||||
:return: (base_policy_type) the policy
|
:return: (base_policy_type) the policy
|
||||||
"""
|
"""
|
||||||
if base_policy_type not in _policy_registry:
|
if base_policy_type not in _policy_registry:
|
||||||
raise ValueError("Error: the policy type {} is not registered!".format(base_policy_type))
|
raise ValueError(f"Error: the policy type {base_policy_type} is not registered!")
|
||||||
if name not in _policy_registry[base_policy_type]:
|
if name not in _policy_registry[base_policy_type]:
|
||||||
raise ValueError("Error: unknown policy type {}, the only registed policy type are: {}!"
|
raise ValueError(f"Error: unknown policy type {name},"
|
||||||
.format(name, list(_policy_registry[base_policy_type].keys())))
|
"the only registed policy type are: {list(_policy_registry[base_policy_type].keys())}!")
|
||||||
return _policy_registry[base_policy_type][name]
|
return _policy_registry[base_policy_type][name]
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -174,13 +174,12 @@ def register_policy(name, policy):
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
sub_class = str(th.random.randint(100))
|
sub_class = str(th.random.randint(100))
|
||||||
if sub_class is None:
|
if sub_class is None:
|
||||||
raise ValueError("Error: the policy {} is not of any known subclasses of BasePolicy!".format(policy))
|
raise ValueError(f"Error: the policy {policy} is not of any known subclasses of BasePolicy!")
|
||||||
|
|
||||||
if sub_class not in _policy_registry:
|
if sub_class not in _policy_registry:
|
||||||
_policy_registry[sub_class] = {}
|
_policy_registry[sub_class] = {}
|
||||||
if name in _policy_registry[sub_class]:
|
if name in _policy_registry[sub_class]:
|
||||||
raise ValueError("Error: the name {} is alreay registered for a different policy, will not override."
|
raise ValueError(f"Error: the name {name} is alreay registered for a different policy, will not override.")
|
||||||
.format(name))
|
|
||||||
_policy_registry[sub_class][name] = policy
|
_policy_registry[sub_class][name] = policy
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -123,7 +123,7 @@ def json_to_data(json_string, custom_objects=None):
|
||||||
)
|
)
|
||||||
except pickle.UnpicklingError:
|
except pickle.UnpicklingError:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
"Could not deserialize object {}. ".format(data_key) +
|
f"Could not deserialize object {data_key}. " +
|
||||||
"Consider using `custom_objects` argument to replace " +
|
"Consider using `custom_objects` argument to replace " +
|
||||||
"this object."
|
"this object."
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -161,7 +161,7 @@ class VecEnv(ABC):
|
||||||
:return: (str or None) name of module whose attribute is being shadowed, if any.
|
:return: (str or None) name of module whose attribute is being shadowed, if any.
|
||||||
"""
|
"""
|
||||||
if hasattr(self, name) and already_found:
|
if hasattr(self, name) and already_found:
|
||||||
return "{0}.{1}".format(type(self).__module__, type(self).__name__)
|
return f"{type(self).__module__}.{type(self).__name__}"
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
@ -230,10 +230,10 @@ class VecEnvWrapper(VecEnv):
|
||||||
"""
|
"""
|
||||||
blocked_class = self.getattr_depth_check(name, already_found=False)
|
blocked_class = self.getattr_depth_check(name, already_found=False)
|
||||||
if blocked_class is not None:
|
if blocked_class is not None:
|
||||||
own_class = "{0}.{1}".format(type(self).__module__, type(self).__name__)
|
own_class = f"{type(self).__module__}.{type(self).__name__}"
|
||||||
format_str = ("Error: Recursive attribute lookup for {0} from {1} is "
|
error_str = (f"Error: Recursive attribute lookup for {name} from {own_class} is "
|
||||||
"ambiguous and hides attribute from {2}")
|
"ambiguous and hides attribute from {blocked_class}")
|
||||||
raise AttributeError(format_str.format(name, own_class, blocked_class))
|
raise AttributeError(error_str)
|
||||||
|
|
||||||
return self.getattr_recursive(name)
|
return self.getattr_recursive(name)
|
||||||
|
|
||||||
|
|
@ -272,7 +272,7 @@ class VecEnvWrapper(VecEnv):
|
||||||
all_attributes = self._get_all_attributes()
|
all_attributes = self._get_all_attributes()
|
||||||
if name in all_attributes and already_found:
|
if name in all_attributes and already_found:
|
||||||
# this venv's attribute is being hidden because of a higher venv.
|
# this venv's attribute is being hidden because of a higher venv.
|
||||||
shadowed_wrapper_class = "{0}.{1}".format(type(self).__module__, type(self).__name__)
|
shadowed_wrapper_class = f"{type(self).__module__}.{type(self).__name__}"
|
||||||
elif name in all_attributes and not already_found:
|
elif name in all_attributes and not already_found:
|
||||||
# we have found the first reference to the attribute. Now check for duplicates.
|
# we have found the first reference to the attribute. Now check for duplicates.
|
||||||
shadowed_wrapper_class = self.venv.getattr_depth_check(name, True)
|
shadowed_wrapper_class = self.venv.getattr_depth_check(name, True)
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ def copy_obs_dict(obs):
|
||||||
:param obs: (OrderedDict<ndarray>): a dict of numpy arrays.
|
:param obs: (OrderedDict<ndarray>): a dict of numpy arrays.
|
||||||
:return (OrderedDict<ndarray>) a dict of copied numpy arrays.
|
:return (OrderedDict<ndarray>) a dict of copied numpy arrays.
|
||||||
"""
|
"""
|
||||||
assert isinstance(obs, OrderedDict), "unexpected type for observations '{}'".format(type(obs))
|
assert isinstance(obs, OrderedDict), f"unexpected type for observations '{type(obs)}'"
|
||||||
return OrderedDict([(k, np.copy(v)) for k, v in obs.items()])
|
return OrderedDict([(k, np.copy(v)) for k, v in obs.items()])
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -61,7 +61,7 @@ def obs_space_info(obs_space):
|
||||||
elif isinstance(obs_space, gym.spaces.Tuple):
|
elif isinstance(obs_space, gym.spaces.Tuple):
|
||||||
subspaces = {i: space for i, space in enumerate(obs_space.spaces)}
|
subspaces = {i: space for i, space in enumerate(obs_space.spaces)}
|
||||||
else:
|
else:
|
||||||
assert not hasattr(obs_space, 'spaces'), "Unsupported structured space '{}'".format(type(obs_space))
|
assert not hasattr(obs_space, 'spaces'), f"Unsupported structured space '{type(obs_space)}'"
|
||||||
subspaces = {None: obs_space}
|
subspaces = {None: obs_space}
|
||||||
keys = []
|
keys = []
|
||||||
shapes = {}
|
shapes = {}
|
||||||
|
|
|
||||||
|
|
@ -182,7 +182,7 @@ class VecNormalize(VecEnvWrapper):
|
||||||
:param path: (str) path to log dir
|
:param path: (str) path to log dir
|
||||||
"""
|
"""
|
||||||
for rms, name in zip([self.obs_rms, self.ret_rms], ['obs_rms', 'ret_rms']):
|
for rms, name in zip([self.obs_rms, self.ret_rms], ['obs_rms', 'ret_rms']):
|
||||||
with open("{}/{}.pkl".format(path, name), 'wb') as file_handler:
|
with open(f"{path}/{name}.pkl", 'wb') as file_handler:
|
||||||
pickle.dump(rms, file_handler)
|
pickle.dump(rms, file_handler)
|
||||||
|
|
||||||
def load_running_average(self, path):
|
def load_running_average(self, path):
|
||||||
|
|
@ -190,5 +190,5 @@ class VecNormalize(VecEnvWrapper):
|
||||||
:param path: (str) path to log dir
|
:param path: (str) path to log dir
|
||||||
"""
|
"""
|
||||||
for name in ['obs_rms', 'ret_rms']:
|
for name in ['obs_rms', 'ret_rms']:
|
||||||
with open("{}/{}.pkl".format(path, name), 'rb') as file_handler:
|
with open(f"{path}/{name}.pkl", 'rb') as file_handler:
|
||||||
setattr(self, name, pickle.load(file_handler))
|
setattr(self, name, pickle.load(file_handler))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue