2017-09-15 126 views
0

这是与共享参数,但不同的输入(占位符)创建2个模型的简单tensorflow代码。tensorflow InvalidArgumentError:“您必须喂值占位符张量”

import tensorflow as tf 
import numpy as np 


class Test: 
    def __init__(self): 
     self.x = tf.placeholder(tf.float32, [None] + [64], name='states') 

     self.y = tf.placeholder(tf.float32, [None] + [64], 
           name='y') 
     self.x_test = tf.placeholder(tf.float32, [None] + [64], 
            name='states_test') 

     self.is_training = tf.placeholder(tf.bool, name='is_training') 

     self.model() 

    def network(self, x, reuse): 
     with tf.variable_scope('test_network', reuse=reuse): 
      h1 = tf.layers.dense(x, 64) 
      bn1 = tf.layers.batch_normalization(h1, training=self.is_training) 
      drp1 = tf.layers.dropout(tf.nn.relu(bn1), rate=.9, training=self.is_training, 
            name='dropout') 
      h2 = tf.layers.dense(drp1, 64) 
      bn2 = tf.layers.batch_normalization(h2, training=self.is_training) 
      out = tf.layers.dropout(tf.nn.relu(bn2), rate=.9, training=self.is_training, 
            name='dropout') 
      return out 

    def model(self): 
     self.out = self.network(self.x, False) 
     self.out_test = self.network(self.x_test, True) 

     self.loss = tf.losses.mean_squared_error(self.out, self.y) 
     extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) 
     with tf.control_dependencies(extra_update_ops): 
      self.train_step = tf.train.RMSPropOptimizer(.00002).minimize(self.loss) 


def main(_): 
    my_test = Test() 
    sess = tf.Session() 
    init = tf.global_variables_initializer() 
    sess.run(init) 

    batch_x = np.zeros((4, 64)) 
    batch_y = np.zeros((4, 64)) 
    for i in range(10): 
     feed_dict = {my_test.x: batch_x, my_test.y: batch_y, my_test.is_training: True} 
     _, loss = sess.run([my_test.train_step, my_test.loss], feed_dict) 

if __name__ == '__main__': 
    tf.app.run() 

当我运行“train_step”节点我得到这个错误:

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'states_test' with dtype float and shape [?,64] 
     [[Node: states_test = Placeholder[dtype=DT_FLOAT, shape=[?,64], _device="/job:localhost/replica:0/task:0/gpu:0"]()]] 
     [[Node: mean_squared_error/value/_77 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_2678_mean_squared_error/value", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]] 

虽然train_step节点没有连接到“states_test”占位符ID并不需要它来运行!,那我为什么要喂它呢?

但是,如果我改变,因此第二个网络优化后创建的模型功能,代码运行没有任何错误! (像这样):

def model(self): 
    self.out = self.network(self.x, False) 

    self.loss = tf.losses.mean_squared_error(self.out, self.y) 
    extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) 
    with tf.control_dependencies(extra_update_ops): 
     self.train_step = tf.train.RMSPropOptimizer(.00002).minimize(self.loss) 

    self.out_test = self.network(self.x_test, True) 

为什么会这样即使两个代码导致同样tensorflow图? 任何人都可以解释这种行为?

回答

0

的问题在于使用一批规范的,即这些行:

extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) 
with tf.control_dependencies(extra_update_ops): 
      self.train_step = tf.train.RMSPropOptimizer(.00002).minimize(self.loss) 

注意,你有两个图形中,共享变量 - 你的训练和测试图。你先创建两个,然后创建优化器。但是,使用控制依赖extra_update_ops,这是所有更新OPS的集合。问题是 - 每个批处理规范会创建更新操作(以跟踪均值/差异) - 并且您的火车图中有一个,并且您的测试图中有一个。因此通过请求控制依赖你告诉TF您的火车运营人员可以在执行两列车和测试图表执行当且仅当一批规范的统计数据。这需要喂养测试样品。那么你应该怎么做?更改extra_update_ops只包括列车运行图的更新(通过名称范围,手动过滤或任何其他方法)或构造测试图形之前调用tf.get_collection,所以:

def model(self): 
     self.out = self.network(self.x, False) 
     # Note that at this point we only gather train batch_norms 
     extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) 

     self.out_test = self.network(self.x_test, True) 

     self.loss = tf.losses.mean_squared_error(self.out, self.y) 
     with tf.control_dependencies(extra_update_ops): 
      self.train_step = tf.train.RMSPropOptimizer(.00002).minimize(self.loss) 

你可能想通过重用= true来你的蝙蝠也是。

相关问题