title: "freeze" some variables/scopes in tensorflow: stop_gradient vs passing variables to minimize
body: <p>I am trying to implement <a href="http://arxiv.org/abs/1406.2661">Adversarial NN</a>, which requires to 'freeze' one or the other part of the graph during alternating training minibatches. I.e. there two sub-networks: G and D.</p>
  
  <pre><code>G( Z ) -&gt;  Xz
  D( X ) -&gt;  Y
  </code></pre>
  
  <p>where loss function of <code>G</code> depends on <code>D[G(Z)], D[X]</code>.</p>
  
  <p>First I need to train parameters in D with all G parameters fixed, and then parameters in G with parameters in D fixed. Loss function in first case will be negative loss function in the second case and the update will have to apply to the parameters of whether first or second subnetwork.</p>
  
  <p>I saw that tensorflow has <code>tf.stop_gradient</code> function. For purpose of training the D (downstream) subnetwork I can use this function to block the gradient flow to </p>
  
  <pre><code> Z -&gt; [ G ] -&gt; tf.stop_gradient(Xz) -&gt; [ D ] -&gt; Y
  </code></pre>
  
  <p>The <code>tf.stop_gradient</code> is very succinctly annotated with no in-line example (and example <code>seq2seq.py</code> is too long and not that easy to read), but looks like it must be called during the graph creation. <strong>Does it imply that if I want to block/unblock gradient flow in alternating batches, I need to re-create and re-initialize the graph model?</strong> </p>
  
  <p>Also it seems that <strong>one cannot block the gradient flowing through the G (upstream) network by means of <code>tf.stop_gradient</code>, right?</strong></p>
  
  <p>As an alternative I saw that one can pass the list of variables to the optimizer call as <code>opt_op = opt.minimize(cost, &lt;list of variables&gt;)</code>, which would be an easy solution if one could get all variables in the scopes of each subnetwork. <strong>Can one get a <code>&lt;list of variables&gt;</code> for a tf.scope?</strong></p>