python - 在Python中找不到指定相对路径的文件

标签 python tensorflow

通常在Python中,默认文件夹应该是我假设的当前工作目录,或者可能在默认用户目录中。但是,运行以下代码后from here ,我在之前的两个地方都找不到下载的数据。那么问题是相对路径 /tmp/tensorflow/mnist/input_data 位于哪里?谢谢!

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)

最佳答案

我还运行了这些示例,并且我的路径存储在 c: 中。我正在使用 window 。

完整路径是:

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')

关于python - 在Python中找不到指定相对路径的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43764701/

相关文章:

python - 将 tf.dataset 中的每个样本映射到 id

python-3.x - 如何获得 Tensorflow 中线性回归的正确答案?

python - 尝试使用多线程并行化 python 算法并避免 GIL 限制

python - 为什么 IPython 在它启动的目录中执行 code.py?

python - 训练 tf.estimator.DNNClassifier 时打印准确率

python - Tensorflow 代码破解

python - 在导入 python 时。 ImportError : libcublas. so.9.0: 无法打开共享对象文件: 没有那个文件或目录

python - 根据 pandas 中的值列表获取数据框的行

python - 给定顶点 i 在 pylab 中绘制 3d 曲面

python - 如何在opencv-cv2(Pycharm和macOS)中解决 “process finished with exit code 139 (interrupted by signal 11: SIGSEGV)”?