Search

Search papers and lessons

Maximum Entropy Reinforcement Learning

Level 4 · 11 min read

Maximum entropy RL rewards the agent for keeping its policy random as well as for earning reward, which yields softer Bellman equations and policies that explore more.

On this page

  • Colour = topic mix
  • Discrete math
  • Probability
  • Reinforcement learning
  • Calculus
  • Linear algebra
  • Stochastic processes
  • Sign in to see your progress

TL;DR

Standard reinforcement learning asks an agent to maximise its total reward, the sum of the rewards it collects over time. Maximum entropy RL asks for a little more: maximise the reward and also keep the agent's policy as random as it can be while still doing well. A bonus proportional to the Entropy of the policy (a measure of how spread out its action probabilities are) is added to every step's reward. The result is a set of Bellman equations with a soft maximum in place of the hard one, and policies that keep trying several good options instead of collapsing onto one.

Where you'll see it: in the Soft Actor-Critic family of robot-control algorithms, where the policy must stay broad enough to recover from surprises, and in the derivation of soft Q-learning.

Intuition

Start from a choice you already make

You are choosing a lunch spot. Your favourite place is good, but the second-best place is nearly as good and you have not tried it lately. A strictly greedy chooser always goes to the favourite. A chooser who values variety picks the favourite most of the time and sometimes the other place. The second chooser is not being irrational. They have found more information and a backup plan, and they will still eat well on average.

Maximum entropy RL builds this preference into the objective. The agent is paid for reward and paid a little for not being sure what it will do next.

Why the idea exists

A greedy policy has two problems. First, it explores too little. If the agent always takes the action its current estimates favour, it never learns that an unvisited action might be better. Second, it is brittle. A policy that has collapsed onto one action has no alternative when the world changes slightly, such as a robot finding its path blocked.

Adding an entropy bonus addresses both at once. The policy stays spread over the good actions, so it keeps sampling them, and the learned behaviour degrades gracefully when conditions shift. A coefficient α\alpha sets the price of randomness: a large α\alpha favours variety, a small α\alpha recovers ordinary greedy reward maximisation.

0.20.40.60.800.51probability p of action 1objective value
  • reward + entropy bonus
Two actions, with action 1 worth reward 1 and action 2 worth 0, and alpha = 1. The objective peaks at p = e/(1+e), about 0.731, not at p = 1: the entropy term keeps some probability on the worse action.

The math

The objective

Let r(st,at)r(s_t, a_t) be the reward at step tt and let π(⋅∣s)\pi(\cdot \mid s) be the policy, the distribution over actions in state ss. The maximum entropy objective is

J(π)=Eπ[∑t(r(st,at)+α H(π(⋅∣st)))],J(\pi) = \mathbb{E}_\pi \left[ \sum_{t} \Big( r(s_t, a_t) + \alpha \, \mathcal{H}\big(\pi(\cdot \mid s_t)\big) \Big) \right],

where H(π(⋅∣s))=−∑aπ(a∣s)log⁡π(a∣s)\mathcal{H}(\pi(\cdot \mid s)) = -\sum_a \pi(a \mid s) \log \pi(a \mid s) is the Entropy of the action distribution in state ss, and α>0\alpha \gt 0 is the temperature that sets how much the bonus counts. Here log⁡\log is the natural logarithm (see Logarithm), so entropy is measured in nats. The expectation is over trajectories generated by π\pi in the environment, which is a Markov decision process. Setting α=0\alpha = 0 gives back the ordinary objective.

The soft value function

In ordinary RL the value of a state is the maximum over actions of the action value, V(s)=max⁡aQ(s,a)V(s) = \max_a Q(s, a). The soft version replaces the maximum with a smooth, log-sum-exp form:

V(s)=αlog⁡∑aexp⁡ ⁣(Q(s,a)α).V(s) = \alpha \log \sum_a \exp\!\left( \frac{Q(s, a)}{\alpha} \right).

Here Q(s,a)Q(s, a) is the action value, the expected return from taking aa in ss. The exponential means high-value actions dominate the sum, but every action contributes something. As α\alpha shrinks towards 0, the expression approaches the hard maximum.

The optimal policy is a softmax

The best policy under the objective picks each action with probability proportional to the exponential of its value:

π∗(a∣s)=exp⁡ ⁣(Q(s,a)−V(s)α).\pi^*(a \mid s) = \exp\!\left( \frac{Q(s, a) - V(s)}{\alpha} \right).

This is the Softmax of the action values with temperature α\alpha. Every action has positive probability, and the probabilities sum to 1 because V(s)V(s) is exactly the normaliser. Large α\alpha flattens the distribution; small α\alpha sharpens it onto the best action.

The soft Bellman equation

Taking one step and then following the soft value gives the soft Bellman equation, with γ\gamma the discount factor from the Bellman equation:

Q(s,a)=r(s,a)+γ Es′[V(s′)].Q(s, a) = r(s, a) + \gamma \, \mathbb{E}_{s'}\big[ V(s') \big].

The only change from the ordinary Bellman equation is that max⁡a\max_a has become αlog⁡∑aexp⁡(⋅/α)\alpha \log \sum_a \exp(\cdot / \alpha). Iterating this update is soft value iteration, and it converges to QQ and VV that satisfy the two equations above.

Worked examples

Example
1. One state, two actions (tiny)

A single state with two actions. Action 1 gives reward 1, action 2 gives reward 0, and the temperature is α=1\alpha = 1. Let pp be the probability of action 1.

  1. Objective for one step: p⋅1+H=p−plog⁡p−(1−p)log⁡(1−p)p \cdot 1 + \mathcal{H} = p - p \log p - (1-p) \log(1-p).
  2. Soft policy from the formula: π(1)=e1/(e1+e0)=e/(e+1)≈0.7311\pi(1) = e^{1} / (e^{1} + e^{0}) = e / (e + 1) \approx 0.7311.
  3. Check by setting the derivative to zero: 1−log⁡p+log⁡(1−p)=01 - \log p + \log(1 - p) = 0, so p/(1−p)=ep / (1 - p) = e, which gives p≈0.7311p \approx 0.7311. The same answer from two routes.
  4. Value of the objective at this pp: 0.7311−0.7311×log⁡(0.7311)−0.2689×log⁡(0.2689)≈0.7311+0.2290+0.3532=1.31330.7311 - 0.7311 \times \log(0.7311) - 0.2689 \times \log(0.2689) \approx 0.7311 + 0.2290 + 0.3532 = 1.3133.
  5. Soft value: V=log⁡(e1+e0)=log⁡(3.7183)≈1.3133V = \log(e^{1} + e^{0}) = \log(3.7183) \approx 1.3133.

Interpretation: the best policy plays action 1 about 73 percent of the time, not 100 percent. The agent gives up a little reward to keep action 2 alive, and the objective value equals the soft value of the state.

Example
2. A state with three action values (standard case)

One state with Q(s,a1)=2Q(s, a_1) = 2, Q(s,a2)=1Q(s, a_2) = 1, Q(s,a3)=0Q(s, a_3) = 0, and α=1\alpha = 1.

  1. Exponentials: e2≈7.3891e^{2} \approx 7.3891, e1≈2.7183e^{1} \approx 2.7183, e0=1e^{0} = 1. Their sum is 11.107311.1073.
  2. Soft value: V(s)=log⁡(11.1073)≈2.4076V(s) = \log(11.1073) \approx 2.4076.
  3. Policy: π(a1)=7.3891/11.1073≈0.6652\pi(a_1) = 7.3891 / 11.1073 \approx 0.6652, π(a2)≈2.7183/11.1073≈0.2447\pi(a_2) \approx 2.7183 / 11.1073 \approx 0.2447, π(a3)≈1/11.1073≈0.0900\pi(a_3) \approx 1 / 11.1073 \approx 0.0900.
  4. Check: 0.6652+0.2447+0.0900≈1.00000.6652 + 0.2447 + 0.0900 \approx 1.0000.
  5. Compare with the hard maximum: max⁡aQ=2\max_a Q = 2, so the soft value is 0.4076 higher than the best single action value.

Interpretation: the soft value is above the best action value because it counts the entropy of mixing the choices. The policy puts most weight on action 1, but action 2 gets 24 percent and action 3 gets 9 percent. Setting α\alpha to 0.1 would shrink the weights on the worse actions to about 4.5×10−54.5 \times 10^{-5} for action 2 and 2×10−92 \times 10^{-9} for action 3, so the hard maximum is recovered in the limit.

Example
3. A continuous robot action (realistic in structure)

A robot joint is controlled by a torque aa, and the policy outputs a Gaussian N(μ,σ2)\mathcal{N}(\mu, \sigma^2) over torques. For a continuous action the sum in the entropy becomes an integral, called differential entropy, and for a Gaussian it equals

H=12log⁡ ⁣(2πe σ2).\mathcal{H} = \tfrac{1}{2} \log\!\big( 2\pi e \, \sigma^2 \big).

Take α=0.2\alpha = 0.2 and compare two policies with the same mean torque.

  1. Broad policy, σ=0.5\sigma = 0.5: H=12log⁡(2πe×0.25)=12log⁡(4.2699)≈0.7258\mathcal{H} = \tfrac{1}{2} \log(2\pi e \times 0.25) = \tfrac{1}{2} \log(4.2699) \approx 0.7258 nats. Bonus: 0.2×0.7258≈0.14520.2 \times 0.7258 \approx 0.1452.
  2. Narrow policy, σ=0.05\sigma = 0.05: H=12log⁡(2πe×0.0025)=12log⁡(0.042699)≈−1.5768\mathcal{H} = \tfrac{1}{2} \log(2\pi e \times 0.0025) = \tfrac{1}{2} \log(0.042699) \approx -1.5768 nats. Bonus: 0.2×(−1.5768)≈−0.31540.2 \times (-1.5768) \approx -0.3154.
  3. Difference in bonus: 0.1452−(−0.3154)≈0.460.1452 - (-0.3154) \approx 0.46 in favour of the broad policy.

Interpretation: differential entropy can be negative, and that is fine, because only differences matter for the objective. The narrow policy is penalised heavily. Unless the narrow policy earns at least 0.46 more reward per step, the entropy term keeps the controller broad. This is the mechanism behind Soft Actor-Critic (an off-policy method, meaning it can learn from older experience stored in a replay buffer), which also tunes α\alpha during training so that the entropy stays near a chosen target.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
α\alpha too largeThe policy is nearly uniform and learns slowly, because it rarely exploits what it has learnedDecrease α\alpha, or tune it toward a target entropy
α\alpha too smallThe policy collapses early and stops exploring, as in ordinary greedy RLIncrease α\alpha, or use an entropy schedule
The task needs one fixed best answer, with no randomness at test timeA stochastic policy adds noise to the final behaviourAct with the mode of the policy at evaluation time
Rewards are sparse and deterministicThe entropy bonus can dominate the rare reward signalScale the reward, or combine with a [[sparse-reward-problem
Simple discrete problems with tabular valuesThe soft machinery adds complexity for little gainPlain Q-learning, or ϵ\epsilon-greedy exploration

Choosing α\alpha is the central practical question. Too much entropy wastes reward, and too little brings back the brittleness the method was meant to fix.

History

The idea of picking the most random distribution consistent with constraints comes from statistical physics and from Edwin Jaynes's principle of maximum entropy. In reinforcement learning, Brian Ziebart and colleagues used the same principle in 2008 for inverse reinforcement learning, to recover a reward function from demonstrations. Emanuel Todorov and Hilbert Kappen had earlier shown that some control problems become linear when the cost of deviating from a default policy is measured with a relative entropy. Haarnoja and colleagues brought the soft Bellman equation to deep RL in 2017 as soft Q-learning, and their 2018 Soft Actor-Critic paper made it a practical, off-policy method for continuous control. It caught on because it was stable across tasks and needed less tuning than many alternatives.

Recap

Maximum entropy RL adds α\alpha times the policy entropy to each step's reward. The value function becomes a log-sum-exp instead of a maximum, and the optimal policy becomes a Softmax of the action values with temperature α\alpha. That policy keeps every good action in play, which helps exploration and robustness. The Bellman equation keeps its one-step form, so the usual algorithms carry over. Practical systems such as Soft Actor-Critic build directly on this, and the policy-gradient view of the same objective appears in Policy Gradient.

Check yourself

QuestionWhat does the temperature alpha control in the maximum entropy objective, and what happens as alpha goes to 0?Show answer

Alpha sets the weight of the entropy bonus relative to reward. A large alpha favours randomness and a small alpha favours reward. As alpha goes to 0 the bonus vanishes, the soft value αlog⁡∑aexp⁡(Q/α)\alpha \log \sum_a \exp(Q/\alpha) approaches max⁡aQ\max_a Q, and the policy approaches the greedy policy of ordinary RL.

QuestionA state has Q values 3, 1 and 1 with alpha = 1. Compute the soft value V(s) to three decimals.Show answer

V=log⁡(e3+e1+e1)=log⁡(20.0855+2.7183+2.7183)=log⁡(25.5221)≈3.240V = \log(e^{3} + e^{1} + e^{1}) = \log(20.0855 + 2.7183 + 2.7183) = \log(25.5221) \approx 3.240. The hard maximum is 3, and the soft value is slightly higher because the two lower actions add to the sum.

QuestionIn Example 1, why is the best policy not to play action 1 with probability 1, even though it gives reward 1?Show answer

The objective adds the entropy of the policy. Putting all probability on action 1 gives entropy 0, so the objective would be 1. Putting probability e/(e+1)≈0.731e/(e+1) \approx 0.731 on action 1 gives reward 0.7310.731 plus entropy 0.5820.582, a total of 1.313, which is higher. Some randomness is worth more than the small reward it costs.

QuestionA designer sees a robot policy's standard deviation shrink from 0.5 to 0.05 during training and worries that the policy has become too precise. Using the entropy formula and alpha = 0.2, explain how the objective changes and what the designer should do.Show answer

Differential entropy falls by 12log⁡(100)≈2.30\tfrac{1}{2}\log(100) \approx 2.30 nats when σ\sigma drops tenfold, so the bonus falls by 0.2×2.30≈0.460.2 \times 2.30 \approx 0.46 per step. The objective now favours the narrow policy less, so it must earn at least 0.46 more reward per step to justify the collapse. The designer should check whether reward improved enough to justify it, and if not, raise alpha or set a target entropy that keeps the spread from collapsing.

Sources