Search

Search papers and lessons

Target Network

Level 3 · 12 min read

A target network is a copy of a learner's value network that is held still for a while, so the regression target does not move with every update and training stays stable.

On this page

  • Colour = topic mix
  • Probability
  • Reinforcement learning
  • Discrete math
  • Calculus
  • Neural network basics
  • Linear algebra
  • ReadReady to readNot yet

TL;DR

A target network is a second copy of a value network whose weights are kept fixed for a stretch of training. The learning step aims at targets computed by this copy, so the target stays put while the main network moves. This removes a feedback loop that can otherwise make deep value learning diverge.

Builds on: Deep Q-Network · Leads to: Double DQN, Overestimation Bias

Where it's used

Atari from pixels. The 2015 Nature paper on deep Q-networks trained one network to play 49 Atari 2600 games from raw screen frames, with the same settings across games. The target network, refreshed every 10,000 parameter updates, was one of the changes that made this stable enough to work.

Continuous-control robots and simulated limbs. The DDPG algorithm (Lillicrap and colleagues, 2015) keeps target copies of both its actor and critic, and updates them a little at every step rather than in jumps. Simulated robot arms and legs learned to reach and walk with this setup.

Any value-based agent that bootstraps. Many later agents that learn from their own estimates of future value keep a slowly changing copy of the network to compute those estimates. The copy is a standard part of the recipe, not a special trick for one game.

How it works

Take one transition: in state ss the agent takes action aa, gets reward r=1r = 1, and lands in a state s′s' that is not the end of the episode. Use discount γ=0.9\gamma = 0.9. The online network Q(s,a;θ)Q(s, a; \theta), a value estimate for the pair, currently predicts 2.52.5 for the pair (s,a)(s, a). The target network, with frozen weights θ−\theta^-, predicts 2.02.0 for the best action in s′s'. Everything below is about what happens next.

  1. Build the target. The reward plus the discounted best next value gives 1+0.9×2.0=2.81 + 0.9 \times 2.0 = 2.8. The target uses the frozen copy, so 2.02.0 is the value the copy holds, not the online network's latest guess. Check: 0.9×2=1.80.9 \times 2 = 1.8, and 1.8+1=2.81.8 + 1 = 2.8.

  2. Measure the error. The prediction is 2.52.5, so the gap is 2.8−2.5=0.32.8 - 2.5 = 0.3. The squared error for this transition is 0.32=0.090.3^2 = 0.09. Check: a gap of 0.30.3 squared is 0.090.09, and the sign of the gap does not matter after squaring.

  3. Take a gradient step on the online weights. The step nudges Q(s,a;θ)Q(s, a; \theta) up toward 2.82.8. Because the online network shares weights across all states, the same step also changes its guesses for s′s'. Suppose the prediction for (s,a)(s, a) rises to 2.62.6, and the best value at s′s' rises to 2.42.4. Check: the prediction moved closer to 2.82.8 (from a gap of 0.30.3 to 0.20.2).

  4. Look at the target again, this time from the frozen copy. It still says 2.02.0 at s′s', so the target is still 2.82.8 and the gap is 0.20.2. Had the target been computed from the online network instead, it would now be 1+0.9×2.4=3.161 + 0.9 \times 2.4 = 3.16, and the gap would have grown from 0.30.3 to 0.560.56. Check: 0.9×2.4=2.160.9 \times 2.4 = 2.16 and 2.16+1=3.162.16 + 1 = 3.16.

  5. Refresh the copy after CC updates. Every CC updates, copy the online weights into the target network: θ−←θ\theta^- \leftarrow \theta. From then on the target moves, but only in jumps, and between jumps the online network is simply fitting a fixed set of targets, as in ordinary supervised regression. Check: the target changes at most once every CC steps, so it is constant for CC consecutive updates.

flowchart LR
  A["Online network: prediction Q(s,a)"] --> B["Gap to target"]
  C["Target network with frozen weights"] --> D["Target r + gamma max Q(s',a')"]
  D --> B
  B --> E["Gradient step on online weights"]
  E --> A
  E -->|"every C updates"| F["Copy weights into target network"]
  F --> C

The math

Definition: Target network

A target network is a second parameter vector θ−\theta^- that has the same shape as the online weights θ\theta. It is used only to compute the regression target, never to compute the prediction that gets the gradient. Its weights are updated by copying from θ\theta on a schedule.

Step 1 in symbols: the frozen target

For a transition (s,a,r,s′,done)(s, a, r, s', \text{done}), where done\text{done} is 1 if the episode ended on this step and 0 otherwise, the target is

y=r+γ (1−done)max⁡a′Q(s′,a′;θ−).y = r + \gamma \, (1 - \text{done}) \max_{a'} Q(s', a'; \theta^-).

The (1−done)(1 - \text{done}) factor removes the future when the episode has ended. The max⁡\max runs over the actions available in s′s', and every value inside it comes from θ−\theta^-. The whole expression is a constant with respect to θ\theta: no gradient flows through it.

Step 2 in symbols: the loss

The prediction is Q(s,a;θ)Q(s, a; \theta), and the loss over a minibatch BB of transitions is the mean squared error between prediction and target, as in Mean Squared Error:

L(θ)=1∣B∣∑(s,a,r,s′,done)∈B(y−Q(s,a;θ))2.L(\theta) = \frac{1}{|B|} \sum_{(s,a,r,s',\text{done}) \in B} \big( y - Q(s, a; \theta) \big)^2.

Read the sum as "for each transition, square the gap, then average". The gradient with respect to θ\theta is taken with yy held fixed, so only the prediction term changes as the weights move.

Step 3 in symbols: why the frozen copy helps

Consider the one-step update rule in temporal-difference learning in its plain form, with the target written in terms of the same weights:

y(θ)=r+γmax⁡a′Q(s′,a′;θ).y(\theta) = r + \gamma \max_{a'} Q(s', a'; \theta).

Every gradient step changes yy as well as Q(s,a;θ)Q(s, a; \theta), so the target follows the estimate it is supposed to be measuring. With the copy, ∂y/∂θ=0\partial y / \partial \theta = 0, and the step is an ordinary regression step toward a fixed number. That is the whole point of the construction.

Step 4 in symbols: the gradient step

The update to the online weights is ordinary gradient descent on LL, with yy treated as a number:

θ←θ−α ∇θL(θ),\theta \leftarrow \theta - \alpha \, \nabla_\theta L(\theta),

where α\alpha is the step size. Because yy does not depend on θ\theta, the gradient reaches only the prediction term Q(s,a;θ)Q(s, a; \theta), which is what makes the update look exactly like supervised regression.

Step 5 in symbols: two ways to refresh the copy

Hard update, used in the 2015 DQN: every CC updates, set θ−←θ\theta^- \leftarrow \theta.

Soft update, used in DDPG and many later agents: after every update, move the copy a small fraction of the way toward the online weights,

θ−←τ θ+(1−τ) θ−,0<τ≪1.\theta^- \leftarrow \tau \, \theta + (1 - \tau) \, \theta^-, \qquad 0 < \tau \ll 1.

This is an Exponential Moving Average of the online weights with smoothing factor τ\tau. A typical value in DDPG was τ=0.001\tau = 0.001. A hard update is the limit where the copy takes all of θ\theta at once, every CC steps.

Worked examples

Example
1. A finished episode

A transition ends an episode, with reward r=0r = 0 and done=1\text{done} = 1. The target network gives Q(s′,left;θ−)=5Q(s', \text{left}; \theta^-) = 5 and Q(s′,right;θ−)=7Q(s', \text{right}; \theta^-) = 7.

  1. Take the maximum over actions in s′s': max⁡(5,7)=7\max(5, 7) = 7.
  2. Multiply by the done mask: (1−done)=1−1=0(1 - \text{done}) = 1 - 1 = 0, so the future term is 0×7=00 \times 7 = 0.
  3. Add the reward: y=0+0=0y = 0 + 0 = 0.
  4. Interpretation: after the episode ends there is no future, so the target ignores the target network's values entirely. If the same transition had done=0\text{done} = 0, the target would be 0.9×7=6.30.9 \times 7 = 6.3.
Example
2. A minibatch of two transitions

Two stored transitions, both with γ=0.9\gamma = 0.9 and the target network's best next value as given.

  1. Transition A: r=1r = 1, not done, best next target value 2.02.0. Target yA=1+0.9×2.0=2.8y_A = 1 + 0.9 \times 2.0 = 2.8.
  2. Transition A prediction from the online network: 2.52.5. Gap 2.8−2.5=0.32.8 - 2.5 = 0.3, squared error 0.090.09.
  3. Transition B: r=0r = 0, not done, best next target value 4.04.0. Target yB=0+0.9×4.0=3.6y_B = 0 + 0.9 \times 4.0 = 3.6.
  4. Transition B prediction: 3.03.0. Gap 3.6−3.0=0.63.6 - 3.0 = 0.6, squared error 0.360.36.
  5. Minibatch loss: (0.09+0.36)/2=0.225(0.09 + 0.36) / 2 = 0.225. Check: 0.09+0.36=0.450.09 + 0.36 = 0.45, and half of 0.450.45 is 0.2250.225.
  6. Interpretation: the gradient step will raise both predictions, and it will raise B's prediction more, since B has the larger gap. Both targets stay fixed during the step, so the step is a clean regression.
Example
3. A realistic refresh schedule, with a try-it

Suppose the online network is trained once every 4 agent steps, and the target network is refreshed every 10,000 parameter updates, roughly the setup of the 2015 Atari work.

  1. Agent steps between two refreshes: 10,00010{,}000 updates ×\times 4 steps per update =40,000= 40{,}000 agent steps.
  2. Refreshes during a run of 10 million agent steps: 10,000,000/40,000=25010{,}000{,}000 / 40{,}000 = 250.
  3. Check: 40,000×250=10,000,00040{,}000 \times 250 = 10{,}000{,}000, which matches the run length.
  4. Interpretation: the target is fixed for 40,000 agent steps at a time, and the agent gets 250 distinct targets across the run. If the refresh interval were much shorter, the target would track the online network closely and the instability would return. If it were much longer, the reward signal would take a long time to reach the earlier states.
  5. Try it: a run has 2,000,000 gradient updates in total, and the target is refreshed every 10,000 updates. How many refreshes happen, and how many updates does the target stay fixed for at a time?

Answer: 2,000,000/10,000=2002{,}000{,}000 / 10{,}000 = 200 refreshes, and the target stays fixed for 10,000 updates each time.

Common mistakes and tradeoffs

It is tempting to think the target network is a second learner that is trained separately. In fact it never receives gradients. It is a delayed copy of the online network, and it exists only to make the regression target stable, because the gradient step is computed as if the target were a constant.

It is tempting to think the copy can be dropped as long as there is an experience replay buffer. In fact the replay buffer breaks up correlated samples, while the target network fixes the moving-target problem. They solve different problems, and DQN needed both. See Experience Replay.

It is tempting to think a shorter refresh interval is always safer. In fact a very short interval (or a soft update with a large τ\tau) brings back the feedback loop, and a very long one slows down how fast reward information spreads backwards through the states. The interval is a tradeoff between stability and speed.

It is tempting to think the target network changes nothing about the estimate's bias. In fact it does not remove Overestimation Bias: the max over a noisy copy is still biased upward. Double DQN addresses that separately by using the online network to choose the action and the target network to score it.

When to use it: whenever a value network is trained toward targets that come from its own predictions, which is the case for almost all deep value-based reinforcement learning. When not to: when the targets come from fixed labels, as in ordinary supervised learning, there is nothing to freeze. Alternatives include the soft update described above, and methods that avoid bootstrapping altogether, such as Monte Carlo returns, at the cost of waiting for full episodes.

History

Value-based reinforcement learning with tables had sound convergence results (Q-learning among them). The problem was that neural function approximators trained on their own bootstrapped estimates often diverged, and there was no standard fix.

In 2013, Mnih and colleagues showed that a convolutional network with a replay buffer could learn several Atari games from pixels. That version, built on a convolutional network, still trained directly toward its own latest estimates.

In 2015, the Nature paper by Mnih and colleagues introduced the target network in its hard-update form, refreshed on a fixed schedule, and reported that it was key to stable training across 49 games. The same year, Lillicrap and colleagues adapted the idea to continuous-control actor-critic methods with the soft update, which made the copy move smoothly rather than in jumps.

Recap

  • A value network trained toward its own estimates chases a target that moves with every step, and this can diverge.
  • A target network is a delayed copy of the online weights. It computes the regression target and receives no gradient.
  • The target is r+γ(1−done)max⁡a′Q(s′,a′;θ−)r + \gamma (1-\text{done}) \max_{a'} Q(s', a'; \theta^-), and the loss is the mean squared gap to it.
  • The copy is refreshed either in jumps every CC updates (hard update) or a little at each step (soft update).
  • The copy stabilises training but does not remove overestimation. Read next: Overestimation Bias, then Double DQN.

Check yourself

QuestionWhy is the target computed with the frozen weights theta-minus rather than the online weights theta?Show answer

Because the target would otherwise move every time the online weights move. Each gradient step would change both the prediction and the thing it is aimed at, so the regression chases a moving goal and can diverge. With frozen weights the target is a constant during the step.

QuestionA transition has reward 2, discount 0.9, done equal to 0, and the target network gives a best next value of 3. What is the target?Show answer

Compute 2+0.9×3=2+2.7=4.72 + 0.9 \times 3 = 2 + 2.7 = 4.7. The target is 4.74.7. The done flag is 0, so the future term is kept.

QuestionA network refreshes its target every 5,000 updates and makes 100,000 updates in total. How many distinct refreshes occur?Show answer

Compute 100,000/5,000=20100{,}000 / 5{,}000 = 20. There are 20 refreshes, so the target takes 20 distinct values over the run, plus the initial copy.

QuestionA researcher sets the soft update factor tau to 0.5 and reasons that the target is then nearly the same as the online network after every step. Is this a good setting?Show answer

No. With τ=0.5\tau = 0.5 the copy takes half of the online weights each step, so the target follows the online network quickly and the feedback loop the copy was meant to break comes back. A small τ\tau, such as 0.001, keeps the target slow-moving.

QuestionAn agent with a replay buffer still diverges when the target is the online network. Which part of the fix is missing, and why does the buffer not replace it?Show answer

The target network is missing. The replay buffer breaks the correlation between consecutive samples, but it does not stop the target from moving with the online weights. Only a frozen or slowly moving copy holds the regression target still.

Sources