numpy python 兼容_Python 2和3之间的numpy数组的Pickle不兼容

I am trying to load the MNIST dataset linked here in Python 3.2 using this program:

import pickle

import gzip

import numpy

with gzip.open('mnist.pkl.gz', 'rb') as f:

l = list(pickle.load(f))

print(l)

Unfortunately, it gives me the error:

Traceback (most recent call last):

File "mnist.py", line 7, in

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)

I then tried to decode the pickled file in Python 2.7, and re-encode it. So, I ran this program in 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.

It ran without error, so I reran this program in 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)

However, it gave me the same error as before. How do I get this to work?

解决方案

This seems like some sort of incompatibility. It's trying to load a "binstring" object, which is assumed to be ASCII, while in this case it is binary data. If this is a bug in the Python 3 unpickler, or a "misuse" of the pickler by numpy, I don't know.

Here is something of a workaround, but I don't know how meaningful the data is at this point:

import pickle

import gzip

import numpy

with open('mnist.pkl', 'rb') as f:

u = pickle._Unpickler(f)

u.encoding = 'latin1'

p = u.load()

print(p)

Unpickling it in Python 2 and then repickling it is only going to create the same problem again, so you need to save it in another format.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值