2016-11-07 99 views
0

我的代码是这样写的。为什么在tensorflow中的这段代码不起作用?

def __init__(self, X): 
    ops.reset_default_graph() 
    tl.layers.clear_layers_name() 

    self.sess = tf.Session() 

    self.input_x = tf.placeholder(tf.float32, shape=[None, 784],name="input") 

    input_layer = tl.layers.InputLayer(self.input_x) 
    drop1 = tl.layers.DropoutLayer(input_layer, keep=0.8, name="drop1") 
    relu1 = tl.layers.DenseLayer(drop1, n_units=800, act = tf.nn.relu) 
    drop2 = tl.layers.DropoutLayer(relu1, keep=0.5, name="drop2") 

    self.output = drop2.all_layers[-1] 

    self.gradient = tf.gradients(self.output,self.input_x) 

    init_op = tf.initialize_all_variables() 
    self.sess.run(init_op) 
    self.output.eval(session=self.sess, feed_dict={self.input_x:X}) 

正如你所看到的,有只启动了一个占位符,但是,我遇到了

InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float [[Node: Placeholder = Placeholderdtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]]

我百分之百肯定,我喂的X具有类型FLOAT32,形状[ 1000,784。

+1

好像有占位符除了'self.input_x'之外,你必须找到它。 –

回答

1

正如Olivier正确指出的,缺少Feed值的占位符张量的名称与您直接创建的占位符张量的名称(“input”)不同。

如果您使用的是TensorLayer,则可能无法在不了解TensorLayer层内幕的情况下调用session.run或some_tensor.eval。例如,它们的每个DropoutLayer实例都在内部创建tf.placeholder for the keep probability

也就是说这个库似乎希望你只在下面的例子中你通过自己的API(如fittest)像模型交互:

# Train the network, we recommend to use tl.iterate.minibatches() 
tl.utils.fit(sess, network, train_op, cost, X_train, y_train, x, y_, 
      acc=acc, batch_size=500, n_epoch=500, print_freq=5, 
      X_val=X_val, y_val=y_val, eval_train=False) 

# Evaluation 
tl.utils.test(sess, network, acc, X_test, y_test, x, y_, batch_size=None, cost=cost) 

来源:https://github.com/zsdonghao/tensorlayer#your-first-program

+0

谢谢!问题修复了! – xxx222

相关问题