2015-11-18 14 views
22

Tôi thậm chí còn hỏi? Đây là một công nghệ mới vào thời điểm này mà tôi không thể tìm thấy một cách để giải quyết lỗi này dường như đơn giản. Bạn có thể tìm thấy hướng dẫn tôi xem tại đây- http://www.tensorflow.org/tutorials/mnist/pros/index.html#deep-mnist-for-expertsTensorFlow Lỗi được tìm thấy trong Hướng dẫn

Tôi đã sao chép và dán tất cả mã vào Máy tính xách tay IPython và ở đoạn cuối cùng của mã tôi nhận được lỗi.

# To train and evaluate it we will use code that is nearly identical to that for the simple one layer SoftMax network above. 
# The differences are that: we will replace the steepest gradient descent optimizer with the more sophisticated ADAM optimizer. 

cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv)) 
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) 
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1)) 
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) 
sess.run(tf.initialize_all_variables()) 
for i in range(20000): 
    batch = mnist.train.next_batch(50) 
    if i%100 == 0: 
     train_accuracy = accuracy.eval(feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0}) 
    print "step %d, training accuracy %g"%(i, train_accuracy) 
    train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5}) 

print "test accuracy %g"%accuracy.eval(feed_dict={ 
    x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}) 

Sau khi chạy mã này, tôi nhận được lỗi này.

--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-46-a5d1ab5c0ca8> in <module>() 
    15 
    16 print "test accuracy %g"%accuracy.eval(feed_dict={ 
---> 17  x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}) 

/root/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in eval(self, feed_dict, session) 
    403 
    404  """ 
--> 405  return _eval_using_default_session(self, feed_dict, self.graph, session) 
    406 
    407 

/root/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in _eval_using_default_session(tensors, feed_dict, graph, session) 
    2712  session = get_default_session() 
    2713  if session is None: 
-> 2714  raise ValueError("Cannot evaluate tensor using eval(): No default " 
    2715      "session is registered. Use 'with " 
    2716      "DefaultSession(sess)' or pass an explicit session to " 

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

Tôi nghĩ rằng tôi có thể cần phải cài đặt hoặc cài đặt lại TensorFlow thông qua conda install https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.5.0-cp27-none-linux_x86_64.whl nhưng conda thậm chí không biết cách cài đặt.

Có ai có ý tưởng về cách khắc phục lỗi này không?

Trả lời

29

Tôi đã tìm ra. Như bạn thấy trong lỗi giá trị, nó nói No default session is registered. Use 'with DefaultSession(sess)' or pass an explicit session to eval(session=sess) vì vậy câu trả lời tôi đưa ra là để vượt qua một phiên rõ ràng để eval, giống như nó nói. Đây là nơi tôi thực hiện các thay đổi.

if i%100 == 0: 
     train_accuracy = accuracy.eval(session=sess, feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0}) 

train_step.run(session=sess, feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5}) 

Bây giờ đang làm việc tốt.

+12

hoặc bạn chỉ có thể tạo ra phiên, sess = tf.InteractiveSession, và sau đó thả "phiên = sess" args, nó sẽ đưa phiên bạn đã tạo theo mặc định –

+0

Cảm ơn. Những công việc này. @YaroslavBulatov cool. :) – dksahuji

1

Tôi gặp phải lỗi tương tự khi thử một ví dụ về lưu lượng đơn giản.

import tensorflow as tf 
v = tf.Variable(10, name="v") 
sess = tf.Session() 
sess.run(v.initializer) 
print(v.eval()) 

Giải pháp của tôi là sử dụng sess.as_default(). Ví dụ: tôi đã thay đổi mã của mình thành mã sau và đã hoạt động:

import tensorflow as tf 
v = tf.Variable(10, name="v") 
with tf.Session().as_default() as sess: 
    sess.run(v.initializer)  
    print(v.eval()) 

Một giải pháp khác có thể sử dụng InteractiveSession. Sự khác biệt giữa InteractiveSession và Session là một InteractiveSession tự tạo phiên mặc định để bạn có thể chạy() hoặc eval() mà không gọi một cách rõ ràng phiên.

v = tf.Variable(10, name="v") 
sess = tf.InteractiveSession() 
sess.run(v.initializer) 
print(v.eval()) 
Các vấn đề liên quan