<p><code>tf.gradients</code> returns the gradient with respect to the loss. This means that if your loss is a sum of per-example losses, then the gradient is also the sum of per-example loss gradients. </p>

<p>The summing up is implicit. For instance if you want to minimize the sum of squared norms of <code>Wx-y</code> errors, the gradient with respect to <code>W</code> is <code>2(WX-Y)X'</code> where <code>X</code> is the batch of observations and <code>Y</code> is the batch of labels. You never explicitly form "per-example" gradients that you later sum up, so it's not a simple matter of removing some stage in the gradient pipeline.</p>

<p>A simple way to get <code>k</code> per-example loss gradients is to use batches of size 1 and do <code>k</code> passes. Ian Goodfellow <a href="http://arxiv.org/pdf/1510.01799v2.pdf" rel="noreferrer">wrote up</a> how to get all <code>k</code> gradients in a single pass, for this you would need to specify gradients explicitly and not rely on <code>tf.gradients</code> method</p>
Unaggregated gradients / gradients per example in tensorflow
<p>Given a simple mini-batch gradient descent problem on mnist in tensorflow (like in this <a href="https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/tutorials/mnist/mnist_with_summaries.py" rel="noreferrer">tutorial</a>), how can I retrieve the gradients for each example in the batch individually.</p>

<p><code>tf.gradients()</code> seems to return gradients averaged over all examples in the batch. Is there a way to retrieve gradients before aggregation?</p>

<p>Edit: A first step towards this answer is figuring out at which point tensorflow averages the gradients over the examples in the batch. I thought this happened in <a href="https://github.com/tensorflow/tensorflow/blob/8048088bfb82846078f8023bc6199e6424926624/tensorflow/python/ops/gradients.py#L437" rel="noreferrer">_AggregatedGrads</a>, but that doesn't appear to be the case. Any ideas?</p>