我正在关注TensorFlow MNIST for Experts教程,但是我不知道如何让训练有素的网络预测新的数据集。

下面是我的代码:我在TensorFlow MNIST for Experts tutorial中包含了所有代码行,并导入了一个带有熊猫的csv文件作为dataframe testt。

y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
feed_dict = {x: testt[0], keep_prob:1.0}
classification = y_conv.eval(y, feed_dict)
print(classification)


我得到这个错误

AttributeError                            Traceback (most recent call last)
<ipython-input-36-96dfe9b26149> in <module>()
      2 y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
      3 feed_dict = {x: testt[0], keep_prob:1.0}
----> 4 classification = y_conv.eval(y, feed_dict)
      5 print(classification)

C:\Program Files\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in eval(self, feed_dict, session)
    573
    574     """
--> 575     return _eval_using_default_session(self, feed_dict, self.graph, session)
    576
    577

C:\Program Files\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in _eval_using_default_session(tensors, feed_dict, graph, session)
   3627                        "`eval(session=sess)`.")
   3628   else:
-> 3629     if session.graph is not graph:
   3630       raise ValueError("Cannot use the given session to evaluate tensor: "
   3631                        "the tensor's graph is different from the session's "

AttributeError: 'dict' object has no attribute 'graph'


请帮忙。我不确定如何正确呼叫受过训练的网络。

最佳答案

您只需在训练循环后通过sess.run调用获取y的输出:

with tf.Session() as sess:
    for i in range(1000):
        batch = mnist.train.next_batch(100)
        train_step.run(feed_dict={x: batch[0], y_: batch[1]})

    logits = sess.run(y, feed_dict={x: test_data})
    pred = tf.nn.softmax(logits)
    print(pred)  # or print(tf.argmax(pred, dimension=1)) for the index

关于python - 使用TensorFlow MNIST进行专家预测,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41917491/

10-12 22:53