code stringlengths 66 870k | docstring stringlengths 19 26.7k | func_name stringlengths 1 138 | language stringclasses 1
value | repo stringlengths 7 68 | path stringlengths 5 324 | url stringlengths 46 389 | license stringclasses 7
values |
|---|---|---|---|---|---|---|---|
def __init__(
self,
env,
policy,
observers=None,
transition_observers=None,
num_episodes=1,
):
"""Creates a DynamicEpisodeDriver.
**Note** about bias when using batched environments with `num_episodes`:
When using `num_episodes != None`, a `run` step "finishes" when
... | Creates a DynamicEpisodeDriver.
**Note** about bias when using batched environments with `num_episodes`:
When using `num_episodes != None`, a `run` step "finishes" when
`num_episodes` have been completely collected (hit a boundary).
When used in conjunction with environments that have variable-length
... | __init__ | python | tensorflow/agents | tf_agents/drivers/dynamic_episode_driver.py | https://github.com/tensorflow/agents/blob/master/tf_agents/drivers/dynamic_episode_driver.py | Apache-2.0 |
def _loop_condition_fn(self, num_episodes):
"""Returns a function with the condition needed for tf.while_loop."""
def loop_cond(counter, *_):
"""Determines when to stop the loop, based on episode counter.
Args:
counter: Episode counters per batch index. Shape [batch_size] when
ba... | Returns a function with the condition needed for tf.while_loop. | _loop_condition_fn | python | tensorflow/agents | tf_agents/drivers/dynamic_episode_driver.py | https://github.com/tensorflow/agents/blob/master/tf_agents/drivers/dynamic_episode_driver.py | Apache-2.0 |
def _loop_body_fn(self):
"""Returns a function with the driver's loop body ops."""
def loop_body(counter, time_step, policy_state):
"""Runs a step in environment.
While loop will call multiple times.
Args:
counter: Episode counters per batch index. Shape [batch_size].
time_s... | Returns a function with the driver's loop body ops. | _loop_body_fn | python | tensorflow/agents | tf_agents/drivers/dynamic_episode_driver.py | https://github.com/tensorflow/agents/blob/master/tf_agents/drivers/dynamic_episode_driver.py | Apache-2.0 |
def loop_body(counter, time_step, policy_state):
"""Runs a step in environment.
While loop will call multiple times.
Args:
counter: Episode counters per batch index. Shape [batch_size].
time_step: TimeStep tuple with elements shape [batch_size, ...].
policy_state: Poicy state... | Runs a step in environment.
While loop will call multiple times.
Args:
counter: Episode counters per batch index. Shape [batch_size].
time_step: TimeStep tuple with elements shape [batch_size, ...].
policy_state: Poicy state tensor shape [batch_size, policy_state_dim].
Pa... | loop_body | python | tensorflow/agents | tf_agents/drivers/dynamic_episode_driver.py | https://github.com/tensorflow/agents/blob/master/tf_agents/drivers/dynamic_episode_driver.py | Apache-2.0 |
def run(
self,
time_step=None,
policy_state=None,
num_episodes=None,
maximum_iterations=None,
):
"""Takes episodes in the environment using the policy and update observers.
If `time_step` and `policy_state` are not provided, `run` will reset the
environment and request an in... | Takes episodes in the environment using the policy and update observers.
If `time_step` and `policy_state` are not provided, `run` will reset the
environment and request an initial state from the policy.
**Note** about bias when using batched environments with `num_episodes`:
When using `num_episodes ... | run | python | tensorflow/agents | tf_agents/drivers/dynamic_episode_driver.py | https://github.com/tensorflow/agents/blob/master/tf_agents/drivers/dynamic_episode_driver.py | Apache-2.0 |
def __init__(
self,
env,
policy,
observers=None,
transition_observers=None,
num_steps=1,
):
"""Creates a DynamicStepDriver.
Args:
env: A tf_environment.Base environment.
policy: A tf_policy.TFPolicy policy.
observers: A list of observers that are updated ... | Creates a DynamicStepDriver.
Args:
env: A tf_environment.Base environment.
policy: A tf_policy.TFPolicy policy.
observers: A list of observers that are updated after every step in the
environment. Each observer is a callable(time_step.Trajectory).
transition_observers: A list of obs... | __init__ | python | tensorflow/agents | tf_agents/drivers/dynamic_step_driver.py | https://github.com/tensorflow/agents/blob/master/tf_agents/drivers/dynamic_step_driver.py | Apache-2.0 |
def loop_body(counter, time_step, policy_state):
"""Runs a step in environment.
While loop will call multiple times.
Args:
counter: Step counters per batch index. Shape [batch_size].
time_step: TimeStep tuple with elements shape [batch_size, ...].
policy_state: Policy state t... | Runs a step in environment.
While loop will call multiple times.
Args:
counter: Step counters per batch index. Shape [batch_size].
time_step: TimeStep tuple with elements shape [batch_size, ...].
policy_state: Policy state tensor shape [batch_size, policy_state_dim].
Pass... | loop_body | python | tensorflow/agents | tf_agents/drivers/dynamic_step_driver.py | https://github.com/tensorflow/agents/blob/master/tf_agents/drivers/dynamic_step_driver.py | Apache-2.0 |
def run(self, time_step=None, policy_state=None, maximum_iterations=None):
"""Takes steps in the environment using the policy while updating observers.
Args:
time_step: optional initial time_step. If None, it will use the
current_time_step of the environment. Elements should be shape
[bat... | Takes steps in the environment using the policy while updating observers.
Args:
time_step: optional initial time_step. If None, it will use the
current_time_step of the environment. Elements should be shape
[batch_size, ...].
policy_state: optional initial state for the policy.
ma... | run | python | tensorflow/agents | tf_agents/drivers/dynamic_step_driver.py | https://github.com/tensorflow/agents/blob/master/tf_agents/drivers/dynamic_step_driver.py | Apache-2.0 |
def __init__(
self,
env: py_environment.PyEnvironment,
policy: py_policy.PyPolicy,
observers: Sequence[Callable[[trajectory.Trajectory], Any]],
transition_observers: Optional[
Sequence[Callable[[trajectory.Transition], Any]]
] = None,
info_observers: Optional[Sequence... | A driver that runs a python policy in a python environment.
**Note** about bias when using batched environments with `max_episodes`:
When using `max_episodes != None`, a `run` step "finishes" when
`max_episodes` have been completely collected (hit a boundary).
When used in conjunction with environments... | __init__ | python | tensorflow/agents | tf_agents/drivers/py_driver.py | https://github.com/tensorflow/agents/blob/master/tf_agents/drivers/py_driver.py | Apache-2.0 |
def run( # pytype: disable=signature-mismatch # overriding-parameter-count-checks
self, time_step: ts.TimeStep, policy_state: types.NestedArray = ()
) -> Tuple[ts.TimeStep, types.NestedArray]:
"""Run policy in environment given initial time_step and policy_state.
Args:
time_step: The initial ti... | Run policy in environment given initial time_step and policy_state.
Args:
time_step: The initial time_step.
policy_state: The initial policy_state.
Returns:
A tuple (final time_step, final policy_state).
| run | python | tensorflow/agents | tf_agents/drivers/py_driver.py | https://github.com/tensorflow/agents/blob/master/tf_agents/drivers/py_driver.py | Apache-2.0 |
def make_random_trajectory():
"""Creates a random trajectory.
This trajectory contains Tensors shaped `[1, 6, ...]` where `1` is the batch
and `6` is the number of time steps.
Observations are unbounded but actions are bounded to take values within
`[1, 2]`.
Policy info is also provided, and is equal to ... | Creates a random trajectory.
This trajectory contains Tensors shaped `[1, 6, ...]` where `1` is the batch
and `6` is the number of time steps.
Observations are unbounded but actions are bounded to take values within
`[1, 2]`.
Policy info is also provided, and is equal to the actions. It can be removed
v... | make_random_trajectory | python | tensorflow/agents | tf_agents/drivers/test_utils.py | https://github.com/tensorflow/agents/blob/master/tf_agents/drivers/test_utils.py | Apache-2.0 |
def __init__(
self,
env: tf_environment.TFEnvironment,
policy: tf_policy.TFPolicy,
observers: Sequence[Callable[[trajectory.Trajectory], Any]],
transition_observers: Optional[
Sequence[Callable[[trajectory.Transition], Any]]
] = None,
max_steps: Optional[types.Int] = ... | A driver that runs a TF policy in a TF environment.
**Note** about bias when using batched environments with `max_episodes`:
When using `max_episodes != None`, a `run` step "finishes" when
`max_episodes` have been completely collected (hit a boundary).
When used in conjunction with environments that ha... | __init__ | python | tensorflow/agents | tf_agents/drivers/tf_driver.py | https://github.com/tensorflow/agents/blob/master/tf_agents/drivers/tf_driver.py | Apache-2.0 |
def __init__(
self,
env: gym.Env,
frame_skip: int = 4,
terminal_on_life_loss: bool = False,
screen_size: int = 84,
):
"""Constructor for an Atari 2600 preprocessor.
Args:
env: Gym environment whose observations are preprocessed.
frame_skip: int, the frequency at whic... | Constructor for an Atari 2600 preprocessor.
Args:
env: Gym environment whose observations are preprocessed.
frame_skip: int, the frequency at which the agent experiences the game.
terminal_on_life_loss: bool, If True, the step() method returns
is_terminal=True whenever a life is lost. See... | __init__ | python | tensorflow/agents | tf_agents/environments/atari_preprocessing.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/atari_preprocessing.py | Apache-2.0 |
def step(self, action: np.ndarray) -> np.ndarray:
"""Applies the given action in the environment.
Remarks:
* If a terminal state (from life loss or episode end) is reached, this may
execute fewer than self.frame_skip steps in the environment.
* Furthermore, in this case the returned observ... | Applies the given action in the environment.
Remarks:
* If a terminal state (from life loss or episode end) is reached, this may
execute fewer than self.frame_skip steps in the environment.
* Furthermore, in this case the returned observation may not contain valid
image data and should... | step | python | tensorflow/agents | tf_agents/environments/atari_preprocessing.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/atari_preprocessing.py | Apache-2.0 |
def _pool_and_resize(self):
"""Transforms two frames into a Nature DQN observation.
For efficiency, the transformation is done in-place in self.screen_buffer.
Returns:
transformed_screen: numpy array, pooled, resized screen.
"""
# Pool if there are enough screens to do so.
if self.frame_... | Transforms two frames into a Nature DQN observation.
For efficiency, the transformation is done in-place in self.screen_buffer.
Returns:
transformed_screen: numpy array, pooled, resized screen.
| _pool_and_resize | python | tensorflow/agents | tf_agents/environments/atari_preprocessing.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/atari_preprocessing.py | Apache-2.0 |
def __init__(
self,
envs: Sequence[py_environment.PyEnvironment],
multithreading: bool = True,
):
"""Batch together multiple (non-batched) py environments.
The environments can be different but must use the same action and
observation specs.
Args:
envs: List python environmen... | Batch together multiple (non-batched) py environments.
The environments can be different but must use the same action and
observation specs.
Args:
envs: List python environments (must be non-batched).
multithreading: Python bool describing whether interactions with the given
environmen... | __init__ | python | tensorflow/agents | tf_agents/environments/batched_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/batched_py_environment.py | Apache-2.0 |
def _reset(self):
"""Reset all environments and combine the resulting observation.
Returns:
Time step with batch dimension.
"""
if self._num_envs == 1:
return nest_utils.batch_nested_array(self._envs[0].reset())
else:
time_steps = self._execute(lambda env: env.reset(), self._envs)... | Reset all environments and combine the resulting observation.
Returns:
Time step with batch dimension.
| _reset | python | tensorflow/agents | tf_agents/environments/batched_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/batched_py_environment.py | Apache-2.0 |
def _step(self, actions):
"""Forward a batch of actions to the wrapped environments.
Args:
actions: Batched action, possibly nested, to apply to the environment.
Raises:
ValueError: Invalid actions.
Returns:
Batch of observations, rewards, and done flags.
"""
if self._num_e... | Forward a batch of actions to the wrapped environments.
Args:
actions: Batched action, possibly nested, to apply to the environment.
Raises:
ValueError: Invalid actions.
Returns:
Batch of observations, rewards, and done flags.
| _step | python | tensorflow/agents | tf_agents/environments/batched_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/batched_py_environment.py | Apache-2.0 |
def set_state(self, state: Sequence[Any]) -> None:
"""Restores the environment to a given `state`."""
self._execute(
lambda env_state: env_state[0].set_state(env_state[1]),
zip(self._envs, state)
) | Restores the environment to a given `state`. | set_state | python | tensorflow/agents | tf_agents/environments/batched_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/batched_py_environment.py | Apache-2.0 |
def close(self) -> None:
"""Send close messages to the external process and join them."""
self._execute(lambda env: env.close(), self._envs)
if self._parallel_execution:
self._pool.close()
self._pool.join() | Send close messages to the external process and join them. | close | python | tensorflow/agents | tf_agents/environments/batched_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/batched_py_environment.py | Apache-2.0 |
def unstack_actions(batched_actions: types.NestedArray) -> types.NestedArray:
"""Returns a list of actions from potentially nested batch of actions."""
flattened_actions = tf.nest.flatten(batched_actions)
unstacked_actions = [
tf.nest.pack_sequence_as(batched_actions, actions)
for actions in zip(*flat... | Returns a list of actions from potentially nested batch of actions. | unstack_actions | python | tensorflow/agents | tf_agents/environments/batched_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/batched_py_environment.py | Apache-2.0 |
def convert_time_step(time_step):
"""Convert to agents time_step type as the __hash__ method is different."""
reward = time_step.reward
if reward is None:
reward = 0.0
discount = time_step.discount
if discount is None:
discount = 1.0
observation = tf.nest.map_structure(_maybe_float32, time_step.obs... | Convert to agents time_step type as the __hash__ method is different. | convert_time_step | python | tensorflow/agents | tf_agents/environments/dm_control_wrapper.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/dm_control_wrapper.py | Apache-2.0 |
def spec_from_gym_space(
space: gym.Space,
simplify_box_bounds: bool = True,
name: Optional[Text] = None,
) -> Union[
specs.BoundedArraySpec,
specs.ArraySpec,
tuple[specs.ArraySpec, ...],
list[specs.ArraySpec],
collections.OrderedDict[str, specs.ArraySpec],
]:
"""Converts gymnasium spa... | Converts gymnasium spaces into array specs, or a collection thereof.
Please note:
Unlike OpenAI's gym, Farama's gymnasium provides a dtype for
each current implementation of spaces. dtype should be defined
in all specific subclasses of gymnasium.Space even if it is still
optional in the superclass.
... | spec_from_gym_space | python | tensorflow/agents | tf_agents/environments/gymnasium_wrapper.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/gymnasium_wrapper.py | Apache-2.0 |
def try_simplify_array_to_value(np_array):
"""If given numpy array has all the same values, returns that value."""
first_value = np_array.item(0)
if np.all(np_array == first_value):
return np.array(first_value, dtype=np_array.dtype)
else:
return np_array | If given numpy array has all the same values, returns that value. | try_simplify_array_to_value | python | tensorflow/agents | tf_agents/environments/gymnasium_wrapper.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/gymnasium_wrapper.py | Apache-2.0 |
def nested_spec(spec, child_name):
"""Returns the nested spec with a unique name."""
nested_name = name + '/' + child_name if name else child_name
return spec_from_gym_space(spec, simplify_box_bounds, nested_name) | Returns the nested spec with a unique name. | nested_spec | python | tensorflow/agents | tf_agents/environments/gymnasium_wrapper.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/gymnasium_wrapper.py | Apache-2.0 |
def __getattr__(self, name: Text) -> Any:
"""Forward all other calls to the base environment."""
gym_env = super(GymnasiumWrapper, self).__getattribute__('_gym_env')
return getattr(gym_env, name) | Forward all other calls to the base environment. | __getattr__ | python | tensorflow/agents | tf_agents/environments/gymnasium_wrapper.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/gymnasium_wrapper.py | Apache-2.0 |
def spec_from_gym_space(
space: gym.Space,
dtype_map: Optional[Dict[gym.Space, np.dtype]] = None,
simplify_box_bounds: bool = True,
name: Optional[Text] = None,
) -> specs.BoundedArraySpec:
"""Converts gym spaces into array specs.
Gym does not properly define dtypes for spaces. By default all space... | Converts gym spaces into array specs.
Gym does not properly define dtypes for spaces. By default all spaces set
their type to float64 even though observations do not always return this type.
See:
https://github.com/openai/gym/issues/527
To handle this we allow a dtype_map for setting default types for mappi... | spec_from_gym_space | python | tensorflow/agents | tf_agents/environments/gym_wrapper.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/gym_wrapper.py | Apache-2.0 |
def _to_obs_space_dtype(self, observation):
"""Make sure observation matches the specified space.
Observation spaces in gym didn't have a dtype for a long time. Now that they
do there is a large number of environments that do not follow the dtype in
the space definition. Since we use the space definiti... | Make sure observation matches the specified space.
Observation spaces in gym didn't have a dtype for a long time. Now that they
do there is a large number of environments that do not follow the dtype in
the space definition. Since we use the space definition to create the
tensorflow graph we need to ma... | _to_obs_space_dtype | python | tensorflow/agents | tf_agents/environments/gym_wrapper.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/gym_wrapper.py | Apache-2.0 |
def __init__(
self,
env_constructors: Sequence[EnvConstructor],
start_serially: bool = True,
blocking: bool = False,
flatten: bool = False,
):
"""Batch together environments and simulate them in external processes.
The environments can be different but must use the same action a... | Batch together environments and simulate them in external processes.
The environments can be different but must use the same action and
observation specs.
Args:
env_constructors: List of callables that create environments.
start_serially: Whether to start environments serially or in parallel.
... | __init__ | python | tensorflow/agents | tf_agents/environments/parallel_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/parallel_py_environment.py | Apache-2.0 |
def _stack_time_steps(self, time_steps):
"""Given a list of TimeStep, combine to one with a batch dimension."""
if self._flatten:
return nest_utils.fast_map_structure_flatten(
lambda *arrays: np.stack(arrays), self._time_step_spec, *time_steps
)
else:
return nest_utils.fast_map_s... | Given a list of TimeStep, combine to one with a batch dimension. | _stack_time_steps | python | tensorflow/agents | tf_agents/environments/parallel_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/parallel_py_environment.py | Apache-2.0 |
def render(self, mode: Text = 'rgb_array') -> types.NestedArray:
"""Renders the environment.
Args:
mode: Rendering mode. Currently only 'rgb_array' is supported because this
is a batched environment.
Returns:
An ndarray of shape [batch_size, width, height, 3] denoting RGB images
... | Renders the environment.
Args:
mode: Rendering mode. Currently only 'rgb_array' is supported because this
is a batched environment.
Returns:
An ndarray of shape [batch_size, width, height, 3] denoting RGB images
(for mode=`rgb_array`).
Raises:
NotImplementedError: If the en... | render | python | tensorflow/agents | tf_agents/environments/parallel_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/parallel_py_environment.py | Apache-2.0 |
def __init__(self, env_constructor: EnvConstructor, flatten: bool = False):
"""Step environment in a separate process for lock free paralellism.
The environment is created in an external process by calling the provided
callable. This can be an environment class, or a function creating the
environment a... | Step environment in a separate process for lock free paralellism.
The environment is created in an external process by calling the provided
callable. This can be an environment class, or a function creating the
environment and potentially wrapping it. The returned environment should
not access global v... | __init__ | python | tensorflow/agents | tf_agents/environments/parallel_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/parallel_py_environment.py | Apache-2.0 |
def start(self, wait_to_start: bool = True) -> None:
"""Start the process.
Args:
wait_to_start: Whether the call should wait for an env initialization.
"""
mp_context = multiprocessing.get_context()
self._conn, conn = mp_context.Pipe()
self._process = mp_context.Process(target=self._worke... | Start the process.
Args:
wait_to_start: Whether the call should wait for an env initialization.
| start | python | tensorflow/agents | tf_agents/environments/parallel_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/parallel_py_environment.py | Apache-2.0 |
def wait_start(self) -> None:
"""Wait for the started process to finish initialization."""
result = self._conn.recv()
if isinstance(result, Exception):
self._conn.close()
self._process.join(5)
raise result
assert result == self._READY, result | Wait for the started process to finish initialization. | wait_start | python | tensorflow/agents | tf_agents/environments/parallel_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/parallel_py_environment.py | Apache-2.0 |
def __getattr__(self, name: Text) -> Any:
"""Request an attribute from the environment.
Note that this involves communication with the external process, so it can
be slow.
This method is only called if the attribute is not found in the dictionary
of `ParallelPyEnvironment`'s definition.
Args:... | Request an attribute from the environment.
Note that this involves communication with the external process, so it can
be slow.
This method is only called if the attribute is not found in the dictionary
of `ParallelPyEnvironment`'s definition.
Args:
name: Attribute to access.
Returns:
... | __getattr__ | python | tensorflow/agents | tf_agents/environments/parallel_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/parallel_py_environment.py | Apache-2.0 |
def call(self, name: Text, *args, **kwargs) -> Promise:
"""Asynchronously call a method of the external environment.
Args:
name: Name of the method to call.
*args: Positional arguments to forward to the method.
**kwargs: Keyword arguments to forward to the method.
Returns:
The attr... | Asynchronously call a method of the external environment.
Args:
name: Name of the method to call.
*args: Positional arguments to forward to the method.
**kwargs: Keyword arguments to forward to the method.
Returns:
The attribute.
| call | python | tensorflow/agents | tf_agents/environments/parallel_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/parallel_py_environment.py | Apache-2.0 |
def close(self) -> None:
"""Send a close message to the external process and join it."""
try:
self._conn.send((self._CLOSE, None))
self._conn.close()
except IOError:
# The connection was already closed.
pass
if self._process.is_alive():
self._process.join(5) | Send a close message to the external process and join it. | close | python | tensorflow/agents | tf_agents/environments/parallel_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/parallel_py_environment.py | Apache-2.0 |
def step(
self, action: types.NestedArray, blocking: bool = True
) -> Union[ts.TimeStep, Promise]:
"""Step the environment.
Args:
action: The action to apply to the environment.
blocking: Whether to wait for the result.
Returns:
time step when blocking, otherwise callable that re... | Step the environment.
Args:
action: The action to apply to the environment.
blocking: Whether to wait for the result.
Returns:
time step when blocking, otherwise callable that returns the time step.
| step | python | tensorflow/agents | tf_agents/environments/parallel_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/parallel_py_environment.py | Apache-2.0 |
def reset(self, blocking: bool = True) -> Union[ts.TimeStep, Promise]:
"""Reset the environment.
Args:
blocking: Whether to wait for the result.
Returns:
New observation when blocking, otherwise callable that returns the new
observation.
"""
promise = self.call('reset')
if bl... | Reset the environment.
Args:
blocking: Whether to wait for the result.
Returns:
New observation when blocking, otherwise callable that returns the new
observation.
| reset | python | tensorflow/agents | tf_agents/environments/parallel_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/parallel_py_environment.py | Apache-2.0 |
def render(
self, mode: Text = 'rgb_array', blocking: bool = True
) -> Union[types.NestedArray, Promise]:
"""Renders the environment.
Args:
mode: Rendering mode. Only 'rgb_array' is supported.
blocking: Whether to wait for the result.
Returns:
An ndarray of shape [width, height, ... | Renders the environment.
Args:
mode: Rendering mode. Only 'rgb_array' is supported.
blocking: Whether to wait for the result.
Returns:
An ndarray of shape [width, height, 3] denoting an RGB image when
blocking. Otherwise, callable that returns the rendered image.
Raises:
NotI... | render | python | tensorflow/agents | tf_agents/environments/parallel_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/parallel_py_environment.py | Apache-2.0 |
def _receive(self):
"""Wait for a message from the worker process and return its payload.
Raises:
Exception: An exception was raised inside the worker process.
KeyError: The reveived message is of an unknown type.
Returns:
Payload object of the message.
"""
message, payload = sel... | Wait for a message from the worker process and return its payload.
Raises:
Exception: An exception was raised inside the worker process.
KeyError: The reveived message is of an unknown type.
Returns:
Payload object of the message.
| _receive | python | tensorflow/agents | tf_agents/environments/parallel_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/parallel_py_environment.py | Apache-2.0 |
def _worker(self, conn):
"""The process waits for actions and sends back environment results.
Args:
conn: Connection for communication to the main process.
Raises:
KeyError: When receiving a message of unknown type.
"""
try:
env = cloudpickle.loads(self._pickled_env_constructor)(... | The process waits for actions and sends back environment results.
Args:
conn: Connection for communication to the main process.
Raises:
KeyError: When receiving a message of unknown type.
| _worker | python | tensorflow/agents | tf_agents/environments/parallel_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/parallel_py_environment.py | Apache-2.0 |
def __init__(self, handle_auto_reset: bool = False):
"""Base class for Python RL environments.
Args:
handle_auto_reset: When `True` the base class will handle auto_reset of
the Environment.
"""
self._handle_auto_reset = handle_auto_reset
self._current_time_step = None
common.asser... | Base class for Python RL environments.
Args:
handle_auto_reset: When `True` the base class will handle auto_reset of
the Environment.
| __init__ | python | tensorflow/agents | tf_agents/environments/py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/py_environment.py | Apache-2.0 |
def batch_size(self) -> Optional[int]:
"""The batch size of the environment.
Returns:
The batch size of the environment, or `None` if the environment is not
batched.
Raises:
RuntimeError: If a subclass overrode batched to return True but did not
override the batch_size property.
... | The batch size of the environment.
Returns:
The batch size of the environment, or `None` if the environment is not
batched.
Raises:
RuntimeError: If a subclass overrode batched to return True but did not
override the batch_size property.
| batch_size | python | tensorflow/agents | tf_agents/environments/py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/py_environment.py | Apache-2.0 |
def should_reset(self, current_time_step: ts.TimeStep) -> bool:
"""Whether the Environmet should reset given the current timestep.
By default it only resets when all time_steps are `LAST`.
Args:
current_time_step: The current `TimeStep`.
Returns:
A bool indicating whether the Environment ... | Whether the Environmet should reset given the current timestep.
By default it only resets when all time_steps are `LAST`.
Args:
current_time_step: The current `TimeStep`.
Returns:
A bool indicating whether the Environment should reset or not.
| should_reset | python | tensorflow/agents | tf_agents/environments/py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/py_environment.py | Apache-2.0 |
def observation_spec(self) -> types.NestedArraySpec:
"""Defines the observations provided by the environment.
May use a subclass of `ArraySpec` that specifies additional properties such
as min and max bounds on the values.
Returns:
An `ArraySpec`, or a nested dict, list or tuple of `ArraySpec`s.... | Defines the observations provided by the environment.
May use a subclass of `ArraySpec` that specifies additional properties such
as min and max bounds on the values.
Returns:
An `ArraySpec`, or a nested dict, list or tuple of `ArraySpec`s.
| observation_spec | python | tensorflow/agents | tf_agents/environments/py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/py_environment.py | Apache-2.0 |
def action_spec(self) -> types.NestedArraySpec:
"""Defines the actions that should be provided to `step()`.
May use a subclass of `ArraySpec` that specifies additional properties such
as min and max bounds on the values.
Returns:
An `ArraySpec`, or a nested dict, list or tuple of `ArraySpec`s.
... | Defines the actions that should be provided to `step()`.
May use a subclass of `ArraySpec` that specifies additional properties such
as min and max bounds on the values.
Returns:
An `ArraySpec`, or a nested dict, list or tuple of `ArraySpec`s.
| action_spec | python | tensorflow/agents | tf_agents/environments/py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/py_environment.py | Apache-2.0 |
def discount_spec(self) -> types.NestedArraySpec:
"""Defines the discount that are returned by `step()`.
Override this method to define an environment that uses non-standard
discount values, for example an environment with array-valued discounts.
Returns:
An `ArraySpec`, or a nested dict, list o... | Defines the discount that are returned by `step()`.
Override this method to define an environment that uses non-standard
discount values, for example an environment with array-valued discounts.
Returns:
An `ArraySpec`, or a nested dict, list or tuple of `ArraySpec`s.
| discount_spec | python | tensorflow/agents | tf_agents/environments/py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/py_environment.py | Apache-2.0 |
def step(self, action: types.NestedArray) -> ts.TimeStep:
"""Updates the environment according to the action and returns a `TimeStep`.
If the environment returned a `TimeStep` with `StepType.LAST` at the
previous step the implementation of `_step` in the environment should call
`reset` to start a new s... | Updates the environment according to the action and returns a `TimeStep`.
If the environment returned a `TimeStep` with `StepType.LAST` at the
previous step the implementation of `_step` in the environment should call
`reset` to start a new sequence and ignore `action`.
This method will start a new se... | step | python | tensorflow/agents | tf_agents/environments/py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/py_environment.py | Apache-2.0 |
def render(self, mode: Text = 'rgb_array') -> Optional[types.NestedArray]:
"""Renders the environment.
Args:
mode: One of ['rgb_array', 'human']. Renders to an numpy array, or brings
up a window where the environment can be visualized.
Returns:
An ndarray of shape [width, height, 3] de... | Renders the environment.
Args:
mode: One of ['rgb_array', 'human']. Renders to an numpy array, or brings
up a window where the environment can be visualized.
Returns:
An ndarray of shape [width, height, 3] denoting an RGB image if mode is
`rgb_array`. Otherwise return nothing and ren... | render | python | tensorflow/agents | tf_agents/environments/py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/py_environment.py | Apache-2.0 |
def seed(self, seed: types.Seed) -> Any:
"""Seeds the environment.
Args:
seed: Value to use as seed for the environment.
"""
del seed # unused
raise NotImplementedError('No seed support for this environment.') | Seeds the environment.
Args:
seed: Value to use as seed for the environment.
| seed | python | tensorflow/agents | tf_agents/environments/py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/py_environment.py | Apache-2.0 |
def get_state(self) -> Any:
"""Returns the `state` of the environment.
The `state` contains everything required to restore the environment to the
current configuration. This can contain e.g.
- The current time_step.
- The number of steps taken in the environment (for finite horizon MDPs).
... | Returns the `state` of the environment.
The `state` contains everything required to restore the environment to the
current configuration. This can contain e.g.
- The current time_step.
- The number of steps taken in the environment (for finite horizon MDPs).
- Hidden state (for POMDPs).
... | get_state | python | tensorflow/agents | tf_agents/environments/py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/py_environment.py | Apache-2.0 |
def set_state(self, state: Any) -> None:
"""Restores the environment to a given `state`.
See definition of `state` in the documentation for get_state().
Args:
state: A state to restore the environment to.
"""
raise NotImplementedError(
'This environment has not implemented `set_state... | Restores the environment to a given `state`.
See definition of `state` in the documentation for get_state().
Args:
state: A state to restore the environment to.
| set_state | python | tensorflow/agents | tf_agents/environments/py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/py_environment.py | Apache-2.0 |
def _step(self, action: types.NestedArray) -> ts.TimeStep:
"""Updates the environment according to action and returns a `TimeStep`.
See `step(self, action)` docstring for more details.
Args:
action: A NumPy array, or a nested dict, list or tuple of arrays
corresponding to `action_spec()`.
... | Updates the environment according to action and returns a `TimeStep`.
See `step(self, action)` docstring for more details.
Args:
action: A NumPy array, or a nested dict, list or tuple of arrays
corresponding to `action_spec()`.
| _step | python | tensorflow/agents | tf_agents/environments/py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/py_environment.py | Apache-2.0 |
def _reset(self) -> ts.TimeStep:
"""Starts a new sequence, returns the first `TimeStep` of this sequence.
See `reset(self)` docstring for more details
""" | Starts a new sequence, returns the first `TimeStep` of this sequence.
See `reset(self)` docstring for more details
| _reset | python | tensorflow/agents | tf_agents/environments/py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/py_environment.py | Apache-2.0 |
def _convert_action_spec(spec: tfa_spec.ArraySpec) -> dm_spec.Array:
"""Converts a TF Agents action spec to a DM action spec.
Similar to _convert_spec but changes discrete actions to DiscreteArray rather
than BoundedArray.
Args:
spec: The TF Agents action spec to convert.
Returns:
The converted DM a... | Converts a TF Agents action spec to a DM action spec.
Similar to _convert_spec but changes discrete actions to DiscreteArray rather
than BoundedArray.
Args:
spec: The TF Agents action spec to convert.
Returns:
The converted DM action spec.
| _convert_action_spec | python | tensorflow/agents | tf_agents/environments/py_to_dm_wrapper.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/py_to_dm_wrapper.py | Apache-2.0 |
def __init__(
self,
observation_spec: types.NestedArray,
action_spec: Optional[types.NestedArray] = None,
episode_end_probability: types.Float = 0.1,
discount: types.Float = 1.0,
reward_fn: Optional[RewardFn] = None,
batch_size: Optional[types.Int] = None,
auto_reset: boo... | Initializes the environment.
Args:
observation_spec: An `ArraySpec`, or a nested dict, list or tuple of
`ArraySpec`s.
action_spec: An `ArraySpec`, or a nested dict, list or tuple of
`ArraySpec`s.
episode_end_probability: Probability an episode will end when the
environment... | __init__ | python | tensorflow/agents | tf_agents/environments/random_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/random_py_environment.py | Apache-2.0 |
def __init__(
self,
time_step_spec,
action_spec,
batch_size=1,
episode_end_probability=0.1,
):
"""Initializes the environment.
Args:
time_step_spec: A `TimeStep` namedtuple containing `TensorSpec`s defining
the Tensors returned by `step()` (step_type, reward, disco... | Initializes the environment.
Args:
time_step_spec: A `TimeStep` namedtuple containing `TensorSpec`s defining
the Tensors returned by `step()` (step_type, reward, discount, and
observation).
action_spec: A nest of BoundedTensorSpec representing the actions of the
environment.
... | __init__ | python | tensorflow/agents | tf_agents/environments/random_tf_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/random_tf_environment.py | Apache-2.0 |
def _reset(self):
"""Resets the environment and returns the current time_step."""
obs, _ = self._sample_obs_and_reward()
time_step = ts.restart(
obs, self._batch_size, reward_spec=self._time_step_spec.reward
)
self._update_time_step(time_step)
return self._current_time_step() | Resets the environment and returns the current time_step. | _reset | python | tensorflow/agents | tf_agents/environments/random_tf_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/random_tf_environment.py | Apache-2.0 |
def _step(self, action):
"""Steps the environment according to the action."""
# Make sure the given action is compatible with the spec. We compare it to
# t[0] as the spec doesn't have a batch dim.
tf.nest.map_structure(
lambda spec, t: tf.Assert(spec.is_compatible_with(t[0]), [t]),
self... | Steps the environment according to the action. | _step | python | tensorflow/agents | tf_agents/environments/random_tf_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/random_tf_environment.py | Apache-2.0 |
def game(
name: Text = 'Pong',
obs_type: Text = 'image',
mode: Text = 'NoFrameskip',
version: Text = 'v0',
) -> Text:
"""Generates the full name for the game.
Args:
name: String. Ex. Pong, SpaceInvaders, ...
obs_type: String, type of observation. Ex. 'image' or 'ram'.
mode: String. Ex. ... | Generates the full name for the game.
Args:
name: String. Ex. Pong, SpaceInvaders, ...
obs_type: String, type of observation. Ex. 'image' or 'ram'.
mode: String. Ex. '', 'NoFrameskip' or 'Deterministic'.
version: String. Ex. 'v0' or 'v4'.
Returns:
The full name for the game.
| game | python | tensorflow/agents | tf_agents/environments/suite_atari.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/suite_atari.py | Apache-2.0 |
def load(
environment_name: Text,
discount: types.Int = 1.0,
max_episode_steps: Optional[types.Int] = None,
gym_env_wrappers: Sequence[
types.GymEnvWrapper
] = DEFAULT_ATARI_GYM_WRAPPERS,
env_wrappers: Sequence[types.PyEnvWrapper] = (),
spec_dtype_map: Optional[Dict[gym.Space, np.dty... | Loads the selected environment and wraps it with the specified wrappers. | load | python | tensorflow/agents | tf_agents/environments/suite_atari.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/suite_atari.py | Apache-2.0 |
def load(
bsuite_id: Text,
record: bool = True,
save_path: Optional[Text] = None,
logging_mode: Text = 'csv',
overwrite: bool = False,
) -> py_environment.PyEnvironment:
"""Loads the selected environment.
Args:
bsuite_id: a bsuite_id specifies a bsuite experiment. For an example
`bsui... | Loads the selected environment.
Args:
bsuite_id: a bsuite_id specifies a bsuite experiment. For an example
`bsuite_id` "deep_sea/7" will be 7th level of the "deep_sea" task.
record: whether to log bsuite results.
save_path: the directory to save bsuite results.
logging_mode: which form of loggi... | load | python | tensorflow/agents | tf_agents/environments/suite_bsuite.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/suite_bsuite.py | Apache-2.0 |
def _load_env(
domain_name: Text,
task_name: Text,
task_kwargs=None,
environment_kwargs=None,
visualize_reward: bool = False,
):
"""Loads a DM environment.
Args:
domain_name: A string containing the name of a domain.
task_name: A string containing the name of a task.
task_kwargs: Op... | Loads a DM environment.
Args:
domain_name: A string containing the name of a domain.
task_name: A string containing the name of a task.
task_kwargs: Optional `dict` of keyword arguments for the task.
environment_kwargs: Optional `dict` specifying keyword arguments for the
environment.
visua... | _load_env | python | tensorflow/agents | tf_agents/environments/suite_dm_control.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/suite_dm_control.py | Apache-2.0 |
def load(
domain_name: Text,
task_name: Text,
task_kwargs=None,
environment_kwargs=None,
visualize_reward: bool = False,
render_kwargs=None,
env_wrappers: Sequence[types.PyEnvWrapper] = (),
) -> py_environment.PyEnvironment:
"""Returns an environment from a domain name, task name and optio... | Returns an environment from a domain name, task name and optional settings.
Args:
domain_name: A string containing the name of a domain.
task_name: A string containing the name of a task.
task_kwargs: Optional `dict` of keyword arguments for the task.
environment_kwargs: Optional `dict` specifying ke... | load | python | tensorflow/agents | tf_agents/environments/suite_dm_control.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/suite_dm_control.py | Apache-2.0 |
def load_pixels(
domain_name: Text,
task_name: Text,
observation_key: Text = 'pixels',
pixels_only: bool = True,
task_kwargs=None,
environment_kwargs=None,
visualize_reward: bool = False,
render_kwargs=None,
env_wrappers: Sequence[types.PyEnvWrapper] = (),
env_state_wrappers: Seq... | Returns an environment from a domain name, task name and optional settings.
Args:
domain_name: A string containing the name of a domain.
task_name: A string containing the name of a task.
observation_key: Optional custom string specifying the pixel observation's
key in the `OrderedDict` of observat... | load_pixels | python | tensorflow/agents | tf_agents/environments/suite_dm_control.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/suite_dm_control.py | Apache-2.0 |
def __init__(self, time_step_spec=None, action_spec=None, batch_size=1):
"""Initializes the environment.
Meant to be called by subclass constructors.
Args:
time_step_spec: A `TimeStep` namedtuple containing `TensorSpec`s defining
the Tensors returned by `step()` (step_type, reward, discount,... | Initializes the environment.
Meant to be called by subclass constructors.
Args:
time_step_spec: A `TimeStep` namedtuple containing `TensorSpec`s defining
the Tensors returned by `step()` (step_type, reward, discount, and
observation).
action_spec: A nest of BoundedTensorSpec repres... | __init__ | python | tensorflow/agents | tf_agents/environments/tf_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/tf_environment.py | Apache-2.0 |
def _pack_named_sequence(flat_inputs, input_spec, batch_shape):
"""Assembles back a nested structure that has been flattened."""
named_inputs = []
for flat_input, spec in zip(flat_inputs, tf.nest.flatten(input_spec)):
named_input = tf.identity(flat_input, name=spec.name)
if not tf.executing_eagerly():
... | Assembles back a nested structure that has been flattened. | _pack_named_sequence | python | tensorflow/agents | tf_agents/environments/tf_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/tf_py_environment.py | Apache-2.0 |
def _check_not_called_concurrently(lock):
"""Checks the returned context is not executed concurrently with any other."""
if not lock.acquire(False): # Non-blocking.
raise RuntimeError(
'Detected concurrent execution of TFPyEnvironment ops. Make sure the '
'appropriate step_state is passed to st... | Checks the returned context is not executed concurrently with any other. | _check_not_called_concurrently | python | tensorflow/agents | tf_agents/environments/tf_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/tf_py_environment.py | Apache-2.0 |
def __init__(
self,
environment: py_environment.PyEnvironment,
check_dims: bool = False,
isolation: bool = False,
):
"""Initializes a new `TFPyEnvironment`.
Args:
environment: Environment to interact with, implementing
`py_environment.PyEnvironment`. Or a `callable` tha... | Initializes a new `TFPyEnvironment`.
Args:
environment: Environment to interact with, implementing
`py_environment.PyEnvironment`. Or a `callable` that returns an
environment of this form. If a `callable` is provided and
`thread_isolation` is provided, the callable is executed in th... | __init__ | python | tensorflow/agents | tf_agents/environments/tf_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/tf_py_environment.py | Apache-2.0 |
def __getattr__(self, name: Text) -> Any:
"""Enables access attributes of the wrapped PyEnvironment.
Use with caution since methods of the PyEnvironment can be incompatible
with TF.
Args:
name: Name of the attribute.
Returns:
The attribute.
"""
if name in self.__dict__:
... | Enables access attributes of the wrapped PyEnvironment.
Use with caution since methods of the PyEnvironment can be incompatible
with TF.
Args:
name: Name of the attribute.
Returns:
The attribute.
| __getattr__ | python | tensorflow/agents | tf_agents/environments/tf_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/tf_py_environment.py | Apache-2.0 |
def close(self) -> None:
"""Send close to wrapped env & also to the isolation pool + join it.
Only closes pool when `isolation` was provided at init time.
"""
self._env.close()
if self._pool:
self._pool.join()
self._pool.close()
self._pool = None | Send close to wrapped env & also to the isolation pool + join it.
Only closes pool when `isolation` was provided at init time.
| close | python | tensorflow/agents | tf_agents/environments/tf_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/tf_py_environment.py | Apache-2.0 |
def _current_time_step(self):
"""Returns the current ts.TimeStep.
Returns:
A `TimeStep` tuple of:
step_type: A scalar int32 tensor representing the `StepType` value.
reward: A float32 tensor representing the reward at this
timestep.
discount: A scalar float32 tensor repr... | Returns the current ts.TimeStep.
Returns:
A `TimeStep` tuple of:
step_type: A scalar int32 tensor representing the `StepType` value.
reward: A float32 tensor representing the reward at this
timestep.
discount: A scalar float32 tensor representing the discount [0, 1].
... | _current_time_step | python | tensorflow/agents | tf_agents/environments/tf_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/tf_py_environment.py | Apache-2.0 |
def _reset(self):
"""Returns the current `TimeStep` after resetting the environment.
Returns:
A `TimeStep` tuple of:
step_type: A scalar int32 tensor representing the `StepType` value.
reward: A float32 tensor representing the reward at this
timestep.
discount: A scalar ... | Returns the current `TimeStep` after resetting the environment.
Returns:
A `TimeStep` tuple of:
step_type: A scalar int32 tensor representing the `StepType` value.
reward: A float32 tensor representing the reward at this
timestep.
discount: A scalar float32 tensor representi... | _reset | python | tensorflow/agents | tf_agents/environments/tf_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/tf_py_environment.py | Apache-2.0 |
def _step(self, actions):
"""Returns a TensorFlow op to step the environment.
Args:
actions: A Tensor, or a nested dict, list or tuple of Tensors
corresponding to `action_spec()`.
Returns:
A `TimeStep` tuple of:
step_type: A scalar int32 tensor representing the `StepType` value... | Returns a TensorFlow op to step the environment.
Args:
actions: A Tensor, or a nested dict, list or tuple of Tensors
corresponding to `action_spec()`.
Returns:
A `TimeStep` tuple of:
step_type: A scalar int32 tensor representing the `StepType` value.
reward: A float32 tenso... | _step | python | tensorflow/agents | tf_agents/environments/tf_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/tf_py_environment.py | Apache-2.0 |
def render(self, mode: Text = 'rgb_array') -> Optional[types.NestedTensor]:
"""Renders the environment.
Note for compatibility this will convert the image to uint8.
Args:
mode: One of ['rgb_array', 'human']. Renders to an numpy array, or brings
up a window where the environment can be visual... | Renders the environment.
Note for compatibility this will convert the image to uint8.
Args:
mode: One of ['rgb_array', 'human']. Renders to an numpy array, or brings
up a window where the environment can be visualized.
Returns:
A Tensor of shape [width, height, 3] denoting an RGB imag... | render | python | tensorflow/agents | tf_agents/environments/tf_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/tf_py_environment.py | Apache-2.0 |
def _render(mode):
"""Pywrapper fn to the environments render."""
# Mode might be passed down as bytes or ndarray.
# If so, convert to a str first.
if isinstance(mode, np.ndarray):
mode = str(mode)
if isinstance(mode, bytes):
mode = mode.decode('utf-8')
if mode == 'rg... | Pywrapper fn to the environments render. | _render | python | tensorflow/agents | tf_agents/environments/tf_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/tf_py_environment.py | Apache-2.0 |
def _time_step_from_numpy_function_outputs(self, outputs):
"""Forms a `TimeStep` from the output of the numpy_function outputs."""
batch_shape = () if not self.batched else (self.batch_size,)
batch_shape = tf.TensorShape(batch_shape)
time_step = _pack_named_sequence(
outputs, self.time_step_spec... | Forms a `TimeStep` from the output of the numpy_function outputs. | _time_step_from_numpy_function_outputs | python | tensorflow/agents | tf_agents/environments/tf_py_environment.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/tf_py_environment.py | Apache-2.0 |
def __init__(self, policy, time_major=False):
"""Creates a TrajectoryReplay object.
TrajectoryReplay.run returns the actions and policy info of the new policy
assuming it saw the observations from the given trajectory.
Args:
policy: A tf_policy.TFPolicy policy.
time_major: If `True`, the t... | Creates a TrajectoryReplay object.
TrajectoryReplay.run returns the actions and policy info of the new policy
assuming it saw the observations from the given trajectory.
Args:
policy: A tf_policy.TFPolicy policy.
time_major: If `True`, the tensors in `trajectory` passed to method `run`
... | __init__ | python | tensorflow/agents | tf_agents/environments/trajectory_replay.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/trajectory_replay.py | Apache-2.0 |
def process_step(
time, time_step, policy_state, output_action_tas, output_policy_info_tas
):
"""Take an action on the given step, and update output TensorArrays.
Args:
time: Step time. Describes which row to read from the trajectory
TensorArrays and which location to write i... | Take an action on the given step, and update output TensorArrays.
Args:
time: Step time. Describes which row to read from the trajectory
TensorArrays and which location to write into in the output
TensorArrays.
time_step: Previous step's `TimeStep`.
policy_state: Poli... | process_step | python | tensorflow/agents | tf_agents/environments/trajectory_replay.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/trajectory_replay.py | Apache-2.0 |
def loop_body(
time, time_step, policy_state, output_action_tas, output_policy_info_tas
):
"""Runs a step in environment.
While loop will call multiple times.
Args:
time: Step time.
time_step: Previous step's `TimeStep`.
policy_state: Policy state tensor or nested... | Runs a step in environment.
While loop will call multiple times.
Args:
time: Step time.
time_step: Previous step's `TimeStep`.
policy_state: Policy state tensor or nested structure of tensors.
output_action_tas: Updated nest of `tf.TensorArray`, the new actions.
out... | loop_body | python | tensorflow/agents | tf_agents/environments/trajectory_replay.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/trajectory_replay.py | Apache-2.0 |
def get_tf_env(
environment: Union[
py_environment.PyEnvironment, tf_environment.TFEnvironment
]
) -> tf_environment.TFEnvironment:
"""Ensures output is a tf_environment, wrapping py_environments if needed."""
if environment is None:
raise ValueError('`environment` cannot be None')
if isinstan... | Ensures output is a tf_environment, wrapping py_environments if needed. | get_tf_env | python | tensorflow/agents | tf_agents/environments/utils.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/utils.py | Apache-2.0 |
def validate_py_environment(
environment: py_environment.PyEnvironment,
episodes: int = 5,
observation_and_action_constraint_splitter: Optional[types.Splitter] = None,
action_constraint_suffix: Optional[str] = None,
):
"""Validates the environment follows the defined specs.
Args:
environment: T... | Validates the environment follows the defined specs.
Args:
environment: The environment to test.
episodes: The number of episodes to run a random policy over.
observation_and_action_constraint_splitter: A function used to process
observations with action constraints. These constraints can indicate,... | validate_py_environment | python | tensorflow/agents | tf_agents/environments/utils.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/utils.py | Apache-2.0 |
def __init__(
self,
env: py_environment.PyEnvironment,
process_profile_fn: Callable[[cProfile.Profile], Any],
process_steps: int,
):
"""Create a PerformanceProfiler that uses cProfile to profile env execution.
Args:
env: Environment to wrap.
process_profile_fn: A callback ... | Create a PerformanceProfiler that uses cProfile to profile env execution.
Args:
env: Environment to wrap.
process_profile_fn: A callback that accepts a `Profile` object. After
`process_profile_fn` is called, profile information is reset.
process_steps: The frequency with which `process_pr... | __init__ | python | tensorflow/agents | tf_agents/environments/wrappers.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/wrappers.py | Apache-2.0 |
def __init__(
self,
env: py_environment.PyEnvironment,
times: types.Int,
handle_auto_reset: bool = False,
):
"""Creates an action repeat wrapper.
Args:
env: Environment to wrap.
times: Number of times the action should be repeated.
handle_auto_reset: When `True` the ... | Creates an action repeat wrapper.
Args:
env: Environment to wrap.
times: Number of times the action should be repeated.
handle_auto_reset: When `True` the base class will handle auto_reset of
the Environment.
Raises:
ValueError: If the times parameter is not greater than 1.
... | __init__ | python | tensorflow/agents | tf_agents/environments/wrappers.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/wrappers.py | Apache-2.0 |
def __init__(
self,
env: py_environment.PyEnvironment,
flat_dtype=None,
handle_auto_reset: bool = False,
):
"""Creates a FlattenActionWrapper.
Args:
env: Environment to wrap.
flat_dtype: Optional, if set to a np.dtype the flat action_spec uses this
dtype.
han... | Creates a FlattenActionWrapper.
Args:
env: Environment to wrap.
flat_dtype: Optional, if set to a np.dtype the flat action_spec uses this
dtype.
handle_auto_reset: When `True` the base class will handle auto_reset of
the Environment.
Raises:
ValueError: If any of the ac... | __init__ | python | tensorflow/agents | tf_agents/environments/wrappers.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/wrappers.py | Apache-2.0 |
def __init__(
self,
env: py_environment.PyEnvironment,
idx: Union[Sequence[int], np.ndarray],
handle_auto_reset: bool = False,
):
"""Creates an observation filter wrapper.
Args:
env: Environment to wrap.
idx: Array of indexes pointing to elements to include in output.
... | Creates an observation filter wrapper.
Args:
env: Environment to wrap.
idx: Array of indexes pointing to elements to include in output.
handle_auto_reset: When `True` the base class will handle auto_reset of
the Environment.
Raises:
ValueError: If observation spec is nested.
... | __init__ | python | tensorflow/agents | tf_agents/environments/wrappers.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/wrappers.py | Apache-2.0 |
def _map_actions(self, action, action_map):
"""Maps the given discrete action to the corresponding continuous action.
Args:
action: Discrete action to map.
action_map: Array with the continuous linspaces for the action.
Returns:
Numpy array with the mapped continuous actions.
Raises:... | Maps the given discrete action to the corresponding continuous action.
Args:
action: Discrete action to map.
action_map: Array with the continuous linspaces for the action.
Returns:
Numpy array with the mapped continuous actions.
Raises:
ValueError: If the given action's shpe does ... | _map_actions | python | tensorflow/agents | tf_agents/environments/wrappers.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/wrappers.py | Apache-2.0 |
def _step(self, action):
"""Steps the environment while remapping the actions.
Args:
action: Action to take.
Returns:
The next time_step from the environment.
"""
continuous_actions = self._map_actions(action, self._action_map)
env_action_spec = self._env.action_spec()
if tf.n... | Steps the environment while remapping the actions.
Args:
action: Action to take.
Returns:
The next time_step from the environment.
| _step | python | tensorflow/agents | tf_agents/environments/wrappers.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/wrappers.py | Apache-2.0 |
def _step(self, action):
"""Steps the environment after clipping the actions.
Args:
action: Action to take.
Returns:
The next time_step from the environment.
"""
env_action_spec = self._env.action_spec()
def _clip_to_spec(act_spec, act):
# NumPy does not allow both min and m... | Steps the environment after clipping the actions.
Args:
action: Action to take.
Returns:
The next time_step from the environment.
| _step | python | tensorflow/agents | tf_agents/environments/wrappers.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/wrappers.py | Apache-2.0 |
def __init__(
self,
env: py_environment.PyEnvironment,
observations_allowlist: Optional[Sequence[Text]] = None,
handle_auto_reset: bool = False,
):
"""Initializes a wrapper to flatten environment observations.
Args:
env: A `py_environment.PyEnvironment` environment to wrap.
... | Initializes a wrapper to flatten environment observations.
Args:
env: A `py_environment.PyEnvironment` environment to wrap.
observations_allowlist: A list of observation keys that want to be
observed from the environment. All other observations returned are
filtered out. If not provid... | __init__ | python | tensorflow/agents | tf_agents/environments/wrappers.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/wrappers.py | Apache-2.0 |
def _filter_observations(self, observations):
"""Filters out unwanted observations from the environment.
Args:
observations: A nested dictionary of arrays corresponding to
`observation_spec()`. This is the observation attribute in the TimeStep
object returned by the environment.
Retu... | Filters out unwanted observations from the environment.
Args:
observations: A nested dictionary of arrays corresponding to
`observation_spec()`. This is the observation attribute in the TimeStep
object returned by the environment.
Returns:
A nested dict of arrays corresponding to `... | _filter_observations | python | tensorflow/agents | tf_agents/environments/wrappers.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/wrappers.py | Apache-2.0 |
def _pack_and_filter_timestep_observation(self, timestep):
"""Pack and filter observations into a single dimension.
Args:
timestep: A `TimeStep` namedtuple containing: - step_type: A `StepType`
value. - reward: Reward at this timestep. - discount: A discount in the
range [0, 1]. - observa... | Pack and filter observations into a single dimension.
Args:
timestep: A `TimeStep` namedtuple containing: - step_type: A `StepType`
value. - reward: Reward at this timestep. - discount: A discount in the
range [0, 1]. - observation: A NumPy array, or a nested dict, list or
tuple of ar... | _pack_and_filter_timestep_observation | python | tensorflow/agents | tf_agents/environments/wrappers.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/wrappers.py | Apache-2.0 |
def _flatten_nested_observations(self, observations, is_batched):
"""Flatten individual observations and then flatten the nested structure.
Args:
observations: A flattened NumPy array of shape corresponding to
`observation_spec()` or an `observation_spec()`.
is_batched: Whether or not the p... | Flatten individual observations and then flatten the nested structure.
Args:
observations: A flattened NumPy array of shape corresponding to
`observation_spec()` or an `observation_spec()`.
is_batched: Whether or not the provided observation is batched.
Returns:
A concatenated and fl... | _flatten_nested_observations | python | tensorflow/agents | tf_agents/environments/wrappers.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/wrappers.py | Apache-2.0 |
def __init__(
self, env: py_environment.PyEnvironment, handle_auto_reset: bool = False
):
"""Initializes a wrapper to add a goal to the observation.
Args:
env: A `py_environment.PyEnvironment` environment to wrap.
handle_auto_reset: When `True` the base class will handle auto_reset of
... | Initializes a wrapper to add a goal to the observation.
Args:
env: A `py_environment.PyEnvironment` environment to wrap.
handle_auto_reset: When `True` the base class will handle auto_reset of
the Environment.
Raises:
ValueError: If environment observation is not a dict
| __init__ | python | tensorflow/agents | tf_agents/environments/wrappers.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/wrappers.py | Apache-2.0 |
def get_trajectory_with_goal(
self, trajectory: ts.TimeStep, goal: types.NestedArray
) -> ts.TimeStep:
"""Generates a new trajectory assuming the given goal was the actual target.
One example is updating a "distance-to-goal" field in the observation. Note
that relevant state information must be rec... | Generates a new trajectory assuming the given goal was the actual target.
One example is updating a "distance-to-goal" field in the observation. Note
that relevant state information must be recovered or re-calculated from the
given trajectory.
Args:
trajectory: An instance of `TimeStep`.
g... | get_trajectory_with_goal | python | tensorflow/agents | tf_agents/environments/wrappers.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/wrappers.py | Apache-2.0 |
def get_goal_from_trajectory(
self, trajectory: ts.TimeStep
) -> types.NestedArray:
"""Extracts the goal from a given trajectory.
Args:
trajectory: An instance of `TimeStep`.
Returns:
Environment specific goal
Raises:
NotImplementedError: function should be implemented in ch... | Extracts the goal from a given trajectory.
Args:
trajectory: An instance of `TimeStep`.
Returns:
Environment specific goal
Raises:
NotImplementedError: function should be implemented in child class.
| get_goal_from_trajectory | python | tensorflow/agents | tf_agents/environments/wrappers.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/wrappers.py | Apache-2.0 |
def _reset(self, *args, **kwargs):
"""Resets the environment, updating the trajectory with goal."""
trajectory = self._env.reset(*args, **kwargs)
self._goal = self.get_goal_from_trajectory(trajectory)
return self.get_trajectory_with_goal(trajectory, self._goal) | Resets the environment, updating the trajectory with goal. | _reset | python | tensorflow/agents | tf_agents/environments/wrappers.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/wrappers.py | Apache-2.0 |
def _step(self, *args, **kwargs):
"""Execute a step in the environment, updating the trajectory with goal."""
trajectory = self._env.step(*args, **kwargs)
return self.get_trajectory_with_goal(trajectory, self._goal) | Execute a step in the environment, updating the trajectory with goal. | _step | python | tensorflow/agents | tf_agents/environments/wrappers.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/wrappers.py | Apache-2.0 |
def __init__(
self,
env: py_environment.PyEnvironment,
history_length: int = 3,
include_actions: bool = False,
tile_first_step_obs: bool = False,
handle_auto_reset: bool = False,
):
"""Initializes a HistoryWrapper.
Args:
env: Environment to wrap.
history_length... | Initializes a HistoryWrapper.
Args:
env: Environment to wrap.
history_length: Length of the history to attach.
include_actions: Whether actions should be included in the history.
tile_first_step_obs: If True the observation on reset is tiled to fill the
history.
handle_auto_re... | __init__ | python | tensorflow/agents | tf_agents/environments/wrappers.py | https://github.com/tensorflow/agents/blob/master/tf_agents/environments/wrappers.py | Apache-2.0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.