Python深度学习与Theano的基本计划



我是深度学习的新手,我只是在做宝贝。我有一个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>

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

首先,您可以在http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist/mnist.pkl.gz上手动下载此文件将文件" mnist.pkl.gz"添加到当前目录中,因为如果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)也对我不起作用,但是您只需要下载文件,所以我进行了一些更改

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中必须使用的元组中的数据集。

最新更新