title: How to use stop_gradient in Tensorflow
body: <p>I'm wondering how to use <code>stop_gradient</code> in tensorflow, and the documentation is not clear to me.</p>
  <p>I'm currently using <code>stop_gradient</code> to produce the gradient of the loss function w.r.t. the word embeddings in a CBOW word2vec model. I want to just get the value, and not do backpropagation (as I'm generating adversarial examples).</p>
  <p>Currently, I'm using the code:</p>
  <pre><code>lossGrad = gradients.gradients(loss, embed)[0]
  real_grad = lossGrad.eval(feed_dict)
  </code></pre>
  <p><s>But when I run this, it does the backpropogation anyway!</s> What am I doing wrong, and just as importantly, how can I fix this?</p>
  <p>CLARIFICATION: To clarify by &quot;backpropagation&quot; I mean &quot;calculating values and updating model parameters&quot;.</p>
  <h3>UPDATE</h3>
  <p>If I run the two lines above after the first training step, the I get a different loss after 100 training steps than when I don't run those two lines. I might be fundamentally misunderstanding something about Tensorflow.</p>
  <p>I've tried setting using <code>set_random_seed</code> both in the beginning of the graph declaration and before each training step. The total loss is consistent between multiple runs, but not between including/excluding those two lines. So if it's not the RNG causing the disparity, and it's not unanticipated updating of the model parameters between training steps, do you have any idea what would cause this behavior?</p>
  <h3>SOLUTION</h3>
  <p>Welp, it's a bit late but here's how I solved it. I only wanted to optimize over some, but not all, variables. I thought that the way to prevent optimizing some variables would be to use <code>stop_grad</code> - but I never found a way to make that work. Maybe there is a way, but what worked for me was to adjust my <code>optimizer</code> to only optimize over a list of variables. So instead of:</p>
  <pre><code>opt = tf.train.GradientDescentOptimizer(learning_rate=eta)
  train_op = opt.minimize(loss)
  </code></pre>
  <p>I used:</p>
  <pre><code>opt = tf.train.GradientDescentOptimizer(learning_rate=eta)
  train_op = opt.minimize(loss, var_list=[variables to optimize over])
  </code></pre>
  <p>This prevented <code>opt</code> from updating the variables not in <code>var_list</code>. Hopefully it works for you, too!</p>