Python深入学习theano mnist的基本程序

时间:2017-12-06 11:21:11

标签: python pycharm theano conda mnist

我是深度学习的新手,我正在做我的宝贝步骤。我有一个MacBook Pro 2017型号,我正在尝试使用 Logistic回归运行 MNIST数据集。当我运行官方深度学习网站中提到的示例代码时,我收到以下错误..

<i>Downloading data from http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz
Traceback (most recent call last):
File "/Users/abi/PycharmProjects/LogisticRgr_test/logist_test.py", line 475, in <module>
sgd_optimization_mnist()
File "/Users/abi/PycharmProjects/LogisticRgr_test/logist_test.py", line 277, in sgd_optimization_mnist
datasets = load_data(dataset)
File "/Users/abi/PycharmProjects/LogisticRgr_test/logist_test.py", line 205, in load_data
urllib.request.urlretrieve(origin, dataset)
File "/Users/abi/miniconda2/lib/python2.7/urllib.py", line 98, in urlretrieve
return opener.retrieve(url, filename, reporthook, data)
File "/Users/abi/miniconda2/lib/python2.7/urllib.py", line 249, in retrieve
tfp = open(filename, 'wb')
IOError: [Errno 2] No such file or directory: '/Users/abi/PycharmProjects/LogisticRgr_test/../data/mnist.pkl.gz'

Process finished with exit code 1</i>

1 个答案:

答案 0 :(得分:0)

如文件logistic_sgd.py http://deeplearning.net/tutorial/code/logistic_sgd.py

中所述

首先,您可以在http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz上手动下载此文件 添加文件&#39; mnist.pkl.gz&#39;到当前目录,因为该函数首先检查,如果mnist文件在数据目录中。如果你这样做,那么检查是否安装numpy并且一切正常。

def load_data(dataset):
''' Loads the dataset

:type dataset: string
:param dataset: the path to the dataset (here MNIST)
'''

#############
# LOAD DATA #
#############

# Download the MNIST dataset if it is not present
data_dir, data_file = os.path.split(dataset)
if data_dir == "" and not os.path.isfile(dataset):
    # Check if dataset is in the data directory.
    new_path = os.path.join(
        os.path.split(__file__)[0],
        "..",
        "data",
        dataset
    )
    if os.path.isfile(new_path) or data_file == 'mnist.pkl.gz':
        dataset = new_path

if (not os.path.isfile(dataset)) and data_file == 'mnist.pkl.gz':
    from six.moves import urllib
    origin = (
        'http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz'
    )
    print('Downloading data from %s' % origin)
    urllib.request.urlretrieve(origin, dataset)

print('... loading data')

# Load the dataset
with gzip.open(dataset, 'rb') as f:
    try:
        train_set, valid_set, test_set = pickle.load(f, encoding='latin1')
    except:
        train_set, valid_set, test_set = pickle.load(f)
# train_set, valid_set, test_set format: tuple(input, target)
# input is a numpy.ndarray of 2 dimensions (a matrix)
# where each row corresponds to an example. target is a
# numpy.ndarray of 1 dimension (vector) that has the same length as
# the number of rows in the input. It should give the target
# to the example with the same index in the input.
  1. 与之相符 urllib.request.urlretrieve(origin, dataset)对我来说也不起作用,但你只需下载文件,我就做了一些改动
  2. from urllib import request
    
    def get(url):
        with request.urlopen(url) as r:
           return r.read()
    
    def download(url, file=None):
        if not file:
            file = url.split('/')[-1]
        with open(file, 'wb') as f:
            f.write(get(url))
    

    并调用下载功能

    url = "http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz"
    download(url)
    

    完成这些更改后, load_data 函数可以正常使用

    我做了一些调查:

    替换此声明:

    urllib.request.urlretrieve(origin, dataset)
    

    使用以下声明:

    dataset, header = urllib.request.urlretrieve(origin, 'mnist.pkl.gz')
    

    你从urlretrieve获得了一个元组,并且你需要在gzip.open语句中使用来自元组的数据集!

相关问题