<p>The fact that the model trains on its own predictions is the whole point of Q-learning: it is a concept called bootstrapping, which means reusing your experience. The insight behind this is:</p>

<ul>
<li>The Agent is initialized with some weights</li>
<li>These weights represent the Agent's current representation of the Q-Value function it is trying to approximate</li>
<li>Then it acts on the environment, performing the action it believes to be of highest Q-Value (with some randomness for exploration)</li>
<li>Then it receives some feedback from the environment : a reward, and the new state it is in</li>
<li>By comparing the difference between the Agent's Q-Value approximation for state <code>t</code> (= <code>[s_t_batch, a_batch]</code>) and it's (discounted) approximation for state <code>t+1</code> <strong><em>plus</em></strong> the reward (=<code>y_batch</code>), it is able to measure how wrong it's prediction for <code>Qt</code> is.</li>
<li>From this measure of mistake (called TD-Error) weights are updated in the direction lower MSE, as for any other gradient-based optimization. </li>
<li>(One could wait for more than one step to have more information from the environment to update the weights in an even better direction. One could actually wait for the whole episode to be over and train on that. This continuum between training instantly and waiting for the end is called TD(Lambda), you should look into it)</li>
</ul>

<p>Your loss means exactly this: for one batch, it is the mean-squared error between your model's prediction for time <code>t</code> from its sole Q-Value approximation and its prediction for time <code>t</code> from its Q-Value approximation for the <strong><em>next</em></strong> state and taking into account some "ground truth" from the environment, that is the <em>reward</em> for this timestep.</p>

<p>Your loss does go down it seems to me, it is however very unstable, which is a known issue of vanilla Q-Learning especially vanilla Deep Q-Learning. Look at the overview paper below to have an idea of how more complex algorithms work</p>

<p>I advise you to look into <a href="https://en.wikipedia.org/wiki/Temporal_difference_learning" rel="nofollow noreferrer">Temporal Difference Learning</a>. 
Good ressources also are </p>

<ul>
<li><a href="https://medium.com/emergent-future/simple-reinforcement-learning-with-tensorflow-part-0-q-learning-with-tables-and-neural-networks-d195264329d0" rel="nofollow noreferrer">Simple Reinforcement Learning with Tensorflow</a></li>
<li>The RL Bible: Sutton &amp; Barto, Reinforcement Learning: An Introduction (2015 Edition)</li>
<li>This <a href="http://arxiv.org/abs/1701.07274" rel="nofollow noreferrer">overview paper</a> summarizing insights and implementation of recent algorithms</li>
<li>I wrote my <a href="http://vict0rsch.github.io/thesis/thesisVictorSchmidt.pdf" rel="nofollow noreferrer">Master Thesis</a> on RL, you can checkout the part 2: background theory for more detailed insights</li>
</ul>
How does Deep Q learning work
<p>When I am training my model I have the following segment:</p>

<pre><code>s_t_batch, a_batch, y_batch = train_data(minibatch, model2)
# perform gradient step
loss.append(model.train_on_batch([s_t_batch, a_batch], y_batch))
</code></pre>

<p>where <code>s_t, a_</code> corresponds to current states and actions that were taken in those states respectively. <code>model2</code> is the same as <code>model</code> except that <code>model2</code> has an output of <code>num_actions</code> and <code>model</code> only outputs the value of the action that was taken in that state. </p>

<p>What I find strange (and is really the focus of this question) is in the function <code>train_data</code> I have the line:</p>

<pre><code>y_batch = r_batch + GAMMA * np.max(model.predict(s_t_batch), axis=1)
</code></pre>

<p>The strange part is the fact that I am using the model to generate my <code>y_batch</code> as well as training on them. Doesn't this become some sort of self fulfilling prophecy? If I understand correctly, the model tries to predict the expected maximum reward. Using the <strong>same</strong> model to try and generate <code>y_batch</code> is implying that it is the true model doesn't it?</p>

<p><strong>The question is</strong>, 1. what is the intuition behind using the same model to generate y_batch as it is to train them. 2. (optional) does loss value mean anything. When I plot it, it seems doesn't seem to be converging, however the sum of rewards seem to be increasing (see plots in link below).</p>

<p>The full code can be found <a href="https://github.com/sachinruk/deepschool.io/blob/master/Lesson%2020%20-%20Deep%20Q%20Learning%20-%20Solutions.ipynb" rel="nofollow noreferrer">here</a>, which is an implementation of Deep Q Learning on the CartPole-v0 problem: </p>

<h2>Comments from other forums:</h2>

<ol>
<li>y = r + gamma*np.max(model.predict(s_t_batch), axis=1) is totally natural and y will converge to the true state-action value. And if you don't break down the correlation between consecutive updates with something like experience replay (or better prioritized exp replay) your model WILL diverge. And there are better variants like DDQN, Duelling Network which performs better.</li>
<li>y_batch includes the reward. Both the target and online networks are estimates. It is indeed a somewhat self fulfilling prophecy as DQN's value function is overly optimistic. That is why Double DQN was added a few months later.</li>
<li>y will converge, but not necessarily to the true (I assume you mean optimal) state-action value. No one has proven that the converged value is the optimal value but it is the best approximation we have. However will converge to the the true value for simple enough problems (e.g. grid-world)</li>
</ol>