microsoft / AI-For-Beginners

12 Weeks, 24 Lessons, AI for All!
https://microsoft.github.io/AI-For-Beginners/
MIT License
33.96k stars 5.64k forks source link

pickle.load(mnist_pickle) returns UnicodeDecodeError: 'ascii' codec can't decode byte 0x90 in position 614: ordinal not in range(128) #241

Open GanGral opened 1 year ago

GanGral commented 1 year ago

lessons\3-NeuralNetworks\03-Perceptron

running MNIST = pickle.load(mnist_pickle)

results in

5 with gzip.open('../03-Perceptron/mnist.pkl.gz', 'rb') as mnist_pickle: 6 ----> 7 MNIST = pickle.load(mnist_pickle) 8

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


Workaround, which creates future problems:

MNIST = pickle.load(mnist_pickle, encoding="latin1") works well, however, the next step to plot the dataset would result in

**TypeError Traceback (most recent call last) C:\Users\IGOR~1.KOR\AppData\Local\Temp/ipykernel_32352/2720090614.py in ----> 1 print(MNIST['Train']['Features'][0][130:180]) 2 print(MNIST['Train']['Labels'][0]) 3 features = MNIST['Train']['Features'].astype(np.float32) / 256.0 4 labels = MNIST['Train']['Labels'] 5 fig = pylab.figure(figsize=(10,5))

TypeError: tuple indices must be integers or slices, not str**

GanGral commented 1 year ago

additional info:

mnist.pkl.gz is not available in the original location, so I downloaded it from https://github.com/mnielsen/neural-networks-and-deep-learning/blob/master/data/mnist.pkl.gz

I assume the data is structured differently in this file.

GanGral commented 1 year ago

I see couple of ways of fixing issue:

print(MNIST[0][0][0][130:180]) print(MNIST[0][1][0]) features = MNIST[0][0].astype(np.float32) / 256.0 labels = MNIST[0][1] fig = pylab.figure(figsize=(10,5)) for i in range(10): ax = fig.add_subplot(1,10,i+1) pylab.imshow(features[i].reshape(28,28)) pylab.show()

cutePanda123 commented 8 months ago

i fixed the 'ascii' code error by specifying the encoding parameter: MNIST = pickle.load(mnist_pickle, encoding='latin1')

link to the commit: https://github.com/cutePanda123/AI-For-Beginners/commit/a73db3c0e3ac5c67c1b4b6aecda5f07c8a53d48d

liuyanhui commented 1 week ago

It works , Thank you very much.