下载 MNIST 数据时出错

Error while downloading MNIST data

我正在尝试下载 MNIST 训练和测试集。我从网站下载文件并将其保存在名为 samples.After 的文件夹中,解压后我得到一个名为 train_images 的文件。但是在函数调用中我得到错误名称 train is not defined.

from __future__ import print_function
import gzip
import os
import urllib
import numpy
SOURCE_URL = 'http://yann.lecun.com/exdb/mnist/'


def maybe_download(filename, work_directory):

    if not os.path.exists(work_directory):
        os.mkdir(work_directory)
    filepath = os.path.join(work_directory, filename)
    if not os.path.exists(filepath):
        filepath, _ = urllib.urlretrieve(SOURCE_URL + filename, filepath)
        statinfo = os.stat(filepath)
        print('Succesfully downloaded', filename, statinfo.st_size, 'bytes.')
    print(filepath)
    return filepath
#function call
maybe_download(train-images,"./samples")`

您在函数调用中写了 train-images 而不是 train_images。变量名称不能包含破折号 – train-images 被解析为 train - images.

检查你的函数调用。您使用的第一个参数 (train-images) 未在您的代码中定义。

您的代码的测试版本:

from __future__ import print_function
import os
from urllib import request

SOURCE_URL = 'http://yann.lecun.com/exdb/mnist/'


def maybe_download(filename, work_directory):

    if not os.path.exists(work_directory):
        os.mkdir(work_directory)
    filepath = os.path.join(work_directory, filename)
    if not os.path.exists(filepath):
        filepath, _ = request.urlretrieve(SOURCE_URL + filename, filepath)
        statinfo = os.stat(filepath)
        print('Succesfully downloaded', filename, statinfo.st_size, 'bytes.')
    print(filepath)
    return filepath

#function call
maybe_download("train-images-idx3-ubyte.gz","./samples")

exit(0)
  1. 在你的情况下 python 尝试将 train-image 解释为变量 姓名。如果您想将其用作文件名,则应将其用引号引起来。
  2. 请注意,如果您使用标签中指定的 python3,请在使用前修复 urlencode 导入。
  3. 您也可以查看 http://yann.lecun.com/exdb/mnist/train-image with your browser. It returns 404 as train-image does not exists. But there are several zipped archives with training sets. You can check correct URLs via browser: http://yann.lecun.com/exdb/mnist

您可以尝试使用一些 IDE(如:pycharm)进行开发。它突出了一些提到的问题。