title: stopping condition on gradient value tensorflow
body: <p>I would like to implement a stopping condition based on the value of the gradient of the loss function w.r.t. the weights.
  For example, let's say I have something like this:</p>
  
  <pre><code>optimizer = tf.train.AdamOptimizer()
  grads_and_vars = optimizer.compute_gradients(a_loss_function)
  train_op = optimizer.apply_gradients(grads_and_vars)
  </code></pre>
  
  <p>then I would like to run the graph with something like this:</p>
  
  <pre><code>for step in range(TotSteps):
      output = sess.run([input], feed_dict=some_dict)
      if(grad_taken_in_some_way &lt; some_treshold):
          print("Training finished.")
          break
  </code></pre>
  
  <p>I am not sure what I should pass to sess.run() in order to get as output also the gradient (besides all other stuff I need). I am not even sure whether this is the correct approach or I should do it differently. I made some tries but I failed every time. Hope someone has some hints.
  Thank you in advance!</p>
  
  <p>EDIT: English correction</p>
  
  <p>EDIT2: Answer by <strong>Iballes</strong> is exactly what I wanted to do. Still, I am not sure how to norm and sum all the gradients. Since I have different layer in my CNN and different weights with different shape, if I just do what you suggested, I get an error on the add_n() operation (since I am trying to add together matrices with different shapes). So probably I should do something like:</p>
  
  <pre><code>grad_norms = [tf.nn.l2_normalize(g[0], 0) for g in grads_and_vars]      
  grad_norm = [tf.reduce_sum(grads) for grads in grad_norms]
  final_grad = tf.reduce_sum(grad_norm)
  </code></pre>
  
  <p>Can anyone confirm this?</p>