【问题标题】:Predict with TensorFlow MNIST for Experts使用面向专家的 TensorFlow MNIST 进行预测
【发布时间】:2017-01-29 14:29:49
【问题描述】:

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

下面是我的代码:我在TensorFlow MNIST for Experts tutorial 中有所有行,并且我导入了一个带有熊猫的 csv 文件作为数据框 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'

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

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

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

    【讨论】:

    • 谢谢,这真的很有帮助。
    • 没问题,你可以像这样检索图表中的任何值。重量、损失等。
    猜你喜欢
    • 1970-01-01
    • 2017-10-10
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    相关资源
    最近更新 更多