Pickle incompatibility of numpy arrays between Python 2 and 3

前端 未结 7 1645
遥遥无期
遥遥无期 2020-11-28 01:12

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\',         


        
7条回答
  •  执念已碎
    2020-11-28 02:10

    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.

提交回复
热议问题