---
title: "Policy Gradient with PyTorch"
description: "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."
url: "https://swe.quest/articles/policy-gradient-with-pytorch-8cc397d15e17e5f8"
canonical: "https://huggingface.co/blog/deep-rl-pg"
source: "Hugging Face"
published: "2022-06-30"
difficulty: "easy"
kind: "How-to"
reading_minutes: 7
tags: ["machine-learning","model-training","python"]
---

# Policy Gradient with PyTorch

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2022-06-30, 7 min read, How-to. Original: [https://huggingface.co/blog/deep-rl-pg](https://huggingface.co/blog/deep-rl-pg)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Easy: no special background needed; explains what something is and why it matters

## Topics

- [Machine learning](https://swe.quest/tags/machine-learning.md) (AI & ML)
- [Model training](https://swe.quest/tags/model-training.md) (AI & ML)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- reinforcement learning basics ([easy posts on Model training](https://swe.quest/tags/model-training.md?difficulty=easy))
- Deep Q-Learning
- gradient descent

## Keywords

Reinforce, Policy Gradient, PyTorch, CartPole-v1, Deep Q-Learning, Hugging Face Deep RL Course

## Related posts

- [Advantage Actor Critic (A2C)](https://swe.quest/articles/advantage-actor-critic-a2c-5b4f17254e3ab304.md): Hugging Face · 2022-07-22 · Medium · 7 min read · Machine learning, Model training, Algorithms
  Actor-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.
- [Proximal Policy Optimization (PPO)](https://swe.quest/articles/proximal-policy-optimization-ppo-c85a21363ebb9f11.md): Hugging Face · 2022-08-05 · Hard · 10 min read · Model training, Machine learning, Algorithms
  Proximal Policy Optimization stabilizes policy-gradient training by limiting how far each update moves the policy.
- [An Introduction to Deep Reinforcement Learning](https://swe.quest/articles/an-introduction-to-deep-reinforcement-learning-d17541fa32bf28e1.md): Hugging Face · 2022-05-04 · Easy · 16 min read · Machine learning, Model training
  Deep 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.
- [Engineering and algorithmic interventions for multimodal post-training at Microsoft scale](https://swe.quest/articles/engineering-and-algorithmic-interventions-for-multimodal-post-training-at-fe8208ec9b40ef0c.md): Engineering@Microsoft · 2026-02-27 · Hard · 10 min read · Model training, AI agents, LLMs, Machine learning
  Production-scale RL post-training of multimodal Copilot agents fails silently because policy gradient advantage estimates lose informativeness even as aggregate reward climbs.
- [Introducing Decision Transformers on Hugging Face 🤗](https://swe.quest/articles/introducing-decision-transformers-on-hugging-face-53b9486d51c912ca.md): Hugging Face · 2022-03-28 · Medium · 8 min read · Machine learning, Python, Open source
  Decision 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).

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
