Setu Kumar Basak
Setu Kumar Basak

Reputation: 12022

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

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..

Upvotes: 6

Views: 17820

Answers (3)

Nil
Nil

Reputation: 139

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()

Upvotes: 1

Anirban
Anirban

Reputation: 343

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)

Upvotes: 5

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?

Upvotes: 1

Related Questions