Notes / Reinforcement Learning
Reward functions
Introduction
Given an MDP - representing state space, action space, transition kernel, reward function, initial state distribution, and discount factor - and the standard objective , we design reward functions to shape the behaviour of a policy. Reward function failure modes can be largely grouped into two:
- Misspecification, whereby the reward’s optimum is not what we wanted. The agent succeeds at the stated problem but the reward didn’t represent the desired behavior.
- Uninformativeness, whereby the reward’s optimum is correct but unreachable by the optimizer. The reward carries too little gradient to guide the search, classically because it is too sparse.
The obvious cure for an uninformative reward is to add dense intermediate rewards for behaviours we deem useful, though each such addition adds the risk of misspecification whereby the reward now rewards the hypothesis about good behavior rather than the desired outcome. The obvious cure for misspecification is to reward only the true outcome, which is typically sparse. This is the primary tradeoff in reward design.
Basic Transformations of a Reward
Reinforcement learning rests on the premise that goals and purposes can be adequately represented in a single, scalar signal through the sum of individual terms. Writing specifies a tradeoff between individual objectives. The optimizer will find and exploit that exchange rate at the extremes, where our intuitions about the weights were never tested.
Safe Transformations
-
Positive Scaling - for , replacing with does not change the optimal policy set. Hence, the absolute reward magnitudes do not matter but relative magnitudes between terms do. One caveat, however, is that this invariance only holds for the unregularized objective. With a fixed entropy coefficient or temperature, scaling changes the optimal policy.
-
Constant Offsets - while replacing with may seem innocent, this is only the case in the infinite-horizon case. But in the episodic setting with a variable termination time , the shift is:
which depends on . It may be more clear to view it this way:
A positive bias rewards survival, while a negative one punishes it. With a positive reward, an agent can be rewarded simply for existing without accomplishing the desired task. Hence, a policy is only invariant to a bias when the episode length doesn’t depend on the policy.
To see this in action, consider a corridor with a crash state at one end and the goal at the other, both terminal. The agent starts two cells from the crash and six from the goal. Every step pays , and entering the goal pays an additional . The figure below solves for the optimal policy with value iteration and runs it from . The plot underneath shows - the return that the bonus alone adds to an episode of length .
A few things worth trying:
- Start at , where the agent simply walks to the goal. Slowly increase - at around (with ), staying alive becomes worth more than the goal and the agent paces back and forth forever.
- Now push negative. Below roughly , each step costs enough that the agent takes the shortest way out of the episode, even though that means crashing.
- Increase towards and repeat the first experiment. The effective horizon grows, so a bonus of only is now enough to make the agent never finish.
Potential-Based Reward Shaping
Potential-based reward shaping involves assigning each state a score and paying the agent the change in that score as it moves. This is an alternative to rewarding the action itself. The danger comes from a potential function that creates a cycle in the state space which the agent can farm without achieving the goal itself. Any cycle in the state space must have a net reward of zero to ensure this is impossible. Another important caveat is that adding correct potential-based reward shaping does not change the optimal policy set, it simply changes (shapes) how easy it is for the optimizer to discover it.
Suppose we augment with an additional shaping term , producing . The shaping term is governed by the potential function . When is the optimal policy unchanged?
Theorem: Let be any bounded function and define Then every optimal policy of is optimal in and vice versa. Moreover, the optimal action-value functions are related by:
One might ask why several intuitive alternatives to the shaping term do not work. We go through them one by one:
- Imagine we define , where is how close to the goal a state is. Then, every time you enter a desirable state you get a reward, which may seem reasonable at first glance. However, the agent will then prefer taking detours to the goal before it collects more shaping reward!
- Suppose we instead define . Now the agent only gets rewarded for making progress. However recall that the MDP discounts future rewards (). Then, the discounted shaping term becomes - note that the shaping term now depends on the trajectory that the agent takes since the terms do not cancel out.
- With the intermediate terms cancel and the reward becomes a function of the starting state. The total trajectory reward becomes , where as as the episode goes on the total reward becomes purely a function of the starting state.
More broadly, reward shaping is a guess of the value function. A bad cannot corrupt the optimum but can badly slow learning. The quality of the potential is judged by , as in to say that it also must be of a similar scale.
As an example, for a robot that must reach a goal, we can define a potential function of Euclidean distance to the goal. However, in the presence of obstacles, the potential shaping term will fight the optimizer to find the optimal policy around the obstacle.
We can see this in the gridworld displayed below. The agent starts at and must reach , which pays and ends the episode, with . A wall separates the two with a gap at the bottom, so the optimal path first has to move away from the goal. The potential is times the straight-line closeness to - exactly the Euclidean potential from the example above - and zero at itself since it is terminal. Arrows show the greedy policy from exact value iteration, and the line is the rollout from .
Things to try:
- Select show to see the potential. Note how it pulls the agent towards the wall rather than towards the gap.
- Select and drag up from zero. Already at the agent stops next to and never enters - parking beside the goal and collecting every step beats the one-time .
- Select . It holds out longer, but from about the agent again refuses to enter the goal, since stepping into the terminal state forfeits the potential it has built up.
- Select and sweep across the whole range. The policy never differs from the unshaped optimum in a single cell - this is the theorem above at work. Switch between show and show : the values change, but their ordering over actions does not.
The Informativeness Axis
The Cost of Sparsity
Let’s consider the canonical hard case - a deterministic MDP of horizon with actions per state, where reward 1 is delivered only for one specific action sequence and 0 everywhere else. A uniform random policy reaches the reward with probability . Until then, the gradient is zero and there is hence nothing to learn from. Sparse-reward problems are hence exploration problems and are not an amenable setup to the optimizer for large horizons and action spaces.
This is why the true task metric is often unusable as a training signal, while it may be the most faithful specification possible. The difficulty of credit assignment to states scales with the effective horizon - the number of steps over which the reward must be propagated backwards. A potential pre-propagates that information and reduces the effective horizon towards 1. Even a capturing the coarse structure of shortens the horizon over which the optimizer must discover the rest.
Interaction of Dense Rewards with Terminal Rewards
A dense per-step term of magnitude contributes to the return. This is seen by expanding the rewards over time - the dense reward term contributes the same amount at each step: . When we factor out , we find . The term inside the parentheses is an infinite geometric series:
Hence, the overall dense term amounts to . When we provide a terminal event reward of magnitude , we must compare its magnitude to the magnitude of the accumulated dense per-step reward to provide a meaningful gradient towards the desired terminal state.
Misspecification and Reward Hacking
Hackable Rewards
Let be the true objective and the proxy we optimize. Informally, reward hacking occurs when increasing decreases . An objective pair is deemed unhackable if no policy improvement under the proxy ever corresponds to a regression under the true reward. Formally, an objective pair is unhackable iff:
- The ordering of rewards over all stochastic policies between any two states is equivalent by both objectives
- The ordering of rewards over all stochastic policies between any two states is trivial by at least one of the two objectives - i.e. the objective assigns the same return to all policies between the same two states
Only in these two cases will an improvement in the proxy never regress the true reward.
The second point is worth explaining more closely. One example of a trivial return is an objective that provides a reward of 1 at each step over episodes of timesteps. Then, if we know any two policies started at state and reached state both in timesteps, then their returns were equal no matter the intermediate trajectories. Another example is potential-based terms whose return over a trajectory between two states is simply - once again, the intermediate trajectory does not matter for the cumulative return.
Hence, when shaping the reward we strive to construct a proxy for the true objective that does not admit a different ordering of policies in their return between the same start and goal states. Any proxy that induces a genuinely different ordering over policies - not merely a different numerical reward - admits some policy pair on which optimizing the proxy makes the true objective worse.
Multiplicative Rewards
Another useful idea is as follows - the default reward is a weighted sum . Its defining property is substitutability whereby the agent may abandon one term entirely if the resulting gain elsewhere exceeds the loss. When terms are conjunctive requirements - do the task and stay smooth and respect the limit - substitutability is wrong. The alternative is multiplicative composition:
With each . The cost for this is a bootstrapping problem - early in training, when some factor is near zero, the whole product is near zero everywhere and the gradient signal for the other factors is crushed. Multiplicative rewards therefore pair well with curricula or with hybrids in which the task terms multiply and the regularization terms are added.
Summary
During training, a couple points are useful in practice:
- Maintain a true metric you don’t optimize over and compare it to the proxy reward. Divergence between the two is indicative of hacking.
- Example: consider a warehouse robot with a proxy reward of +1 per box moved while the real objective is to move unique boxes to the truck. The agent might discover that if it picks up one box, puts it down, and picks it up again then the proxy reward skyrockets. Meanwhile, the intended objective isn’t being completed and the true objective stays at 0.
- Reward misspecification is more likely to surface later in training as optimizer capability improves. Rewards should be audited anytime capability is changed - e.g. more training time, larger NN, etc.
- Example: consider a robot tasked with cleaning a room where its reward is volume of dirt collected into the dust pan. Early on in training the robot performs poorly and cleans only 10% of the room. It soon improves and cleans 90% of the room. Optimization pressure increases and the robot then finds that it can exploit rewards by dumping the dustpan onto the floor and then collecting the dirt back into it. The reward bug was always present but was not found by the weaker, earlier-stage policy.
- The point here is that a more capable optimizer, either by means of having converged to a good policy or being run with greater capacity than previous runs, will be more likely to find loopholes in the reward. This does not mean that the loopholes were never there. When we increase capability, we must audit for reward hacking.
- Example: consider a robot tasked with cleaning a room where its reward is volume of dirt collected into the dust pan. Early on in training the robot performs poorly and cleans only 10% of the room. It soon improves and cleans 90% of the room. Optimization pressure increases and the robot then finds that it can exploit rewards by dumping the dustpan onto the floor and then collecting the dirt back into it. The reward bug was always present but was not found by the weaker, earlier-stage policy.
- When balancing multiple desired aspects of the behavior (e.g. maintaining altitude, minimizing control effort, maintaining airspeed) - bias towards including all of them in a reward rather than a subset that still appears to produce reasonable behavior. Sometimes we may see nominal behavior while only representing a subset of the desired behaviors in the reward, but the optimizer will optimize non-included variables to its desire to maximize rewards for the included ones. Hence, even an arbitrarily-weighted composition of all desired terms is safer than a tuned subset that appears to function well.
Review: Taxonomy of Rewards
| Component | Role | Design Note |
|---|---|---|
| Terminal | Encodes the goal | |
| Shaping | Makes the task trainable | Should approximate |
| Regularization | Control effort, smoothness | Scale in return units |
| Constraint penalty | Safety and limits | Prefer a constrained MDP for soft limits, encode hard limits in the env |
| Alive bonus | Episode-length preference | Not neutral |
Constrained MDPs
When a requirement is a constraint - such as staying above an airspeed, keeping roll bounded - encoding it as a penalty term forces the optimizer to discover by trial and error the weight at which the constraint happens to bind. That weight depends on the reward scale, the discount, and the current policy, so it does not transfer between tasks. The principal alternative is a constrained MDP.
Typically, we solve , where - the expected discounted return of the reward. Now suppose we’re flying a drone and one of the constraints is to keep current below 70A. We can define another signal, , denoted cost. We can then define the discounted accumulated cost as . We also specify the maximum cost we’re willing to tolerate .
A constrained MDP can be formulated as:
and can be solved via the Lagrangian:
alternating a policy update on the effective reward with a multiplier update:
Since the primal problem is a maximization, the dual function is minimized. The multiplier is the penalty weight we would’ve otherwise tuned by hand but the update formulation tunes it automatically, increasing it while the constraint is violated and relaxing it once satisfied. We simply specify which is interpretable and task-meaningful.
Below is a toy version of the drone example. The policy is a single parameter - think of it as how aggressively we fly - with reward and current draw A. Each iteration takes one gradient step on for the effective reward , followed by the multiplier update above. The dashed lines mark the limit and the optimal multiplier .
Things to try:
- With the defaults, watch the current overshoot the 70 A limit early on, and rise until it pulls the current back to exactly . settles at without us ever choosing a penalty weight.
- Drag all the way left. The multiplier now reacts slowly, and the current peaks near 87 A before the constraint is enforced. Drag it right and tracks the violation almost immediately.
- Lower to 50 A. The constraint is now more expensive and more than doubles - a hand-tuned weight would have had to change with it.
- Raise above 100 A, the most current this policy can ever draw. The constraint never binds and stays at zero.