【问题标题】:Why this code in tensorflow doesn't work?为什么 tensorflow 中的这段代码不起作用?
【发布时间】:2018-04-29 23:41:52
【问题描述】:

我的代码是这样写的。

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:您必须为占位符张量提供一个值 'Placeholder' 与 dtype 浮动 [[节点:占位符 = Placeholderdtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]]

我百分百确定我输入的 X 的类型为 float32,形状为 [1000,784]。

【问题讨论】:

  • 好像在self.input_x以外的地方有占位符,你必须找到它。

标签: machine-learning tensorflow deep-learning


【解决方案1】:

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

如果您使用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

【讨论】:

    【解决方案2】:

    您有两种方法在 TensorLayer 中使用 DropoutLayer

    1)使用TensorLayer创建的内部keep prob占位符,见tutorial_mlp_dropout1.py

    2) 我们没有使用占位符来控制保持概率,而是构建了两个图用于训练和测试,参见tutorial_mlp_dropout2.py

    所以在你的情况下,如果你不想使用 TensorLayer 创建的内部占位符,你可以使用第二种方式。

    【讨论】:

      猜你喜欢
      • 2010-09-18
      相关资源
      最近更新 更多