How to?

285-hw2: Policy Gradients

Jan 1, 2025
技术学习285
2 Minutes
349 Words

Takeaways:

  1. cartpole 训练更稳定的方法:large batch (10000 vs 1000) 训练更稳定, reward-to-to(vs 每一步优势函数=整条轨迹G), normalized advantage

Experiment 1 (CartPole):

default

default

回答问题:

  • Which value estimator has better performance without advantage normalization: the trajectory- centric one, or the one using reward-to-go?
    • rtg.
  • Between the two value estimators, why do you think one is generally preferred over the other?
    • rtg. 因为这样能反映每个 action 的 value 而不是整条轨迹,更好学习. [ai]: 减少 credit assignment 的噪声 i.e. 减少“把奖励归因给不相关动作”的情况.
  • Did advantage normalization help?
    • yes.
  • Did the batch size make an impact?
    • yes.

解题过程失误:

  1. get_action 不是 argmax 使用最大概率的策略而应当使用 dist=torch.distributions.Categorical(logits=logits) 然后 dist.sample(). 那种 argmax 写法是 LLM 的.
  2. 求梯度不是 -prob * advantage 别忘了是 -log_prob * advantages
  3. 除以 std 需要 std + 1e-8

复现:

  • [email protected]
    /hw285.git ; cd hw2
  • uv run src/scripts/run.py —env_name CartPole-v0 -n 100 -b 1000
    -exp_name cartpole # 直接使用了 pdf 的命令

回忆提纲:

  • 代码中每次 forward 的实际 batch size 确实是不一样的。因为代码 rollout 过程是采样一整条 traj,traj 的每个 action step 作为一个 sample,所以 batch 可能是 20 + 17 + ... 这样随机累加直到满足预定 batch size.
  • 默认 MLP: 2 隐藏层,每层 64 神经元. 本任务输入是 4 维观测 [小车位置, 小车速度, 杆子角度, 杆尖角速度],输出是 1 个离散动作(2 选 1:向左或向右推小车)
Article title:285-hw2: Policy Gradients
Article author:Julyfun
Release time:Jan 1, 2025
Copyright 2026
Sitemap