Deep Q-Network (DQN)¶
DQN is a model-free, off-policy algorithm that trains a control policies directly from high-dimensional sensory using a deep function approximator to represent the Q-value function.
Paper: Playing Atari with Deep Reinforcement Learning.
Algorithm¶
Algorithm implementation¶
Decision making¶
act(...)Learning algorithm¶
_update(...)Usage¶
# import the agent and its default configuration
from skrl.agents.torch.dqn import DQN, DQN_CFG
# instantiate the agent's models
models = {}
models["q_network"] = ...
models["target_q_network"] = ... # only required during training
# adjust some configuration if necessary
cfg_agent = DQN_CFG()
cfg_agent.KEY = ...
# instantiate the agent
# (assuming a defined environment <env> and memory <memory>)
agent = DQN(
models=models,
memory=memory, # only required during training
cfg=cfg_agent,
observation_space=env.observation_space,
state_space=env.state_space,
action_space=env.action_space,
device=env.device,
)
# import the agent and its default configuration
from skrl.agents.jax.dqn import DQN, DQN_CFG
# instantiate the agent's models
models = {}
models["q_network"] = ...
models["target_q_network"] = ... # only required during training
# adjust some configuration if necessary
cfg_agent = DQN_CFG()
cfg_agent.KEY = ...
# instantiate the agent
# (assuming a defined environment <env> and memory <memory>)
agent = DQN(
models=models,
memory=memory, # only required during training
cfg=cfg_agent,
observation_space=env.observation_space,
state_space=env.state_space,
action_space=env.action_space,
device=env.device,
)
Configuration and hyperparameters¶
Spaces¶
The implementation supports the following Gymnasium spaces:
Gymnasium spaces |
Observation |
Action |
|---|---|---|
Discrete |
\(\square\) |
\(\blacksquare\) |
MultiDiscrete |
\(\square\) |
\(\square\) |
Box |
\(\blacksquare\) |
\(\square\) |
Dict |
\(\blacksquare\) |
\(\square\) |
Models¶
The implementation uses 2 deterministic function approximators.
These function approximators (models) must be collected in a dictionary and passed to the constructor of the class
under the argument models.
Notation |
Concept |
Key |
Input shape |
Output shape |
Type |
|---|---|---|---|---|---|
\(Q_\phi(s, a)\) |
Q-network |
|
observation |
action |
|
\(Q_{\phi_{target}}(s, a)\) |
Target Q-network |
|
observation |
action |
Features¶
Support for advanced features is described in the following table:
Feature |
Support and remarks |
|
|
|
|---|---|---|---|---|
Shared model |
- |
\(\square\) |
\(\square\) |
\(\square\) |
RNN support |
- |
\(\square\) |
\(\square\) |
\(\square\) |
Mixed precision |
Automatic mixed precision |
\(\blacksquare\) |
\(\square\) |
\(\square\) |
Distributed |
Single Program Multi Data (SPMD) multi-GPU |
\(\blacksquare\) |
\(\blacksquare\) |
\(\square\) |
API¶
PyTorch¶
- class skrl.agents.torch.dqn.DQN_CFG(*, experiment: ExperimentCfg = <factory>, gradient_steps: int = 1, batch_size: int = 64, discount_factor: float = 0.99, polyak: float = 0.005, learning_rate: float = 0.001, learning_rate_scheduler: type | None = None, learning_rate_scheduler_kwargs: dict = <factory>, observation_preprocessor: type | None = None, observation_preprocessor_kwargs: dict = <factory>, state_preprocessor: type | None = None, state_preprocessor_kwargs: dict = <factory>, random_timesteps: int = 0, learning_starts: int = 0, update_interval: int = 1, target_update_interval: int = 10, exploration_scheduler: Callable[[int, int], float] | None = None, rewards_shaper: Callable | None = None, mixed_precision: bool = False)[source]¶
Bases:
AgentCfgConfiguration for the DQN agent.
Methods:
Attributes:
Batch size for sampling transitions from memory during training.
Parameter that balances the importance of future rewards (close to 1.0) versus immediate rewards (close to 0.0).
Experiment settings.
Epsilon-greedy exploration scheduler function.
Number of gradient steps to perform for each update.
Learning rate for the Q-network.
Learning rate scheduler class for the Q-network.
Keyword arguments for the learning rate scheduler's constructor.
Number of steps to perform before calling the algorithm update function.
Whether to enable automatic mixed precision for higher performance.
Preprocessor class to process the environment's observations.
Keyword arguments for the observation preprocessor's constructor.
Parameter to control the update of the target networks by polyak averaging.
Number of random exploration (sampling random actions) steps to perform before sampling actions from the policy.
Rewards shaping function.
Preprocessor class to process the environment's states.
Keyword arguments for the state preprocessor's constructor.
Number of algorithm updates to perform between target network updates.
Number of environment steps to perform between algorithm updates.
- discount_factor: float = 0.99¶
Parameter that balances the importance of future rewards (close to 1.0) versus immediate rewards (close to 0.0).
Range:
[0.0, 1.0].
- experiment: ExperimentCfg¶
Experiment settings.
- exploration_scheduler: Callable[[int, int], float] | None = None¶
Epsilon-greedy exploration scheduler function.
The function takes the current
timestepand the total number oftimestepsas arguments and returns an epsilon value used to sample greedy actions.
- learning_rate_scheduler: type | None = None¶
Learning rate scheduler class for the Q-network.
See Learning rate schedulers for more details.
- learning_rate_scheduler_kwargs: dict¶
Keyword arguments for the learning rate scheduler’s constructor.
See Learning rate schedulers for more details.
Warning
The
optimizerargument is automatically passed to the learning rate scheduler’s constructor. Therefore, it must not be provided in the keyword arguments.
- observation_preprocessor: type | None = None¶
Preprocessor class to process the environment’s observations.
See Preprocessors for more details.
- observation_preprocessor_kwargs: dict¶
Keyword arguments for the observation preprocessor’s constructor.
See Preprocessors for more details.
- polyak: float = 0.005¶
Parameter to control the update of the target networks by polyak averaging.
Range:
[0.0, 1.0]. Seeupdate_parameters()for more details.
- random_timesteps: int = 0¶
Number of random exploration (sampling random actions) steps to perform before sampling actions from the policy.
- state_preprocessor: type | None = None¶
Preprocessor class to process the environment’s states.
See Preprocessors for more details.
- state_preprocessor_kwargs: dict¶
Keyword arguments for the state preprocessor’s constructor.
See Preprocessors for more details.
- class skrl.agents.torch.dqn.DQN(*, models: dict[str, Model], memory: Memory | None = None, observation_space: gymnasium.Space | None = None, state_space: gymnasium.Space | None = None, action_space: gymnasium.Space | None = None, device: str | torch.device | None = None, cfg: DQN_CFG | dict = {})[source]¶
Bases:
AgentDeep Q-Network (DQN).
https://arxiv.org/abs/1312.5602
- Parameters:
models – Agent’s models.
memory – Memory to storage agent’s data and environment transitions.
observation_space – Observation space.
state_space – State space.
action_space – Action space.
device – Data allocation and computation device. If not specified, the default device will be used.
cfg – Agent’s configuration.
- Raises:
KeyError – If a configuration key is missing.
Methods:
act(observations, states, *, timestep, timesteps)Process the environment's observations/states to make a decision (actions) using the main policy.
enable_models_training_mode([enabled])Set the training mode of all the agent's models: enabled (training) or disabled (evaluation).
enable_training_mode([enabled, apply_to_models])Set the training mode of the agent: enabled (training) or disabled (evaluation).
init(*[, trainer_cfg])Initialize the agent.
load(path)Load the agent from the specified path.
post_interaction(*, timestep, timesteps)Method called after the interaction with the environment.
pre_interaction(*, timestep, timesteps)Method called before the interaction with the environment.
record_transition(*, observations, states, ...)Record an environment transition in memory.
save(path)Save the agent to the specified path.
track_data(tag, value)Track data to TensorBoard.
update(*, timestep, timesteps)Algorithm's main update step.
write_checkpoint(*, timestep, timesteps)Write checkpoint (modules) to persistent storage.
write_tracking_data(*, timestep, timesteps)Write tracking data to TensorBoard.
- act(observations: torch.Tensor, states: torch.Tensor | None, *, timestep: int, timesteps: int) tuple[torch.Tensor, dict[str, Any]][source]¶
Process the environment’s observations/states to make a decision (actions) using the main policy.
- Parameters:
observations – Environment observations.
states – Environment states.
timestep – Current timestep.
timesteps – Number of timesteps.
- Returns:
Agent output. The first component is the expected action/value returned by the agent. The second component is a dictionary containing extra output values according to the model.
- enable_models_training_mode(enabled: bool = True) None[source]¶
Set the training mode of all the agent’s models: enabled (training) or disabled (evaluation).
- Parameters:
enabled – True to enable the training mode, False to enable the evaluation mode.
- enable_training_mode(enabled: bool = True, *, apply_to_models: bool = False) None[source]¶
Set the training mode of the agent: enabled (training) or disabled (evaluation).
The training mode can be queried by the
trainingproperty.- Parameters:
enabled – True to enable the training mode, False to enable the evaluation mode.
apply_to_models – Whether to apply the training mode to all the agent’s models.
- init(*, trainer_cfg: dict[str, Any] | None = None) None[source]¶
Initialize the agent.
- Parameters:
trainer_cfg – Trainer configuration.
- load(path: str) None[source]¶
Load the agent from the specified path.
Note
The final storage device is determined by the constructor of the agent.
- Parameters:
path – Path to load the agent from.
- post_interaction(*, timestep: int, timesteps: int) None[source]¶
Method called after the interaction with the environment.
- Parameters:
timestep – Current timestep.
timesteps – Number of timesteps.
- pre_interaction(*, timestep: int, timesteps: int) None[source]¶
Method called before the interaction with the environment.
- Parameters:
timestep – Current timestep.
timesteps – Number of timesteps.
- record_transition(*, observations: torch.Tensor, states: torch.Tensor, actions: torch.Tensor, rewards: torch.Tensor, next_observations: torch.Tensor, next_states: torch.Tensor, terminated: torch.Tensor, truncated: torch.Tensor, infos: Any, timestep: int, timesteps: int) None[source]¶
Record an environment transition in memory.
- Parameters:
observations – Environment observations.
states – Environment states.
actions – Actions taken by the agent.
rewards – Instant rewards achieved by the current actions.
next_observations – Next environment observations.
next_states – Next environment states.
terminated – Signals that indicate episodes have terminated.
truncated – Signals that indicate episodes have been truncated.
infos – Additional information about the environment.
timestep – Current timestep.
timesteps – Number of timesteps.
- save(path: str) None[source]¶
Save the agent to the specified path.
- Parameters:
path – Path to save the agent to.
- track_data(tag: str, value: float) None[source]¶
Track data to TensorBoard.
Note
Currently only scalar data is supported.
- Parameters:
tag – Data identifier (e.g. ‘Loss/Policy loss’).
value – Value to track.
- update(*, timestep: int, timesteps: int) None[source]¶
Algorithm’s main update step.
- Parameters:
timestep – Current timestep.
timesteps – Number of timesteps.
- write_checkpoint(*, timestep: int, timesteps: int) None[source]¶
Write checkpoint (modules) to persistent storage.
Note
The checkpoints are stored in the subdirectory
checkpointswithin the experiment directory. The checkpoint name is thetimestepargument value (if it is notNone), or the current system date-time otherwise.- Parameters:
timestep – Current timestep.
timesteps – Number of timesteps.
JAX¶
- class skrl.agents.jax.dqn.DQN_CFG(*, experiment: ExperimentCfg = <factory>, gradient_steps: int = 1, batch_size: int = 64, discount_factor: float = 0.99, polyak: float = 0.005, learning_rate: float = 0.001, learning_rate_scheduler: type | None = None, learning_rate_scheduler_kwargs: dict = <factory>, observation_preprocessor: type | None = None, observation_preprocessor_kwargs: dict = <factory>, state_preprocessor: type | None = None, state_preprocessor_kwargs: dict = <factory>, random_timesteps: int = 0, learning_starts: int = 0, update_interval: int = 1, target_update_interval: int = 10, exploration_scheduler: Callable[[int, int], float] | None = None, rewards_shaper: Callable | None = None)[source]¶
Bases:
AgentCfgConfiguration for the DQN agent.
Methods:
Attributes:
Batch size for sampling transitions from memory during training.
Parameter that balances the importance of future rewards (close to 1.0) versus immediate rewards (close to 0.0).
Experiment settings.
Epsilon-greedy exploration scheduler function.
Number of gradient steps to perform for each update.
Learning rate for the Q-network.
Learning rate scheduler class for the Q-network.
Keyword arguments for the learning rate scheduler's constructor.
Number of steps to perform before calling the algorithm update function.
Preprocessor class to process the environment's observations.
Keyword arguments for the observation preprocessor's constructor.
Parameter to control the update of the target networks by polyak averaging.
Number of random exploration (sampling random actions) steps to perform before sampling actions from the policy.
Rewards shaping function.
Preprocessor class to process the environment's states.
Keyword arguments for the state preprocessor's constructor.
Number of algorithm updates to perform between target network updates.
Number of environment steps to perform between algorithm updates.
- discount_factor: float = 0.99¶
Parameter that balances the importance of future rewards (close to 1.0) versus immediate rewards (close to 0.0).
Range:
[0.0, 1.0].
- experiment: ExperimentCfg¶
Experiment settings.
- exploration_scheduler: Callable[[int, int], float] | None = None¶
Epsilon-greedy exploration scheduler function.
The function takes the current
timestepand the total number oftimestepsas arguments and returns an epsilon value used to sample greedy actions.
- learning_rate_scheduler: type | None = None¶
Learning rate scheduler class for the Q-network.
See Learning rate schedulers for more details.
- learning_rate_scheduler_kwargs: dict¶
Keyword arguments for the learning rate scheduler’s constructor.
See Learning rate schedulers for more details.
Warning
The
optimizerargument is automatically passed to the learning rate scheduler’s constructor. Therefore, it must not be provided in the keyword arguments.
- observation_preprocessor: type | None = None¶
Preprocessor class to process the environment’s observations.
See Preprocessors for more details.
- observation_preprocessor_kwargs: dict¶
Keyword arguments for the observation preprocessor’s constructor.
See Preprocessors for more details.
- polyak: float = 0.005¶
Parameter to control the update of the target networks by polyak averaging.
Range:
[0.0, 1.0]. Seeupdate_parameters()for more details.
- random_timesteps: int = 0¶
Number of random exploration (sampling random actions) steps to perform before sampling actions from the policy.
- state_preprocessor: type | None = None¶
Preprocessor class to process the environment’s states.
See Preprocessors for more details.
- state_preprocessor_kwargs: dict¶
Keyword arguments for the state preprocessor’s constructor.
See Preprocessors for more details.
- class skrl.agents.jax.dqn.DQN(*, models: dict[str, Model], memory: Memory | None = None, observation_space: gymnasium.Space | None = None, state_space: gymnasium.Space | None = None, action_space: gymnasium.Space | None = None, device: str | jax.Device | None = None, cfg: DQN_CFG | dict = {})[source]¶
Bases:
AgentDeep Q-Network (DQN).
https://arxiv.org/abs/1312.5602
- Parameters:
models – Agent’s models.
memory – Memory to storage agent’s data and environment transitions.
observation_space – Observation space.
state_space – State space.
action_space – Action space.
device – Data allocation and computation device. If not specified, the default device will be used.
cfg – Agent’s configuration.
- Raises:
KeyError – If a configuration key is missing.
Methods:
act(observations, states, *, timestep, timesteps)Process the environment's observations/states to make a decision (actions) using the main policy.
enable_models_training_mode([enabled])Set the training mode of all the agent's models: enabled (training) or disabled (evaluation).
enable_training_mode([enabled, apply_to_models])Set the training mode of the agent: enabled (training) or disabled (evaluation).
init(*[, trainer_cfg])Initialize the agent.
load(path)Load the agent from the specified path.
post_interaction(*, timestep, timesteps)Method called after the interaction with the environment.
pre_interaction(*, timestep, timesteps)Method called before the interaction with the environment.
record_transition(*, observations, states, ...)Record an environment transition in memory.
save(path)Save the agent to the specified path.
track_data(tag, value)Track data to TensorBoard.
update(*, timestep, timesteps)Algorithm's main update step.
write_checkpoint(*, timestep, timesteps)Write checkpoint (modules) to persistent storage.
write_tracking_data(*, timestep, timesteps)Write tracking data to TensorBoard.
- act(observations: jax.Array, states: jax.Array | None, *, timestep: int, timesteps: int) tuple[jax.Array, dict[str, Any]][source]¶
Process the environment’s observations/states to make a decision (actions) using the main policy.
- Parameters:
observations – Environment observations.
states – Environment states.
timestep – Current timestep.
timesteps – Number of timesteps.
- Returns:
Agent output. The first component is the expected action/value returned by the agent. The second component is a dictionary containing extra output values according to the model.
- enable_models_training_mode(enabled: bool = True) None[source]¶
Set the training mode of all the agent’s models: enabled (training) or disabled (evaluation).
- Parameters:
enabled – True to enable the training mode, False to enable the evaluation mode.
- enable_training_mode(enabled: bool = True, *, apply_to_models: bool = False) None[source]¶
Set the training mode of the agent: enabled (training) or disabled (evaluation).
The training mode can be queried by the
trainingproperty.- Parameters:
enabled – True to enable the training mode, False to enable the evaluation mode.
apply_to_models – Whether to apply the training mode to all the agent’s models.
- init(*, trainer_cfg: dict[str, Any] | None = None) None[source]¶
Initialize the agent.
- Parameters:
trainer_cfg – Trainer configuration.
- load(path: str) None[source]¶
Load the agent from the specified path.
- Parameters:
path – Path to load the agent from.
- post_interaction(*, timestep: int, timesteps: int) None[source]¶
Method called after the interaction with the environment.
- Parameters:
timestep – Current timestep.
timesteps – Number of timesteps.
- pre_interaction(*, timestep: int, timesteps: int) None[source]¶
Method called before the interaction with the environment.
- Parameters:
timestep – Current timestep.
timesteps – Number of timesteps.
- record_transition(*, observations: jax.Array, states: jax.Array, actions: jax.Array, rewards: jax.Array, next_observations: jax.Array, next_states: jax.Array, terminated: jax.Array, truncated: jax.Array, infos: Any, timestep: int, timesteps: int) None[source]¶
Record an environment transition in memory.
- Parameters:
observations – Environment observations.
states – Environment states.
actions – Actions taken by the agent.
rewards – Instant rewards achieved by the current actions.
next_observations – Next environment observations.
next_states – Next environment states.
terminated – Signals that indicate episodes have terminated.
truncated – Signals that indicate episodes have been truncated.
infos – Additional information about the environment.
timestep – Current timestep.
timesteps – Number of timesteps.
- save(path: str) None[source]¶
Save the agent to the specified path.
- Parameters:
path – Path to save the agent to.
- track_data(tag: str, value: float) None[source]¶
Track data to TensorBoard.
Note
Currently only scalar data is supported.
- Parameters:
tag – Data identifier (e.g. ‘Loss/Policy loss’).
value – Value to track.
- update(*, timestep: int, timesteps: int) None[source]¶
Algorithm’s main update step.
- Parameters:
timestep – Current timestep.
timesteps – Number of timesteps.
- write_checkpoint(*, timestep: int, timesteps: int) None[source]¶
Write checkpoint (modules) to persistent storage.
Note
The checkpoints are stored in the subdirectory
checkpointswithin the experiment directory. The checkpoint name is thetimestepargument value (if it is notNone), or the current system date-time otherwise.- Parameters:
timestep – Current timestep.
timesteps – Number of timesteps.