2017-05-03 68 views
1

通常在Python中,默認文件夾應該是我假設的當前工作目錄,或者可能位於默認用戶目錄中。但是,運行以下代碼from here後,我無法在以前的任何地方找到下載的數據。所以問題是相對路徑/tmp/tensorflow/mnist/input_data位於哪裏?謝謝!無法在Python中的指定相對路徑中找到文件

from __future__ import absolute_import 
from __future__ import division 
from __future__ import print_function 

import argparse 
import sys 

from tensorflow.examples.tutorials.mnist import input_data 

import tensorflow as tf 

FLAGS = None 


def main(_): 
    # Import data 
    mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True) 

    # Create the model 
    x = tf.placeholder(tf.float32, [None, 784]) 
    W = tf.Variable(tf.zeros([784, 10])) 
    b = tf.Variable(tf.zeros([10])) 
    y = tf.matmul(x, W) + b 

    # Define loss and optimizer 
    y_ = tf.placeholder(tf.float32, [None, 10]) 

    # The raw formulation of cross-entropy, 
    # 
    # tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.nn.softmax(y)), 
    #         reduction_indices=[1])) 
    # 
    # can be numerically unstable. 
    # 
    # So here we use tf.nn.softmax_cross_entropy_with_logits on the raw 
    # outputs of 'y', and then average across the batch. 
    cross_entropy = tf.reduce_mean(
     tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y)) 
    train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) 

    sess = tf.InteractiveSession() 
    tf.global_variables_initializer().run() 
    # Train 
    for _ in range(1000): 
    batch_xs, batch_ys = mnist.train.next_batch(100) 
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) 

    # Test trained model 
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) 
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 
    print(sess.run(accuracy, feed_dict={x: mnist.test.images, 
             y_: mnist.test.labels})) 

if __name__ == '__main__': 
    parser = argparse.ArgumentParser() 
    parser.add_argument('--data_dir', type=str, default='/tmp/tensorflow/mnist/input_data', 
         help='Directory for storing input data') 
    FLAGS, unparsed = parser.parse_known_args() 
    tf.app.run(main=main, argv=[sys.argv[0]] + unparsed) 
+0

我可能會誤解你的問題,但 的/ tmp/tensorflow/MNIST /輸入_data是絕對路徑。 「所以問題在於相對路徑/ tmp/tensorflow/mnist/input_data位於何處?」 – kecso

+0

@kecso這是一個很好的觀點。我認爲這是一條相對路徑......那麼我怎麼能找到這條路?謝謝 –

+0

這正是/ tmp/tensorflow/mnist/input_data路徑。你應該把它放在你的盒子上。或者只是使用像「mnist/input_data」這樣的相關路徑:「/ path_to_my_py/mnist/input_data」 – kecso

回答

1

我也運行這些例子,我的路徑獲取存儲在c :.我正在使用Windows。

的完整路徑是:

C:\ tmp目錄\ tensorflow \ MNIST \ input_data

如果你希望它是相對於你的工作目錄中添加一個點( 「」 )在你的代碼路徑前:

parser.add_argument('--data_dir', type=str, default='./tmp/tensorflow/mnist/input_data', 
         help='Directory for storing input data') 
相關問題