Python用theano深度学习mnist的基础程序
Python deep learning with theano the basic program of mnist
我是深度学习的新手,我只是在这方面做我的初级步骤。我有一台 MacBook Pro 2017 型号,我正在尝试 运行 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/logistic_sgd.py
首先您可以在 http://www.iro.umontreal.ca/~lisa/deep/data/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.
- 行
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.open 语句中使用元组中的数据集!
我是深度学习的新手,我只是在这方面做我的初级步骤。我有一台 MacBook Pro 2017 型号,我正在尝试 运行 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/logistic_sgd.py
首先您可以在 http://www.iro.umontreal.ca/~lisa/deep/data/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.
- 行
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.open 语句中使用元组中的数据集!