DQN code = trains the model
PyTorch Profiler = measures what takes time during training
1. Import Gymnasium
2. Import PyTorch
- Tensors
- Neural networks
- Calculating loss
- Backpropagation
- Updating model parameters
- Profiling
3. Import neural network tools
nn provides neural-network layers.
For example:
4. Import optimizer
5. Create CartPole
6. Create the DQN model
7. Create optimizer
model.parameters()
Gets all trainable weights from the network.
lr=0.001
Learning rate.
It controls how much the weights change after each training step.
8. Start the profiler
This is the major new part.“While the following code is running, monitor and record what the program is doing.”
What is a profiler?
A profiler helps answer questions like:9. CPU profiling
10. record_shapes=True
11. Start training
Everything inside:12. Reset environment
13. Maximum 100 steps
14. Convert state to tensor
15. Forward pass
16. Select action
argmax() finds the largest Q-value.
If: