首页 文章

为什么tensorflow中的这段代码不起作用?

提问于
浏览
0

我的代码是这样编写的 .

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:您必须使用dtype float [[Node:Placeholder = Placeholderdtype = DT_FLOAT,shape = [],_ device =“/ job:localhost / replica:0 / task:0 / cpu)为占位符张量”占位符“提供值: 0" ]]

我百分百肯定我输入的 X 具有float32类型和形状[1000,784] .

2 回答

  • 2

    正如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

  • 2

    你有 TWO 方法在TensorLayer中使用 DropoutLayer

    1)使用由TensorLayer创建的内部保持概率占位符,请参阅tutorial_mlp_dropout1.py

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

    因此,在您的情况下,如果您不想使用TensorLayer创建的内部占位符,则可以使用第二种方式 .

相关问题