Code Architecture — Asynchronous Methods for Deep RL (A3C) Notebook
Overview
The notebook trains a small actor-critic network on CartPole-v1 with the A3C loss: n-step returns, an advantage-weighted policy gradient, a value loss and an entropy bonus. It is a single-process simplification: one process steps 8 environments in turn and makes one synchronous update from all of them, so there are no asynchronous threads and no parallel speed-up. It runs on the CPU.
Notebook Structure
1. Setup & Imports
- PyTorch, gymnasium, NumPy, matplotlib; nothing is installed
- Seed 42 for
random, NumPy and torch;CUBLAS_WORKSPACE_CONFIGset before importing torch, cuDNN deterministic,torch.use_deterministic_algorithms(True, warn_only=True) - Figures as JPEG at 80 dpi; on Kaggle an
atexithook and a small background process copy the executed notebook toexecuted_solution.ipynb device = cpu(the network is tiny)
2. Hyperparameters
CartPole-v1, state size 4, 2 actions, hidden size 128- Adam, learning rate 3e-3 (eps 1e-5), decayed linearly to 10% of its value over training
- γ = 0.99, t_max = 5, entropy weight 0.01 (all three from the paper), value-loss weight 0.5, gradient clipping at norm 0.5
- 8 environments, 500 updates
3. Actor-Critic Network (ActorCritic)
- Shared body: Linear(4, 128) → Tanh → Linear(128, 128) → Tanh
- Actor head Linear(128, 2) → logits →
Categoricaldistribution; critic head Linear(128, 1) → V(s) forwardreturns the distribution, the value and the entropy- 17,539 parameters; one Adam optimizer
4. Environments (MultiEnv)
- 8
gym.make('CartPole-v1')copies, seeded 42 to 49 step_allsteps them one after another and resets an environment when its episode ends- A 2-environment test prints the observation shape (2, 4)
5. A3C Training Loop (train_a3c)
- For each update: collect 5 steps from all 8 environments, keeping log-probabilities, values and entropies
- Bootstrap from V(s) of the last state (no gradient); n-step returns stop at the end of an episode
- Advantage = return − V(s) (V detached); loss = policy loss + 0.5 × value MSE − 0.01 × entropy, averaged over the 5 × 8 steps
- Gradient clipping, Adam step; records entropy, value loss and the mean reward of the last 20 episodes
- Prints progress every 30 updates
6. Run Training
- Runs
train_a3c()and prints the number of episodes, the final mean reward of the last 20 episodes and the best episode
7. Visualization: Reward Curve
- Episode rewards with a 20-episode moving average and the 500 maximum, and the per-update mean reward
8. Visualization: Policy Entropy
- Entropy per update with the maximum ln(2); prints the start, final, min and max entropy and the mean over the first and last 50 updates
9. Visualization: Value Loss
- Value loss per update; prints the start and final value and the mean over the first and last 50 updates
10. Combined View: Reward & Entropy
- Smoothed mean reward and entropy on two y-axes; prints the final mean reward of the last 20 episodes
11. Evaluation
- 10 episodes sampling actions from the policy (mean, std, min, max and each reward) and 10 episodes with greedy actions (mean and std), on an environment seeded 999
12. Summary
### What the run above showswith the saved run’s numbers
Key Shapes
| Tensor | Shape |
|---|---|
| States for one step | (8, 4) |
| Logits / values / entropies for one step | (8, 2) / (8,) / (8,) |
| Stacked log-probs, values, entropies, returns | (5, 8) |
Deliberate Simplifications vs Full Paper
- No asynchrony: one process and one synchronous update per 5 × 8 steps, instead of 16 actor-learner threads updating a shared model (Supplementary Section 8).
- Task and network: CartPole-v1 with a 2-layer MLP, instead of Atari frames with a CNN.
- Optimiser: Adam with linear decay, instead of RMSProp with statistics shared across threads.
- Length: 500 updates (448 episodes), far shorter than the paper’s days of training.