Answers.Body: <p>The learning model and target model are only different by N steps (typically a few thousand) out the entire taining process. If the process is near complete, they will also be quite similar.</p>
  <p>The target model is not inherently more stable in terms of producing &quot;correct&quot; or &quot;better&quot; Q values. Instead it is kept static for a period of time in order to stabilise the temporal difference (TD) target <span class="math-container">$R_{t+1} + \gamma \text{max}_{a'} \hat{q}(S_{t+1}, a', \theta^{-})$</span></p>
  <p>Due to the copying stage that you listed:</p>
  <ul>
  <li><p>If you return the target model, this is identical to returning the learning model from N steps beforehand.</p>
  </li>
  <li><p>Which in turn means that there was no point in doing the last N steps of training. You may as well have returned the learning model at the same point as copying.</p>
  </li>
  </ul>
  <p>Or another way of thinking about it: Returning the learning model at the end of the training process is identical to copying it to the target model as normal, then returning the target model immediately.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-01T11:55:19.337Z
Answers.Text: Maybe one practical suggestion would be to see if learning becomes "unstable" (in some sense) in the last $N$ steps. If that's the case, maybe you can just derive the policy from the target network.
Answers.UserDisplayName: nbro
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-01T11:59:26.260Z
Answers.Text: @nbro: I'm not sure how that could be made practical. You can detect that changes are happening, but not why (it might be due to discovery of something real that needs adjusting for, or it might be catastrophic forgetting). The practical approach I take is to separately test candidate networks at snapshot points. I generally ignore the relationship with when the last copying was to the target network. However, due to taking snapshots at e.g. every 10,000 steps, and copying to target every 1,000 steps, often there is no difference between target and learning networks, and it's a non-issue.
Answers.UserDisplayName: Neil Slater
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-01T11:42:36.947Z
Answers.LastActivityDate: 2020-11-01T11:42:36.947Z
Answers.UserDisplayName: Neil Slater
Answers.Body: <p>Target network is not more stable. Both networks are the same in the regard that no one is more stable than the other. The reason for using a target network is that your current network after each step is updated. So, by not using a target network and using just the current network, after each update the rewards for many states will be modified slightly. So, for a particular state, after each update, the reward will be modified which will lead to an unstable reward. This happens because when you update Q value function for state S, you may also slightly change the reward it predicts for state S' in the next step. So, each update to Q value function changes slightly the rewards for many other states, which makes the predicted rewards in all those states to be slightly unstable, as they are changed very often and you dont have a clear reward in those states.</p>
  <p>However, by using a target network, your reward will be more stable(as you dont update your target network at each step, but you only update the current network).</p>
  <p>So, by using a target network, the update is more stable, not the individual networks. And because the target network is an older version of the current network, it is sensible to use the current network as it was trained more than the target network</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-01T11:53:39.620Z
Answers.Text: "_This happens because when you update Q value function for state S, you may also slightly change the reward it predicts for state S'._", I don't see how this is true, if you are just updating Q(s, a). Maybe you mean that, if at the next iteration/time step, you use the just updated Q(s, a) to update Q(s', a), then you continue doing this, then you will somehow be oscillating.
Answers.UserDisplayName: nbro
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-01T11:57:06.757Z
Answers.Text: that is true. By using the current network which was updated for a state to predict the reward for other states, the reward for those states will be different than the reward for those states before updating for a particular state.
Answers.UserDisplayName: Otniel-Bogdan Mercea
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-01T11:58:23.173Z
Answers.Text: Yes, but that's different than what you wrote (in the sense that what you wrote is not fully clear), so I would edit your post to clarify that. I would also edit your post to clarify this sentence "Both networks are the same". Both networks have the same architecture, but they are not the same (i.e. do not have the same parameters' values), apart from when you just copied the main value network to the target network.
Answers.UserDisplayName: nbro
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-01T11:48:23.023Z
Answers.LastActivityDate: 2020-11-01T12:00:03.710Z
Answers.LastEditDate: 2020-11-01T12:00:03.710Z
Answers.UserDisplayName: Otniel-Bogdan Mercea
Body: <p>Target network in DQN is known to make the network more stable, and the loss is like &quot;how good I'm now compared to using the target&quot;. What I don't understand is, if the target network is the stable one, why do we keep using/saving the first model as the predictor instead of the target?</p>
  <p>I see in the code everywhere:</p>
  <ul>
  <li>Model</li>
  <li>Target model</li>
  <li>Train model</li>
  <li>Copy to target</li>
  <li>Get loss between them</li>
  </ul>
  <p>At the end, the model is saved and used for prediction and not the target.</p>
  
Tags: <reinforcement-learning><dqn><deep-rl>
Title: Why not use the target network in DQN as the predictor after training
UserDisplayName: Kimboo Rasta
ThreadText: # Why not use the target network in DQN as the predictor after training
  
  (Asked by: username_0 on 2020-11-01)
  
  Target network in DQN is known to make the network more stable, and the loss is like "how good I'm now compared to using the target". What I don't understand is, if the target network is the stable one, why do we keep using/saving the first model as the predictor instead of the target?
  
  I see in the code everywhere:
  
  * Model
  * Target model
  * Train model
  * Copy to target
  * Get loss between them
  
  At the end, the model is saved and used for prediction and not the target.
  
  **Tags:** reinforcement-learning, dqn, deep-rl
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2020-11-01. Score: 2
  
  The learning model and target model are only different by N steps (typically a few thousand) out the entire taining process. If the process is near complete, they will also be quite similar.
  
  The target model is not inherently more stable in terms of producing "correct" or "better" Q values. Instead it is kept static for a period of time in order to stabilise the temporal difference (TD) target $R\_{t+1} + \gamma \text{max}\_{a'} \hat{q}(S\_{t+1}, a', \theta^{-})$
  
  Due to the copying stage that you listed:
  
  * If you return the target model, this is identical to returning the learning model from N steps beforehand.
  * Which in turn means that there was no point in doing the last N steps of training. You may as well have returned the learning model at the same point as copying.
  
  Or another way of thinking about it: Returning the learning model at the end of the training process is identical to copying it to the target model as normal, then returning the target model immediately.
  
  > **Comments:**
  > **username_2**: Maybe one practical suggestion would be to see if learning becomes "unstable" (in some sense) in the last $N$ steps. If that's the case, maybe you can just derive the policy from the target network.  
  > **username_1**: @username_2: I'm not sure how that could be made practical. You can detect that changes are happening, but not why (it might be due to discovery of something real that needs adjusting for, or it might be catastrophic forgetting). The practical approach I take is to separately test candidate networks at snapshot points. I generally ignore the relationship with when the last copying was to the target network. However, due to taking snapshots at e.g. every 10,000 steps, and copying to target every 1,000 steps, often there is no difference between target and learning networks, and it's a non-issue.  
  
  ---
  
  ### Answer by username_3 on 2020-11-01. Score: 1
  
  Target network is not more stable. Both networks are the same in the regard that no one is more stable than the other. The reason for using a target network is that your current network after each step is updated. So, by not using a target network and using just the current network, after each update the rewards for many states will be modified slightly. So, for a particular state, after each update, the reward will be modified which will lead to an unstable reward. This happens because when you update Q value function for state S, you may also slightly change the reward it predicts for state S' in the next step. So, each update to Q value function changes slightly the rewards for many other states, which makes the predicted rewards in all those states to be slightly unstable, as they are changed very often and you dont have a clear reward in those states.
  
  However, by using a target network, your reward will be more stable(as you dont update your target network at each step, but you only update the current network).
  
  So, by using a target network, the update is more stable, not the individual networks. And because the target network is an older version of the current network, it is sensible to use the current network as it was trained more than the target network
  
  > **Comments:**
  > **username_2**: "_This happens because when you update Q value function for state S, you may also slightly change the reward it predicts for state S'._", I don't see how this is true, if you are just updating Q(s, a). Maybe you mean that, if at the next iteration/time step, you use the just updated Q(s, a) to update Q(s', a), then you continue doing this, then you will somehow be oscillating.  
  > **username_3**: that is true. By using the current network which was updated for a state to predict the reward for other states, the reward for those states will be different than the reward for those states before updating for a particular state.  
  > **username_2**: Yes, but that's different than what you wrote (in the sense that what you wrote is not fully clear), so I would edit your post to clarify that. I would also edit your post to clarify this sentence "Both networks are the same". Both networks have the same architecture, but they are not the same (i.e. do not have the same parameters' values), apart from when you just copied the main value network to the target network.  
  
  ---
  
  