gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:27:16 25 4
gpt4 key购买 nike

我的代码是这样写的。

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]。

最佳答案

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

关于machine-learning - 为什么 tensorflow 中的这段代码不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40457938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com