从Michael Nielsen教程问题加载MNIST数据



按照Michael Nielsen在其在线著作《神经网络与深度学习》中指定的方式加载MNIST数据时,我遇到了一个棘手的问题。

他提供了一组函数,如load_data((和load_data_wrapper((,用于加载他使用的MNIST数据。这就是他所指定的:

"""
"mnist_loader"
A library to load the MNIST image data.  For details of the data
structures that are returned, see the doc strings for ``load_data``
and ``load_data_wrapper``.  In practice, ``load_data_wrapper`` is the
function usually called by our neural network code.
"""
#### Libraries
# Standard library
import _pickle as cPickle
import gzip
# Third-party libraries
import numpy as np
def load_data():
"""Return the MNIST data as a tuple containing the training data,
the validation data, and the test data.
The ``training_data`` is returned as a tuple with two entries.
The first entry contains the actual training images.  This is a
numpy ndarray with 50,000 entries.  Each entry is, in turn, a
numpy ndarray with 784 values, representing the 28 * 28 = 784
pixels in a single MNIST image.
The second entry in the ``training_data`` tuple is a numpy ndarray
containing 50,000 entries.  Those entries are just the digit
values (0...9) for the corresponding images contained in the first
entry of the tuple.
The ``validation_data`` and ``test_data`` are similar, except
each contains only 10,000 images.
This is a nice data format, but for use in neural networks it's
helpful to modify the format of the ``training_data`` a little.
That's done in the wrapper function ``load_data_wrapper()``, see
below.
"""
with gzip.open('./data/mnist.pkl.gz', 'rb') as f:
training_data, validation_data, test_data = cPickle.load(f, encoding='latin1')
return (training_data, validation_data, test_data)
def load_data_wrapper():
"""Return a tuple containing ``(training_data, validation_data,
test_data)``. Based on ``load_data``, but the format is more
convenient for use in our implementation of neural networks.
In particular, ``training_data`` is a list containing 50,000
2-tuples ``(x, y)``.  ``x`` is a 784-dimensional numpy.ndarray
containing the input image.  ``y`` is a 10-dimensional
numpy.ndarray representing the unit vector corresponding to the
correct digit for ``x``.
``validation_data`` and ``test_data`` are lists containing 10,000
2-tuples ``(x, y)``.  In each case, ``x`` is a 784-dimensional
numpy.ndarry containing the input image, and ``y`` is the
corresponding classification, i.e., the digit values (integers)
corresponding to ``x``.
Obviously, this means we're using slightly different formats for
the training data and the validation / test data.  These formats
turn out to be the most convenient for use in our neural network
code."""
tr_d, va_d, te_d = load_data()
training_inputs = [np.reshape(x, (784, 1)) for x in tr_d[0]]
training_results = [vectorized_result(y) for y in tr_d[1]]
training_data = list(zip(training_inputs, training_results))
validation_inputs = [np.reshape(x, (784, 1)) for x in va_d[0]]
validation_data = list(zip(validation_inputs, va_d[1]))
test_inputs = [np.reshape(x, (784, 1)) for x in te_d[0]]
test_data = list(zip(test_inputs, te_d[1]))
return (training_data, validation_data, test_data)
def vectorized_result(j):
"""Return a 10-dimensional unit vector with a 1.0 in the jth
position and zeroes elsewhere.  This is used to convert a digit
(0...9) into a corresponding desired output from the neural
network."""
e = np.zeros((10, 1))
e[j] = 1.0
return e

我所做的只是简单地创建一个名为"的类对象;mnist_loader";将这些函数定义指定为其自变量,即:

class mnist_loader(object):
def load_data():

etc.
etc.
def vectorized_results():

然而,当我按照他在书中指定的方式运行代码时:

training_data, validation_data, test_data = 
mnist_loader.load_data_wrapper()

我收到以下错误消息:

"NameError: name 'load_data' is not defined"

然后,我尝试将他的GitHub分叉到我自己的GitHub,并将其作为ZIP文件下载,然后我简单地获取mnist_loader.py(他为加载数据创建的模块(,并将它插入到我当前的工作目录中,看看它是否有任何不同——然而,它只是给了我samme错误消息。

为了进一步注意,我已经将gzip.open('./data/mnist.pkl.gz', 'rb')参数更改为我自己的工作目录,所以我认为这不是问题所在。

我不知道还能做什么,但我想克服这个小障碍,因为他的书很有趣。

希望你能帮忙。

干杯。

数据加载程序库旨在完全按照提供的方式使用。您不应该将它们的代码更改为类,您应该简单地从提供的模块中调用函数。

import mnist_loader
training_data, validation_data, test_data = mnist_loader.load_data_wrapper()

应该按原样工作。

语法CCD_ 2表示模块CCD_ 4中的调用函数CCD_。

你也可以这样做:

from mnist_loader import load_data_wrapper
training_data, validation_data, test_data = load_data_wrapper()

最新更新