cPickle.UnpicklingError: invalid load key, ' '.?

邮差的信 提交于 2019-12-12 10:55:34

问题


I am trying to use the mnist_data for hand written digit recognition.Now i tried this code to load the data.

import cPickle
import numpy as np


def load_data():
    f = open('G:/thesis paper/data sets/mnist.pkl.gz', 'rb')
    training_data, validation_data, test_data = cPickle.load(f)
    f.close()
    return (training_data, validation_data, test_data)


def load_data_nn():
    training_data, validation_data, test_data = load_data()
    inputs = [np.reshape(x, (784, 1)) for x in training_data[0]]
    results = [vectorized_result(y) for y in training_data[1]]
    training_data = zip(inputs, results)
    test_inputs = [np.reshape(x, (784, 1)) for x in test_data[0]]
    return (training_data, test_inputs, test_data[1])


def vectorized_result(j):
    e = np.zeros((10, 1))
    e[j] = 1.0
    return e


if __name__ == '__main__':
    tr_data,test_inp,test_data=load_data_nn()

But i am getting this error.

   File "D:/NeuralNet/mnist_loader.py", line 42, in load_data
     training_data, validation_data, test_data = cPickle.load(f) cPickle.UnpicklingError: invalid load key, ''.

I couldn't understand what the error is trying to say and how to remove this error..Thanks in advance..


回答1:


The argument you've passed to cPickle.load() has to be a .pkl file. mnist.pkl is provided inside of mnist.pkl.gz

So, you have to open that .gz first. Try this:

import gzip
f = gzip.open('mnist.pkl.gz', 'rb')
train_set, valid_set, test_set = cPickle.load(f)



回答2:


The first solution to all pickle problems is use "pickle" instead of "cPickle" until you've debugged everything. You'll get much better error messages and can debug better.

It looks like you've gzipped your pickle. You'll need to unzip before loading.

Did your own software produce the pickle, and are you sure that the object to be unpickled is a tuple?




回答3:


This worked for me:

f = gzip.open('../data/mnist.pkl.gz', 'rb')
training_data, validation_data, test_data = cPickle.load(f,encoding='latin1')
f.close()


来源:https://stackoverflow.com/questions/31775514/cpickle-unpicklingerror-invalid-load-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!