下载MNIST数据时出错

时间:2018-08-11 08:19:39

标签: python python-3.x mnist

我正在尝试下载MNIST火车和测试仪。我从网站上下载文件并将其保存在名为samples的文件夹中。解压缩后得到的文件名为train_images。但是在函数调用中,我收到未定义名称训练的错误。

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")`

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

检查您的函数调用。您使用的第一个参数(训练图像)未在代码中定义。

经过测试的代码版本:

List
  1. 在您的情况下,python尝试将 train-image 解释为变量 名称。如果要使用它作为文件名,则应使用引号将其引起来。
  2. 请注意,如果您使用tag3中指定的python3,则请在使用前修复ObjectBinding<Predicate<Log>> binding = new ObjectBinding<Predicate<String>>() { private final Set<String> strings = new HashSet<>(); { sourceList.getSelectionModel().getSelectedItems().addListener(new ListChangeListener<String>() { @Override public void onChanged(ListChangeListener.Change<? extends String> c) { boolean changed = false; // modify set on selection change while (c.next()) { if (c.wasRemoved()) { changed = true; c.getRemoved().stream().map(String::toLowerCase).forEach(strings::remove); } if (c.wasAdded()) { changed = true; c.getAddedSubList().stream().map(String::toLowerCase).forEach(strings::add); } } if (changed) { invalidate(); } } }); } @Override protected Predicate<Log> computeValue() { return log -> strings.contains(log.getSource().toLowerCase()); } }; sourceFilter.bind(binding); 导入。
  3. 您还可以使用浏览器检查http://yann.lecun.com/exdb/mnist/train-image。由于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) 不存在,它返回404。但是,有一些带有培训集的压缩存档。您可以通过浏览器检查正确的URL:http://yann.lecun.com/exdb/mnist

您可以尝试在开发中使用某些IDE(例如:pycharm)。它突出显示了某些提到的问题。

相关问题