title: How to use feed_dict with output from Tensorflow Layer?
body: <p>How can I feed a tf.placeholder with the output of another layer?</p>
  
  <p>I would like to train neural networks and then reconfigure them for training with other networks. To best explain my problem, I have simplified my code:</p>
  
  <pre><code>import numpy as np
  import tensorflow as tf
  
  dtype_tf = tf.float32
  batch_size = 32
  
  nn1_input_shape = 3
  nn1_output_shape = 10
  nn1_input_test = np.random.randn(batch_size, nn1_input_shape)
  
  nn2_input_shape = 10
  nn2_output_shape = 2
  nn2_input_test = np.random.randn(batch_size, nn2_input_shape)
  
  nn1_input = tf.placeholder(dtype_tf, shape=[None, nn1_input_shape])
  nn1_output = tf.placeholder(dtype_tf, shape=[None, nn1_output_shape])
  nn1_pred = tf.layers.Dense(nn1_output_shape)(nn1_input)
  
  nn2_input = tf.placeholder(dtype_tf, shape=[None, nn2_input_shape])
  nn2_output = tf.placeholder(dtype_tf, shape=[None, nn2_output_shape])
  nn2_pred = tf.layers.Dense(nn2_output_shape)(nn2_input)
  
  init = tf.global_variables_initializer()
  
  sess = tf.Session()
  sess.run(init)
  
  a = sess.run(nn1_pred, feed_dict={nn1_input: nn1_input_test}) # works! returns array with shape: (32, 10)
  b = sess.run(nn2_pred, feed_dict={nn2_input: nn2_input_test}) # works! returns array with shape: (32, 2)
  c = sess.run(nn2_pred, feed_dict={nn2_input: a}) # works! returns array with shape: (32, 2)
  d = sess.run(nn2_pred, feed_dict={nn2_input: nn1_pred, nn1_input: nn1_input_test}) # error
  </code></pre>
  
  <p>I would like to train network 2, duplicate it multiple times and build another network around it.
  Option <code>c=</code> works, but does not allow me to pass gradients through network 2. Option <code>d=</code> returns the following:</p>
  
  <pre><code>Backend Qt5Agg is interactive backend. Turning interactive mode on.
  2019-11-18 XX:XX:XX.XXXXXX: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
  2019-11-18 XX:XX:XX.XXXXXX: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:964] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
  2019-11-18 XX:XX:XX.XXXXXX: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1411] Found device 0 with properties: 
  name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7335
  pciBusID: 0000:01:00.0
  totalMemory: 7.93GiB freeMemory: 7.70GiB
  2019-11-18 XX:XX:XX.XXXXXX: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1490] Adding visible gpu devices: 0
  2019-11-18 XX:XX:XX.XXXXXX: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] Device interconnect StreamExecutor with strength 1 edge matrix:
  2019-11-18 XX:XX:XX.XXXXXX: I tensorflow/core/common_runtime/gpu/gpu_device.cc:977]      0 
  2019-11-18 XX:XX:XX.XXXXXX: I tensorflow/core/common_runtime/gpu/gpu_device.cc:990] 0:   N 
  2019-11-18 XX:XX:XX.XXXXXX: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1103] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 7434 MB memory) -&gt; physical GPU (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1)
  Traceback (most recent call last):
    File "/somePathToMyAnacondaEnv/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2882, in run_code
      exec(code_obj, self.user_global_ns, self.user_ns)
    File "&lt;ipython-input-2-bba3c2d1e391&gt;", line 36, in &lt;module&gt;
      d = sess.run(nn2_pred, feed_dict={nn2_input: nn1_pred, nn1_input: nn1_input_test}) # error
    File "/somePathToMyAnacondaEnv/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 887, in run
      run_metadata_ptr)
    File "/somePathToMyAnacondaEnv/envs/IotaLearning27/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1061, in _run
      'feed with key ' + str(feed) + '.')
  TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles.For reference, the tensor object was Tensor("dense/BiasAdd:0", shape=(?, 10), dtype=float32) which was passed to the feed with key Tensor("Placeholder_2:0", shape=(?, 10), dtype=float32).
  </code></pre>
  
  <p>More specifically:
  Network 2 (nn2, shown in yellow) represents a physical device with unexplainable behavior. When trained, nn2 does a good job of fitting to experimental observation. Next, I would like to freeze these weights (presumably using tf.optimizer.adam.minimize(var_list=[...]), etc) and duplicate nn2 multiple times so that it can be used as a stand-alone layer in a larger network (nn1, shown in blue). The end goal is to optimize a system of multiple devices by adjusting the physical device parameters (weights in nn1) according to some loss function (output of nn2).</p>
  
  <p>Thank you! Any suggestions are appreciated!</p>
  
  <p><a href="https://i.stack.imgur.com/LghEK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LghEK.png" alt="Rough representation of desired network."></a></p>