DQN
lerax.algorithm.DQN
Bases: AbstractAlgorithm[PolicyType, DQNState[PolicyType]]
Double Deep Q-Network (Double DQN) algorithm.
Uses the online network to select actions and the target network to evaluate them, which reduces overestimation bias compared to standard DQN.
The target network is a periodic copy of the online network, updated
every target_update_interval iterations.
Attributes:
| Name | Type | Description |
|---|---|---|
optimizer |
optax.GradientTransformation
|
The optimizer used for training. |
buffer_size |
int
|
The size of the replay buffer. |
gamma |
float
|
Discount factor for future rewards. |
learning_starts |
int
|
Number of initial steps to collect before training. |
num_envs |
int
|
Number of parallel environments. |
num_steps |
int
|
Number of steps per iteration. |
batch_size |
int
|
Batch size for training. |
target_update_interval |
int
|
How often to copy the online network to the target. |
max_grad_norm |
float
|
Maximum gradient norm for gradient clipping. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
buffer_size
|
int
|
The size of the replay buffer. |
1000000
|
gamma
|
float
|
Discount factor for future rewards. |
0.99
|
learning_starts
|
int
|
Number of initial steps to collect before training. |
100
|
num_envs
|
int
|
Number of parallel environments. |
1
|
num_steps
|
int
|
Number of steps per iteration. |
4
|
batch_size
|
int
|
Batch size for training. |
32
|
target_update_interval
|
int
|
How often to copy the online network to the target. |
10000
|
max_grad_norm
|
float
|
Maximum gradient norm for gradient clipping. |
10.0
|
learning_rate
|
optax.ScalarOrSchedule
|
Learning rate for the optimizer. |
0.0001
|
dqn_loss_grad
class-attribute
instance-attribute
consolidate_callbacks
consolidate_callbacks(
callback: Sequence[AbstractCallback]
| AbstractCallback
| None = None,
) -> AbstractCallback
learn
learn(
env: AbstractEnvLike,
policy: PolicyType,
total_timesteps: int,
*,
key: Key[Array, ""],
callback: Sequence[AbstractCallback]
| AbstractCallback
| None = None,
) -> PolicyType
Train the policy on the environment for a given number of timesteps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
AbstractEnvLike
|
The environment to train on. |
required |
policy
|
PolicyType
|
The policy to train. |
required |
total_timesteps
|
int
|
The total number of timesteps to train for. |
required |
key
|
Key[Array, '']
|
A JAX PRNG key. |
required |
callback
|
Sequence[AbstractCallback] | AbstractCallback | None
|
A callback or list of callbacks to use during training. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
policy |
PolicyType
|
The trained policy. |
__init__
__init__(
*,
buffer_size: int = 1000000,
gamma: float = 0.99,
learning_starts: int = 100,
num_envs: int = 1,
num_steps: int = 4,
batch_size: int = 32,
target_update_interval: int = 10000,
max_grad_norm: float = 10.0,
learning_rate: optax.ScalarOrSchedule = 0.0001,
)
step
step(
env: AbstractEnvLike,
policy: PolicyType,
state: DQNStepState[PolicyType],
*,
key: Key[Array, ""],
callback: AbstractCallback,
) -> DQNStepState[PolicyType]
Perform a single environment step and store in replay buffer.
collect_learning_starts
collect_learning_starts(
env: AbstractEnvLike,
policy: PolicyType,
step_state: DQNStepState[PolicyType],
callback: AbstractCallback,
key: Key[Array, ""],
) -> DQNStepState[PolicyType]
Collect random initial experience before training begins.
collect_rollout
collect_rollout(
env: AbstractEnvLike,
policy: PolicyType,
step_state: DQNStepState[PolicyType],
callback: AbstractCallback,
key: Key[Array, ""],
) -> DQNStepState[PolicyType]
Collect a rollout of experience into the replay buffer.
reset
reset(
env: AbstractEnvLike,
policy: PolicyType,
*,
key: Key[Array, ""],
callback: AbstractCallback,
) -> DQNState[PolicyType]
iteration
iteration(
state: DQNState[PolicyType],
*,
key: Key[Array, ""],
callback: AbstractCallback,
) -> DQNState[PolicyType]
per_iteration
Periodically update the target network.
lerax.algorithm.DQNState
Bases: AbstractAlgorithmState[PolicyType]
Iteration-level state for DQN.
Attributes:
| Name | Type | Description |
|---|---|---|
iteration_count |
Int[Array, '']
|
The current iteration count. |
step_state |
DQNStepState[PolicyType]
|
The step-level state. |
env |
AbstractEnvLike
|
The environment being used. |
policy |
PolicyType
|
The online policy being trained. |
opt_state |
optax.OptState
|
The optimizer state. |
callback_state |
AbstractCallbackState
|
The callback state. |
target_policy |
PolicyType
|
The target policy for stable Q-value estimation. |
next
next[A: AbstractAlgorithmState](
step_state: AbstractStepState,
policy: PolicyType,
opt_state: optax.OptState,
) -> A
Return a new algorithm state for the next iteration.
Increments the iteration count and updates the step state, policy, and optimizer state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step_state
|
AbstractStepState
|
The new step state. |
required |
policy
|
PolicyType
|
The new policy. |
required |
opt_state
|
optax.OptState
|
The new optimizer state. |
required |
Returns:
| Type | Description |
|---|---|
A
|
A new algorithm state with the updated fields. |
with_callback_states
Return a new algorithm state with the given callback state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callback_state
|
AbstractCallbackState
|
The new callback state. |
required |
Returns:
| Type | Description |
|---|---|
A
|
A new algorithm state with the updated callback state. |