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)
我可能會誤解你的問題,但 的/ tmp/tensorflow/MNIST /輸入_data是絕對路徑。 「所以問題在於相對路徑/ tmp/tensorflow/mnist/input_data位於何處?」 – kecso
@kecso這是一個很好的觀點。我認爲這是一條相對路徑......那麼我怎麼能找到這條路?謝謝 –
這正是/ tmp/tensorflow/mnist/input_data路徑。你應該把它放在你的盒子上。或者只是使用像「mnist/input_data」這樣的相關路徑:「/ path_to_my_py/mnist/input_data」 – kecso