如何将feed\ u dict与Tensorflow层的输出一起使用?

2024-09-29 01:27:31 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将另一层的输出提供给tf.placeholder

我想训练神经网络,然后重新配置它们与其他网络一起训练。为了更好地解释我的问题,我简化了代码:

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

我想训练网络2,复制它多次,并围绕它建立另一个网络。 选项c=起作用,但不允许我通过网络2传递渐变。选项d=返回以下内容:

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) -> 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 "<ipython-input-2-bba3c2d1e391>", line 36, in <module>
    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).

更具体地说: 网络2(nn2,以黄色显示)表示具有无法解释的行为的物理设备。经过训练后,nn2能很好地拟合实验观测。接下来,我想冻结这些权重(可能使用tf.optimizer.adam.minimize(var_list=[…]),等等)并多次复制nn2,以便它可以在更大的网络中用作独立层(nn1,以蓝色显示)。最终目标是通过根据损失函数(nn2的输出)调整物理设备参数(nn1中的权重)来优化多设备系统

谢谢你!如有任何建议,我们将不胜感激

Rough representation of desired network.


Tags: runinputoutputgpudevicetftensorflowfeed