Miscellaneous Utilities

keras_gym.utils.enable_logging Enable logging output.
keras_gym.utils.generate_gif Store a gif from the episode frames.
keras_gym.utils.get_env_attr Get the given attribute from a potentially wrapped environment.
keras_gym.utils.get_transition Generate a transition from the environment.
keras_gym.utils.has_env_attr Check if a potentially wrapped environment has a given attribute.
keras_gym.utils.is_policy Check whether an object is an (updateable) policy.
keras_gym.utils.is_qfunction Check whether an object is a state-action value function, or Q-function.
keras_gym.utils.is_vfunction Check whether an object is a state value function, or V-function.
keras_gym.utils.render_episode Run a single episode with env.render() calls with each time step.
keras_gym.utils.set_tf_loglevel Set the logging level for Tensorflow logger.
keras_gym.utils.enable_logging(level=20, level_tf=40)[source]

Enable logging output.

This executes the following two lines of code:

import logging
logging.basicConfig(level=logging.INFO)
set_tf_loglevel(logging.ERROR)

Note that set_tf_loglevel() is another keras-gym utility function.

Parameters:
level : int, optional

Log level for native python logging. For instance, if you’d like to see more verbose logging messages you might set level=logging.DEBUG.

level_tf : int, optional

Log level for tensorflow-specific logging (logs coming from the C++ layer).

keras_gym.utils.generate_gif(env, policy, filepath, resize_to=None, duration=50)[source]

Store a gif from the episode frames.

Parameters:
env : gym environment

The environment to record from.

policy : keras-gym policy object

The policy that is used to take actions.

filepath : str

Location of the output gif file.

resize_to : tuple of ints, optional

The size of the output frames, (width, height). Notice the ordering: first width, then height. This is the convention PIL uses.

duration : float, optional

Time between frames in the animated gif, in milliseconds.

keras_gym.utils.get_env_attr(env, attr, default='__ERROR__', max_depth=100)[source]

Get the given attribute from a potentially wrapped environment.

Note that the wrapped envs are traversed from the outside in. Once the attribute is found, the search stops. This means that an inner wrapped env may carry the same (possibly conflicting) attribute. This situation is not resolved by this function.

Parameters:
env : gym environment

A potentially wrapped environment.

attr : str

The attribute name.

max_depth : positive int, optional

The maximum depth of wrappers to traverse.

keras_gym.utils.get_transition(env)[source]

Generate a transition from the environment.

This basically does a single step on the environment and then closes it.

Parameters:
env : gym environment

A gym environment.

Returns:
s, a, r, s_next, a_next, done, info : tuple

A single transition. Note that the order and the number of items returned is different from what env.reset() return.

keras_gym.utils.has_env_attr(env, attr, max_depth=100)[source]

Check if a potentially wrapped environment has a given attribute.

Parameters:
env : gym environment

A potentially wrapped environment.

attr : str

The attribute name.

max_depth : positive int, optional

The maximum depth of wrappers to traverse.

keras_gym.utils.is_policy(obj, check_updateable=False)[source]

Check whether an object is an (updateable) policy.

Parameters:
obj

Object to check.

check_updateable : bool, optional

If the obj is a policy, also check whether or not the policy is updateable.

Returns:
bool

Whether obj is a (updateable) policy.

keras_gym.utils.is_qfunction(obj, qtype=None)[source]

Check whether an object is a state-action value function, or Q-function.

Parameters:
obj

Object to check.

qtype : 1 or 2, optional

Check for specific Q-function type, i.e. type-I or type-II.

Returns:
bool

Whether obj is a (type-I/II) Q-function.

keras_gym.utils.is_vfunction(obj)[source]

Check whether an object is a state value function, or V-function.

Parameters:
obj

Object to check.

Returns:
bool

Whether obj is a V-function.

keras_gym.utils.render_episode(env, policy, step_delay_ms=0)[source]

Run a single episode with env.render() calls with each time step.

Parameters:
env : gym environment

A gym environment.

policy : callable

A policy objects that is used to pick actions: a = policy(s).

step_delay_ms : non-negative float

The number of milliseconds to wait between consecutive timesteps. This can be used to slow down the rendering.

keras_gym.utils.set_tf_loglevel(level)[source]

Set the logging level for Tensorflow logger. This also sets the logging level of the underlying C++ layer.

Parameters:
level : int

A logging level as provided by the builtin logging module, e.g. level=logging.INFO.