Answers.Body: <blockquote>
  <p>I don't see how the target Q-value gets updated when the current Q-value is changed.</p>
  </blockquote>
  <p>Without a separate target network, this happens because the approximator will <em>generalise</em>, and the generalisation will include the successor/target states. This can be very likely in many environments, since successor states often have many similar features to the previous states (think how similar any video game might look a few video frames on from any position).</p>
  <p>In tabular variants of Q learning this does not happen, a change to any single Q value give state, action is always made to a single estimate that is isolated from all other estimates. Adding approximation changes things, and is not avoidable - in fact it is usually desirable to have strong generalisation in order to obtain good estimates for never-seen-before states. However, the flip side of this strong generalisation is that updating estimates for <span class="math-container">$Q(s,a)$</span> will impact many other <span class="math-container">$Q(s_i,a_j)$</span></p>
  <p>One related thing worth bearing in mind is that the Q-function update in DQN is a <em>semi</em>-gradient update. If you do not use a target network, then technically the full gradient needs to take account of the changes to the TD target when the weights change (because both <span class="math-container">$Q(s,a)$</span> and <span class="math-container">$Q(s',a')$</span> are calculated use the same approximator). So one way to try and solve the same issue is to alter the update to use the full gradient. The maths for this is more complex than normal DQN, but is addressed in <a href="http://incompleteideas.net/book/the-book-2nd.html" rel="noreferrer">the Sutton &amp; Barto book</a> in chapter 11 section 11.7 &quot;Gradient-TD Methods&quot;.</p>
  <p>In practice for DQN, most experiments seem to prefer some variant of target networks. This also <em>effectively</em> changes the learning task to a full gradient one, with different progression of the TD targets. Although it is not as theoretically nice as a full gradient method, it seems to be a pragmatic choice by many researchers and developers.</p>
  <p>The paper <a href="https://arxiv.org/abs/2103.05981#" rel="noreferrer">Full Gradient DQN Reinforcement Learning: A Provably Convergent Scheme</a> attempts to make a fair comparison of DQN with a TD-gradient DQN, and comes to the conclusion that the full gradient approach is better - however, there may be other reasons to prefer the slightly clunkier semi-gradient plus target network approach.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-01T18:12:48.540Z
Answers.LastActivityDate: 2022-07-01T18:37:23.057Z
Answers.LastEditDate: 2022-07-01T18:37:23.057Z
Answers.UserDisplayName: Neil Slater
Body: <p>After reviewing similar <a href="https://ai.stackexchange.com/questions/6982/why-does-dqn-require-two-different-networks">posts</a> on this topic, I understand that a target network is used to prevent &quot;divergence&quot;, but am not sure what it actually means. Q-values are predicted using a function approximator. The weights of the function approximator are then updated using the difference between the Q-value and TD target (<span class="math-container">$r + \gamma\max_aQ(s',a)$</span>). Now assuming, that the estimate of the Q-value was wrong, the weights could easily be updated so that they are correct the next time the state is encountered. My confusion arises when online blogposts say that this change in weights modifies the target Q-value(<span class="math-container">$Q(s', a)$</span>) too. I don't see how the target Q-value gets updated when the current Q-value is changed.</p>
  
Tags: <reinforcement-learning><deep-rl><dqn>
Title: Why do Q-values diverge without a target network?
UserDisplayName: desert_ranger
ThreadText: # Why do Q-values diverge without a target network?
  
  (Asked by: username_0 on 2022-07-01)
  
  After reviewing similar [posts](https://ai.stackexchange.com/questions/6982/why-does-dqn-require-two-different-networks) on this topic, I understand that a target network is used to prevent "divergence", but am not sure what it actually means. Q-values are predicted using a function approximator. The weights of the function approximator are then updated using the difference between the Q-value and TD target ($r + \gamma\max\_aQ(s',a)$). Now assuming, that the estimate of the Q-value was wrong, the weights could easily be updated so that they are correct the next time the state is encountered. My confusion arises when online blogposts say that this change in weights modifies the target Q-value($Q(s', a)$) too. I don't see how the target Q-value gets updated when the current Q-value is changed.
  
  **Tags:** reinforcement-learning, deep-rl, dqn
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-07-01. Score: 5 | Accepted answer
  
  > I don't see how the target Q-value gets updated when the current Q-value is changed.
  
  Without a separate target network, this happens because the approximator will *generalise*, and the generalisation will include the successor/target states. This can be very likely in many environments, since successor states often have many similar features to the previous states (think how similar any video game might look a few video frames on from any position).
  
  In tabular variants of Q learning this does not happen, a change to any single Q value give state, action is always made to a single estimate that is isolated from all other estimates. Adding approximation changes things, and is not avoidable - in fact it is usually desirable to have strong generalisation in order to obtain good estimates for never-seen-before states. However, the flip side of this strong generalisation is that updating estimates for $Q(s,a)$ will impact many other $Q(s\_i,a\_j)$
  
  One related thing worth bearing in mind is that the Q-function update in DQN is a *semi*-gradient update. If you do not use a target network, then technically the full gradient needs to take account of the changes to the TD target when the weights change (because both $Q(s,a)$ and $Q(s',a')$ are calculated use the same approximator). So one way to try and solve the same issue is to alter the update to use the full gradient. The maths for this is more complex than normal DQN, but is addressed in [the Sutton & Barto book](http://incompleteideas.net/book/the-book-2nd.html) in chapter 11 section 11.7 "Gradient-TD Methods".
  
  In practice for DQN, most experiments seem to prefer some variant of target networks. This also *effectively* changes the learning task to a full gradient one, with different progression of the TD targets. Although it is not as theoretically nice as a full gradient method, it seems to be a pragmatic choice by many researchers and developers.
  
  The paper [Full Gradient DQN Reinforcement Learning: A Provably Convergent Scheme](https://arxiv.org/abs/2103.05981#) attempts to make a fair comparison of DQN with a TD-gradient DQN, and comes to the conclusion that the full gradient approach is better - however, there may be other reasons to prefer the slightly clunkier semi-gradient plus target network approach.
  
  
  ---
  
  