Iliad

Policy Gradients and Misgeneralization

Cluster DD.2 · Policy Gradients and Misgeneralization

Implement Vanilla Policy Gradient to train a neural network using reinforcement learning, and explore emperically the concepts of goal misgeneralisation in a toy environment.

By David Quarel (ARENA), based on work by Matthew Farrugia-Roberts (University of Oxford)

When Bad Goals ALSO Fit Your Training Data — Watch on YouTube ↗

Prerequisites

  • D.1.2 - Reinforcement Learning
  • Be familar with the PyTorch prerequisites.
    • In particular, if you're unfamilar with more advanced indexing like use of torch.gather, feel free to solve the exercise with a sequential python for-loop, and then use the solution code when you want the training loop to run quickly.
What you’ll learn
  • Understand the derivation of (Vanilla) Policy Gradient (VPG).
  • Implement Policy Gradient in PyTorch, and use to train an agent to play a toy RL environment.
  • Understand reward functions, what behaviour they incentivise and whow they can be used as probes for behaviour.
  • Understand what potential shaping is, and how it provides hints without allowing reward hacking.
  • The difference between Goal Misgeneralisation and Goal Misspecification.

Roadmap for today

  • 10:00–10:30
    • Lecture on intro to Policy Gradient
  • 10:30–13:00
    • Work through VPG material
  • 13:00-14:00
    • (Lunch + Break Time)
  • 14:00–14:30
    • Lecture on intro to Goal Misgeneralization
  • 14:30–17:30
    • Work through Goal Misgen. material
  • 17:30–18:00
    • Buffer + Feedback

Vanilla Policy Gradient

Work through Section [2.2.2] of the ARENA program on Vanilla Policy Gradient. Follow the provided link, and then through to the exercises on colab.

  • The network is small enough, and the training loop fast enough, that you can run this material locally on your laptop without problems if you prefer.

Goal Misgeneralization

Work through the provided colab notebook on Goal Misgeneralisation.

  • The network is again small, but some of the later runs might be slow without a GPU, so we would recommend against running this locally unless you have a GPU in your laptop.

Potential shaping

The notebook shapes the reward with a potential function. These exercises show this cannot change the optimal policy.

Definition (Potential shaping). Take an MDP with states , reward and discount . A potential is a bounded . The shaped reward is

For a trajectory , taking action in state returns the next state and reward . Write , and .

Exercise 1. Show that

Solution

With ,

so

Exercise 2. Show that . Where are boundedness of and used?

Solution

Truncate at and apply Exercise 1:

If then as , since . Letting gives . (An unbounded could grow like , and the boundary term would not vanish.)

Exercise 3. Show that for every policy .

Solution

Using Exercise 2, and that under the conditioning:

Exercise 4. Assume and the action set are finite, so an optimal policy exists (D.1.2, Exercise 3.7). Show that and have the same optimal policies, and that .

Solution

Averaging Exercise 3 over gives . The shift is the same for every policy, so for all policies and states

A policy is optimal if its value is at least that of every other policy in every state, so is optimal for iff it is optimal for .

Now take an optimal policy for , which exists by D.1.2. It is also optimal for , so by Exercise 3

(For episodic tasks the same holds if at terminal states, so the boundary term in Exercise 2 vanishes.)

Exercise 5. In the notebook, while holding a shard and otherwise. What shaping bonus does picking up a shard and dropping it again earn? What if we instead paid per PICKUP?

Solution

The robot goes with and . The discounted shaping bonus is

Paying per pickup instead, repeating the cycle forever earns , so an agent can farm it rather than bin shards.