Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Google deepmind Dm control Environment Spec Methods

From Leeroopedia
Revision as of 12:42, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Google_deepmind_Dm_control_Environment_Spec_Methods.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Metadata Value
Implementation Environment Spec Methods
Domain Reinforcement_Learning, Physics_Simulation
Source dm_control
Workflow Control_Suite_RL_Training
Last Updated 2026-02-15 00:00 GMT

Overview

Concrete tool for querying the action and observation specifications of a dm_control environment through the action_spec() and observation_spec() methods.

Description

The Environment class in dm_control.rl.control exposes two spec methods:

  • action_spec() -- delegates to self._task.action_spec(self._physics). The task returns a BoundedArraySpec (or a nested structure of them) describing the shape, dtype, and element-wise minimum/maximum of valid actions.
  • observation_spec() -- first attempts to call self._task.observation_spec(self._physics). If the task raises NotImplementedError, the environment falls back to calling self._task.get_observation(self._physics) and inferring specs from the resulting numpy arrays via _spec_from_observation. When flat_observation=True, the inferred spec reflects the flattened array.

Both methods are required by the dm_env.Environment interface and are called before the first episode to configure agent networks, replay buffers, and normalisers.

Usage

Use this implementation when:

  • You need the exact shape, dtype, and bounds of the action space to construct a policy network.
  • You need the observation dictionary structure to build input preprocessing layers.
  • You want to programmatically validate that a wrapper preserves or modifies specs as expected.

Code Reference

Attribute Detail
Source Location dm_control/rl/control.py:L129-153
Signatures Environment.action_spec() -> BoundedArraySpec, Environment.observation_spec() -> OrderedDict[str, ArraySpec]
Import from dm_control.rl.control import Environment (typically obtained via suite.load)

I/O Contract

Inputs (for both methods)

Name Type Description
self Environment The environment instance. No additional arguments are required.

Outputs -- action_spec()

Name Type Description
return dm_env.specs.BoundedArraySpec Contains shape, dtype, minimum (array), and maximum (array) describing valid actions.

Outputs -- observation_spec()

Name Type Description
return OrderedDict[str, dm_env.specs.ArraySpec] Maps observation names (e.g. "position", "velocity") to ArraySpec instances with shape, dtype, and name.

Usage Examples

Inspect action spec to build a policy:

from dm_control import suite

env = suite.load('cheetah', 'run')

action_spec = env.action_spec()
print(f"Action shape: {action_spec.shape}")       # e.g. (6,)
print(f"Action dtype: {action_spec.dtype}")        # e.g. float64
print(f"Action min:   {action_spec.minimum}")      # e.g. [-1. -1. -1. -1. -1. -1.]
print(f"Action max:   {action_spec.maximum}")      # e.g. [1. 1. 1. 1. 1. 1.]

Inspect observation spec to size an input layer:

from dm_control import suite

env = suite.load('cartpole', 'balance')

obs_spec = env.observation_spec()
for key, spec in obs_spec.items():
    print(f"Observation '{key}': shape={spec.shape}, dtype={spec.dtype}")

# Example output:
# Observation 'position': shape=(3,), dtype=float64
# Observation 'velocity': shape=(2,), dtype=float64

Use flat observations for a single input vector:

from dm_control import suite

env = suite.load(
    'walker', 'walk',
    environment_kwargs={'flat_observation': True},
)
obs_spec = env.observation_spec()
# Returns {'observations': ArraySpec(shape=(24,), dtype=float64)}
print(f"Flat observation size: {obs_spec['observations'].shape[0]}")

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment