title: TensorFlow: "Cannot capture a stateful node by value" in tf.contrib.data API
body: <p>For transfer learning, one often uses a network as a feature extractor to create a dataset of features, on which another classifier is trained (e.g. a SVM).</p>
  
  <p>I want to implement this using the Dataset API (<a href="https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/data" rel="nofollow noreferrer"><code>tf.contrib.data</code></a>) and <a href="https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/data#transforming-a-dataset" rel="nofollow noreferrer"><code>dataset.map()</code></a>:</p>
  
  <pre><code># feature_extractor will create a CNN on top of the given tensor
  def features(feature_extractor, ...):
      dataset = inputs(...)  # This creates a dataset of (image, label) pairs
  
      def map_example(image, label):
          features = feature_extractor(image, trainable=False)
          #  Leaving out initialization from a checkpoint here... 
          return features, label
  
      dataset = dataset.map(map_example)
  
      return dataset
  </code></pre>
  
  <p>Doing this fails when creating an iterator for the dataset.</p>
  
  <pre><code>ValueError: Cannot capture a stateful node by value.
  </code></pre>
  
  <p>This is true, the kernels and biases of the network are variables and thus stateful. For this particular example they don't have to be though.</p>
  
  <p>Is there a way to make Ops and specifically <a href="https://www.tensorflow.org/api_docs/python/tf/Variable" rel="nofollow noreferrer"><code>tf.Variable</code></a> objects stateless?</p>
  
  <p>Since I'm using <a href="https://www.tensorflow.org/api_docs/python/tf/layers" rel="nofollow noreferrer"><code>tf.layers</code></a> I cannot simply create them as constants, and setting <code>trainable=False</code> won't create constants neither but just won't add the variables to the <code>GraphKeys.TRAINABLE_VARIABLES</code> collection.</p>