2017-02-08 19 views
2

我一直在瀏覽幾篇Tensorflow教程,並且在使用模型進行了訓練/測試之後,還沒有看到任何有關使用模型的內容。我通過計算器看了看,發現不喜歡here如何在Tensorflow中預測未標記的圖像

工作了我,所以我正在使用的代碼here與我改變了代碼,所以我可以嘗試運行的預測,而不是事後的會議閉幕除外幾個解決方案。對於預測,我只是使用一個測試樣本,但試圖在沒有給出標籤的情況下完成。我想看看什麼課程是預測的。

# Launch the graph 
#with tf.Session() as sess: 
sess = tf.Session() 
sess.run(init) 
step = 1 
# Keep training until reach max iterations 
while step * batch_size < training_iters: 
    batch_x, batch_y = mnist.train.next_batch(batch_size) 
    # Run optimization op (backprop) 
    sess.run(optimizer, feed_dict={x: batch_x, y: batch_y, 
           keep_prob: dropout}) 
    if step % display_step == 0: 
     # Calculate batch loss and accuracy 
     loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x, 
                 y: batch_y, 
                 keep_prob: 1.}) 
     print("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \ 
      "{:.6f}".format(loss) + ", Training Accuracy= " + \ 
      "{:.5f}".format(acc)) 
    step += 1 
print("Optimization Finished!") 

# Calculate accuracy for 256 mnist test images 
print("Testing Accuracy:", \ 
    sess.run(accuracy, feed_dict={x: mnist.test.images[:256], 
            y: mnist.test.labels[:256], 
            keep_prob: 1.})) 

從我上面列出的我應該可以做這樣的事情

print(tf.run(pred, feed_dict={x: mnist.test.images[0]})) 

雖然它看起來像作爲tensorflow是說有沒有運行該功能已被刪除堆棧溢出頁。在同一頁的註釋建議這樣做

print(pred.eval(feed_dict={x: mnist.test.images[0]})) 

,但我發現這個錯誤

ValueError: Cannot evaluate tensor using `eval()`: No default session is registered. Use `with sess.as_default()` or pass an explicit session to  `eval(session=sess)` 

有了這個,我發現我需要運行哪些上面說過,但我發現一個問題,張量是不正確的大小

with sess.as_default(): 
    print(pred.eval(feed_dict={x: mnist.test.images[0]})) 

ValueError: Cannot feed value of shape (784,) for Tensor 'Placeholder:0', which has shape '(?, 784)' 

因此,從這裏看起來像數據沒有正確對齊?我試過使用重塑沒有成功。如果任何人都可以將我指向正確的方向,那麼我可以弄清楚如何實際使用我的模型來獲得很好的應用程序。

編輯:這是一個更簡單的程序。我有同樣的問題

from tensorflow.examples.tutorials.mnist import input_data 
mnist = input_data.read_data_sets('MNIST_data', one_hot=True) 

import tensorflow as tf 
sess = tf.InteractiveSession() 

x = tf.placeholder(tf.float32, shape=[None, 784]) 
y_ = tf.placeholder(tf.float32, shape=[None, 10]) 

W = tf.Variable(tf.zeros([784,10])) 
b = tf.Variable(tf.zeros([10])) 

sess.run(tf.global_variables_initializer()) 

y = tf.matmul(x,W) + b 

cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y, y_)) 

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) 

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

correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) 
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 
print(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels})) 
y.eval(feed_dict={x: mnist.test.images[0]}) 

我得到了同樣的問題如上

ValueError: Cannot feed value of shape (784,) for Tensor 'Placeholder:0', which has shape '(?, 784)' 

回答

0

您的筆記本電腦的鏈接是本地鏈路,所以沒有人能看到它。此外,你錯過了一些代碼。 「y」的定義在哪裏?

所有你需要做的就是評估y,例如:y.eval(feed_dict = {x:NEW_DATA})

+0

道歉。我更新了鏈接。我將使用所有代碼編輯我的帖子。 – Exuro

+0

我做了一個編輯以顯示一個較小的例子。如您所述,我正在接受y輸出並使用eval。現在我得到一個輸出,但我不確定這些值是什麼。 – Exuro

+0

好吧,我想通了。得到了評估工作。我的問題是沒有認識到產出代表着這個階級,而最高價值是最可能的。我將找出如何將其轉化爲0 - 1概率。謝謝。 – Exuro