Windows

时间:2017-07-29 15:57:24

标签: python machine-learning neural-network

我在D:

中提取了这4个文件
train-images-idx3-ubyte
train-labels-idx1-ubyte 
t10k-images-idx3-ubyte
t10k-labels-idx1-ubyte

我收到错误:

FileNotFoundError: [Errno 2] No such file or directory: 'D:/train-labels-idx1-ubyte'`

我的代码:

def load_mnist(path, kind='train'):
    """Load MNIST data from `path`"""
    path = "D:/"
    labels_path = os.path.join(path,
                               '%s-labels-idx1-ubyte' % kind)
    images_path = os.path.join(path,
                               '%s-images-idx3-ubyte' % kind)

    with open(labels_path, 'rb') as lbpath:
        magic, n = struct.unpack('>II',
                                 lbpath.read(8))
        labels = np.fromfile(lbpath,
                             dtype=np.uint8)

    with open(images_path, 'rb') as imgpath:
        magic, num, rows, cols = struct.unpack(">IIII",
                                               imgpath.read(16))
        images = np.fromfile(imgpath,
                             dtype=np.uint8).reshape(len(labels), 784)

    return images, labels

完整错误:

Traceback (most recent call last):
  File "C:/Users/PycharmProjects/MachineLearning/NN2.py", line 28, in <module>
    X_train, y_train = load_mnist('mnist/', kind='train')
  File "C:/Users/PycharmProjects/MachineLearning/NN2.py", line 14, in load_mnist
    with open(labels_path, 'rb') as lbpath:
FileNotFoundError: [Errno 2] No such file or directory: 'D:/train-labels-idx1-ubyte'

这是来自教科书的原始代码: https://github.com/rasbt/python-machine-learning-book/blob/master/code/ch12/ch12.ipynb

2 个答案:

答案 0 :(得分:2)

这可能是路径问题。如何通过命令行终端导航到D:文件夹,在那里打开Python解释器并执行

import os
os.listdir()

显示所有文件&amp; D:下的文件夹。然后,您可以检查train-labels-idx1-ubyte是否确实存在以及它是如何拼写的。

答案 1 :(得分:0)

这是解决方案: https://www.reddit.com/r/learnpython/comments/6qc9t1/path_to_existing_file_in_root_folder_not_found_on/

这是错字哈哈。对不起,我只是没有注意到。