Python 3 - FileNotFoundError:[Errno 2]没有这样的文件或目录:' ../ data / mnist.pkl'

时间:2018-04-09 12:05:30

标签: python python-3.x mnist

我正在尝试加载此神经网络和深度学习示例(http://neuralnetworksanddeeplearning.com/chap1.html)的mnist数据集,但我一直收到此错误。

>>> import mnist_loader
>>> training_data, validation_data, test_data = mnist_loader.load_data_wrapper()
Traceback (most recent call last):
  File "<pyshell#34>", line 1, in <module>
    training_data, validation_data, test_data = mnist_loader.load_data_wrapper()
  File "C:/Users/Joseph Gannon/AppData/Local/Programs/Python/Python36-32\mnist_loader.py", line 68, in load_data_wrapper
    tr_d, va_d, te_d = load_data()
  File "C:/Users/Joseph Gannon/AppData/Local/Programs/Python/Python36-32\mnist_loader.py", line 42, in load_data
    f = open('mnist.pkl', 'rb')
  File "C:\Users\Joseph Gannon\AppData\Local\Programs\Python\Python36-32\lib\gzip.py", line 53, in open
    binary_file = GzipFile(filename, gz_mode, compresslevel)
  File "C:\Users\Joseph Gannon\AppData\Local\Programs\Python\Python36-32\lib\gzip.py", line 163, in __init__
    fileobj = self.myfileobj = builtins.open(filename, mode or 'rb')
FileNotFoundError: [Errno 2] No such file or directory: '../data/mnist.pkl'

这是我正在使用的代码:

"""
mnist_loader
~~~~~~~~~~~~

A library to load the MNIST image data.  For details of the data
structures that are returned, see the doc strings for ``load_data``
and ``load_data_wrapper``.  In practice, ``load_data_wrapper`` is the
function usually called by our neural network code.
"""

#### Libraries
# Standard library
import _pickle as cPickle
import gzip

# Third-party libraries
import numpy as np

def load_data():
    """Return the MNIST data as a tuple containing the training data,
    the validation data, and the test data.

    The ``training_data`` is returned as a tuple with two entries.
    The first entry contains the actual training images.  This is a
    numpy ndarray with 50,000 entries.  Each entry is, in turn, a
    numpy ndarray with 784 values, representing the 28 * 28 = 784
    pixels in a single MNIST image.

    The second entry in the ``training_data`` tuple is a numpy ndarray
    containing 50,000 entries.  Those entries are just the digit
    values (0...9) for the corresponding images contained in the first
    entry of the tuple.

    The ``validation_data`` and ``test_data`` are similar, except
    each contains only 10,000 images.

    This is a nice data format, but for use in neural networks it's
    helpful to modify the format of the ``training_data`` a little.
    That's done in the wrapper function ``load_data_wrapper()``, see
    below.
    """
    f = gzip.open('mnist.pkl.gz', 'rb')
    training_data, validation_data, test_data = cPickle.load(f)
    f.close()
    return (training_data, validation_data, test_data)

def load_data_wrapper():
    """Return a tuple containing ``(training_data, validation_data,
    test_data)``. Based on ``load_data``, but the format is more
    convenient for use in our implementation of neural networks.

    In particular, ``training_data`` is a list containing 50,000
    2-tuples ``(x, y)``.  ``x`` is a 784-dimensional numpy.ndarray
    containing the input image.  ``y`` is a 10-dimensional
    numpy.ndarray representing the unit vector corresponding to the
    correct digit for ``x``.

    ``validation_data`` and ``test_data`` are lists containing 10,000
    2-tuples ``(x, y)``.  In each case, ``x`` is a 784-dimensional
    numpy.ndarry containing the input image, and ``y`` is the
    corresponding classification, i.e., the digit values (integers)
    corresponding to ``x``.

    Obviously, this means we're using slightly different formats for
    the training data and the validation / test data.  These formats
    turn out to be the most convenient for use in our neural network
    code."""
    tr_d, va_d, te_d = load_data()
    training_inputs = [np.reshape(x, (784, 1)) for x in tr_d[0]]
    training_results = [vectorized_result(y) for y in tr_d[1]]
    training_data = zip(training_inputs, training_results)
    validation_inputs = [np.reshape(x, (784, 1)) for x in va_d[0]]
    validation_data = zip(validation_inputs, va_d[1])
    test_inputs = [np.reshape(x, (784, 1)) for x in te_d[0]]
    test_data = zip(test_inputs, te_d[1])
    return (training_data, validation_data, test_data)

def vectorized_result(j):
    """Return a 10-dimensional unit vector with a 1.0 in the jth
    position and zeroes elsewhere.  This is used to convert a digit
    (0...9) into a corresponding desired output from the neural
    network."""
    e = np.zeros((10, 1))
    e[j] = 1.0
    return e

我该如何解决?

4 个答案:

答案 0 :(得分:0)

我喜欢99%确定你提供错误的文件路径来获取这样的错误。

答案 1 :(得分:0)

您面临的错误是:

FileNotFoundError: [Errno 2] No such file or directory: '../data/mnist.pkl'

这意味着python无法找到输入数据文件mnist.pkl

代码正在以下位置../data/mnist.pkl

中查找该文件

哪个应该是当前文件夹上方的一个文件夹,并且位于data文件夹中。

  • 文件mnist.pkl是否存在?

另请注意,Windows / Unix路径方向之间的差异可能也会导致问题。 c:\ vs ../data/

答案 2 :(得分:0)

面对许多问题之后,

示例

允许

temp =“ W:\ Projects”

temp =“ W:\ Python项目”

temp =“ C:\ users”

temp =“ C:\ Program Files”

不允许

temp =“ C:\ Program Files(x86)”

temp =“ C:\ Program Files(x86)\ DocClass”

pickle.dump(train_tfidf_transformer,open(temp +“ \ train_TfidfTransformer.pkl,” wb“))

我的结论是文件夹名称中没有两个空格,但是如果直接给出此路径,那么它就起作用了,因为当您传递诸如temp之类的字符串时,他们会检查该字符串是否为原始字符串?如果没有转换为原始字符串(如果是),则没有转换

答案 3 :(得分:0)

.pkl 文件是在运行 .py 文件后创建的。您必须在指定目录中有 mnist.py 文件。运行'python mnist.py'