使用TensorFlow MNIST预测专家

时间:2017-01-29 04:02:01

标签: python tensorflow

我正在关注TensorFlow MNIST for Experts教程,但我不知道如何让受过训练的网络预测一组新数据。

下面是我的代码:我在TensorFlow MNIST for Experts tutorial中有所有行,我导入了一个带有pandas的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'

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

1 个答案:

答案 0 :(得分:2)

您需要在训练循环后通过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