Policy Gradient with PyTorch
Policy-gradient methods optimize a stochastic policy directly via gradient ascent on expected return, instead of deriving a policy from learned Q-values as in Deep Q-Learning. Their advantages are built-in exploration, robustness to perceptual aliasing, and handling of continuous or high-dimensional action spaces. Their drawbacks are convergence to local maxima, slow training and high variance. Reinforce (Monte Carlo Policy Gradient) collects a full episode, estimates the gradient from the log-probabilities of the actions weighted by the return, and updates the weights. A linked Colab notebook implements it in PyTorch on CartPole-v1, PixelCopter and Pong.
Related posts
- Advantage Actor Critic (A2C)
Hugging Face mediumAdeptActor-Critic methods reduce the high variance of Monte-Carlo policy-gradient estimates in Reinforce by pairing a policy network (Actor) with a value network (Critic) that scores the actions taken. A2C stabilizes training further by using the advantage function, which measures how much better an action is than the state's average value. The TD error serves as a practical estimator, so separate Q and V networks aren't needed. The unit closes with a Colab exercise training bipedal and spider walkers in PyBullet with Stable-Baselines3.
- Proximal Policy Optimization (PPO)
Hugging Face hardMasterProximal Policy Optimization stabilizes policy-gradient training by limiting how far each update moves the policy. It computes the probability ratio between the current and old policy and clips it to [1−ε, 1+ε] (ε=0.2 in the paper). It then takes the minimum of the clipped and unclipped advantage-weighted objectives, so the gradient goes to zero when an update would push the ratio further outside the range. Six ratio/advantage cases show when updates happen, and the method is contrasted with TRPO's KL constraint. The final actor-critic loss combines the clipped objective, a value loss and an entropy bonus, followed by a pointer to a from-scratch PyTorch implementation trained on CartPole-v1 and LunarLander-v2.
- An Introduction to Deep Reinforcement Learning
Hugging Face easyNoviceDeep reinforcement learning trains an agent to act in an environment through trial and error, using rewards as its only feedback, with the goal of maximizing expected cumulative (discounted) return. The core concepts are the state/action/reward loop, the Markov property, observation vs. state, discrete vs. continuous action spaces, discounting with gamma (typically 0.95–0.99), episodic vs. continuing tasks, and the exploration/exploitation tradeoff. The two main ways to learn an optimal policy are policy-based methods, which map states directly to actions or action distributions, and value-based methods, which learn state values and act greedily toward them. It is the first unit of Hugging Face's free Deep RL course, which goes on to train a lunar lander agent with Stable Baselines3.
- Engineering and algorithmic interventions for multimodal post-training at Microsoft scale
Engineering@Microsoft hardMasterProduction-scale RL post-training of multimodal Copilot agents fails silently because policy gradient advantage estimates lose informativeness even as aggregate reward climbs. Five interventions address this: a staged curriculum (verifiable rewards only for the first 30% of training, with an entropy floor), an adaptive loop that injects near-miss trajectories when effective sample size drops below 20%, per-trajectory gradient normalization by σ_source·sqrt(length) to cut gradient concentration (Gini ~0.75 to ~0.35), softplus constraint penalties plus an annealed parallelism reward to avoid serial collapse, and mixed-horizon sampling instead of horizon curricula. ESS collapse proved a leading indicator of learning stalls by about 35 epochs, and each fix comes with explicit costs such as ~15% memory overhead or lower peak single-horizon performance.
- Introducing Decision Transformers on Hugging Face 🤗
Hugging Face mediumAdeptDecision Transformer, an offline reinforcement learning method that recasts RL as return-conditioned sequence modeling, is now part of the 🤗 transformers library, with nine pretrained checkpoints for Gym continuous-control tasks (Hopper, Walker2D, HalfCheetah). Instead of fitting a value function, a GPT-2 model takes the last K timesteps of return-to-go, state and action tokens and autoregressively predicts actions that reach a desired return. Code covers loading the model, padding and masking a 20-step context window, normalizing states with the mean and standard deviation from each model card, and running an evaluation loop. Setting the target return directly controls how well the resulting policy performs.