python pickle UnicodeDecodeError
Asked Answered
A

2

19

I'm trying to load the mnist character dataset (following the tutorial outlined here: http://neuralnetworksanddeeplearning.com/chap1.html )

when I run the load_data_wrapper function I get the error.

UnicodeDecodeError: 'ascii' codec can't decode byte 0x90 in position 614: ordinal not in range(128)

The code run is:

import numpy as np
import gzip


def load_data():
    f = gzip.open('../data/mnist.pkl.gz', 'rb')
    training_data, validation_data, test_data = pickle.load(f)
    f.close()
    return (training_data, validation_data, test_data)

def load_data_wrapper():
    tr_d, va_d, te_d = load_data()
    training_inputs = [np.reshape(x, (784,1)) for x in tr_d[0]]
    training_results = [vectorized_result(y) for y in tr_d[1]]
    training_data = zip(training_inputs, training_results)
    validation_inputs = [np.reshape(x,(784, 1))for x in va_d[0]]
    validation_data = zip(validation_inputs, va_d[1])
    test_inputs = [np.reshape(x, (784, 1)) for x in te_d[0]]
    test_data = zip(test_inputs, te_d[1])
    return(training_data, validation_data, test_data)

def vectorized_result(j):
    e = np.zeros((10,1))
    e[j] = 1.0
    return e

UPDATE: The problem seems to be that I am trying to unpickle with python 3.6 which was pickled with python 2.x.

Andesite answered 8/11, 2016 at 18:19 Comment(0)
A
43

As stated the main problem turned out to be incompatibility between python 2.x cPickle and python 3.x pickle.

setting the encoding to 'latin-1' seems to work.

training_data, validation_data, test_data = pickle.load(f, encoding='latin1')

Answer here helped a lot: Pickle incompatability of numpy arrays between Python 2 and 3

Andesite answered 8/11, 2016 at 21:9 Comment(0)
N
-3

You can try this:

  import chainer  
  train, test = chainer.datasets.get_mnist(ndim=1)
Nanoid answered 15/9, 2018 at 13:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.