cPickle.UnpicklingError:无效的加载密钥''.? [英] cPickle.UnpicklingError: invalid load key, ' '.?

查看:1377
本文介绍了cPickle.UnpicklingError:无效的加载密钥''.?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 mnist_data 手写数字识别.现在我尝试使用此代码加载数据.

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

推荐答案

您传递给cPickle.load()的参数必须是.pkl文件. mnist.pkl位于mnist.pkl.gz内部

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

因此,您必须首先打开该.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)

这篇关于cPickle.UnpicklingError:无效的加载密钥''.?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆