我正在尝试加载链接的 MNIST 数据集here在 Python 3.2 中使用此程序:
import pickle
import gzip
import numpy
with gzip.open('mnist.pkl.gz', 'rb') as f:
l = list(pickle.load(f))
print(l)
不幸的是,它给了我错误:
Traceback (most recent call last):
File "mnist.py", line 7, in <module>
train_set, valid_set, test_set = pickle.load(f)
UnicodeDecodeError: 'ascii' codec can't decode byte 0x90 in position 614: ordinal not in range(128)
然后我尝试在 Python 2.7 中解码 pickled 文件,然后重新编码。所以,我在 Python 2.7 中运行了这个程序:
import pickle
import gzip
import numpy
with gzip.open('mnist.pkl.gz', 'rb') as f:
train_set, valid_set, test_set = pickle.load(f)
# Printing out the three objects reveals that they are
# all pairs containing numpy arrays.
with gzip.open('mnistx.pkl.gz', 'wb') as g:
pickle.dump(
(train_set, valid_set, test_set),
g,
protocol=2) # I also tried protocol 0.
它运行没有错误,所以我在 Python 3.2 中重新运行这个程序:
import pickle
import gzip
import numpy
# note the filename change
with gzip.open('mnistx.pkl.gz', 'rb') as f:
l = list(pickle.load(f))
print(l)
但是,它给了我和以前一样的错误。我该如何让它发挥作用?
这是加载 MNIST 数据集的更好方法。