Implementation:Google deepmind Dm control Environment Spec Methods
| 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 toself._task.action_spec(self._physics). The task returns aBoundedArraySpec(or a nested structure of them) describing the shape, dtype, and element-wise minimum/maximum of valid actions.
observation_spec()-- first attempts to callself._task.observation_spec(self._physics). If the task raisesNotImplementedError, the environment falls back to callingself._task.get_observation(self._physics)and inferring specs from the resulting numpy arrays via_spec_from_observation. Whenflat_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]}")