DQN¶
Figure: DQN algorithm pseudocode 1
toyrl.dqn.simple_config
module-attribute
¶
simple_config = DqnConfig(env_name='CartPole-v1', render_mode=None, solved_threshold=475.0, max_training_steps=500000, learning_rate=0.00025, use_target_network=True, target_soft_update_beta=0.0, target_update_frequency=5, log_wandb=True)
toyrl.dqn.DqnConfig
dataclass
¶
DqnConfig(env_name: str = 'CartPole-v1', render_mode: str | None = None, solved_threshold: float = 475.0, gamma: float = 0.999, replay_buffer_capacity: int = 10000, max_training_steps: int = 500000, learning_starts: int = 10000, policy_update_frequency: int = 10, batches_per_training_step: int = 16, batch_size: int = 128, updates_per_batch: int = 1, learning_rate: float = 0.01, use_target_network: bool = False, target_update_frequency: int = 10, target_soft_update_beta: float = 0.0, log_wandb: bool = False)
Configuration for DQN algorithm.
gamma
class-attribute
instance-attribute
¶
gamma: float = 0.999
The discount factor for future rewards.
replay_buffer_capacity
class-attribute
instance-attribute
¶
replay_buffer_capacity: int = 10000
The maximum capacity of the experience replay buffer.
max_training_steps
class-attribute
instance-attribute
¶
max_training_steps: int = 500000
The maximum number of environment steps to train for.
learning_starts
class-attribute
instance-attribute
¶
learning_starts: int = 10000
The number of steps to collect before starting learning.
policy_update_frequency
class-attribute
instance-attribute
¶
policy_update_frequency: int = 10
How often to update the policy network (in environment steps).
batches_per_training_step
class-attribute
instance-attribute
¶
batches_per_training_step: int = 16
The number of experience batches to sample in each training step.
batch_size
class-attribute
instance-attribute
¶
batch_size: int = 128
The size of each training batch.
updates_per_batch
class-attribute
instance-attribute
¶
updates_per_batch: int = 1
The number of optimization steps to perform on each batch.
learning_rate
class-attribute
instance-attribute
¶
learning_rate: float = 0.01
The learning rate for the optimizer.
use_target_network
class-attribute
instance-attribute
¶
use_target_network: bool = False
Whether to use a separate target network (Double DQN when True).
target_update_frequency
class-attribute
instance-attribute
¶
target_update_frequency: int = 10
How often to update the target network (in environment steps).
toyrl.dqn.PolicyNet
¶
Bases: Module
Source code in toyrl/dqn.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
|
forward
¶
forward(x: Tensor) -> Tensor
Source code in toyrl/dqn.py
74 75 |
|
toyrl.dqn.Experience
dataclass
¶
toyrl.dqn.ReplayBuffer
dataclass
¶
ReplayBuffer(replay_buffer_size: int = 10000, buffer: list[Experience] = list(), _head_pointer: int = 0)
__len__
¶
__len__() -> int
Source code in toyrl/dqn.py
94 95 |
|
add_experience
¶
add_experience(experience: Experience) -> None
Source code in toyrl/dqn.py
97 98 99 100 101 102 103 104 105 106 107 |
|
reset
¶
reset() -> None
Source code in toyrl/dqn.py
109 110 111 |
|
sample
¶
sample(batch_size: int) -> list[Experience]
Source code in toyrl/dqn.py
113 114 |
|
toyrl.dqn.Agent
¶
Agent(policy_net: PolicyNet, target_net: PolicyNet | None, optimizer: Optimizer, replay_buffer_size: int)
Source code in toyrl/dqn.py
118 119 120 121 122 123 124 125 126 127 128 129 |
|
add_experience
¶
add_experience(experience: Experience) -> None
Source code in toyrl/dqn.py
131 132 |
|
act
¶
Source code in toyrl/dqn.py
134 135 136 137 138 139 140 |
|
sample
¶
sample(batch_size: int) -> list[Experience]
Source code in toyrl/dqn.py
142 143 |
|
policy_update
¶
policy_update(gamma: float, experiences: list[Experience]) -> float
Source code in toyrl/dqn.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
|
polyak_update
¶
polyak_update(beta: float) -> None
Source code in toyrl/dqn.py
175 176 177 178 179 180 |
|
toyrl.dqn.DqnTrainer
¶
DqnTrainer(config: DqnConfig)
Source code in toyrl/dqn.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
|
agent
instance-attribute
¶
agent = Agent(policy_net=policy_net, target_net=target_net, optimizer=optimizer, replay_buffer_size=replay_buffer_capacity)
train
¶
train() -> None
Source code in toyrl/dqn.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
|
-
L. Graesser and W. L. Keng, Foundations of deep reinforcement learning: Theory and practice in python. Addison-Wesley Professional, 2019. ↩