cPickle.UnpicklingError: invalid load key, ' '.?
Asked Answered
C

3

6

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

Claybourne answered 2/8, 2015 at 19:3 Comment(0)
A
5

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)
Audition answered 7/8, 2015 at 17:34 Comment(0)
I
1

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?

Invaginate answered 2/8, 2015 at 19:7 Comment(0)
S
1

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()
Such answered 15/12, 2017 at 15:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.