the alternative to from keras.datasets import mnist

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



the alternative to from keras.datasets import mnist



I have been experimenting with a Keras example, which needs to import MNIST data


from keras.datasets import mnist
import numpy as np
(x_train, _), (x_test, _) = mnist.load_data()



It generates error messages such as Exception: URL fetch failure on https://s3.amazonaws.com/img-datasets/mnist.pkl.gz: None -- [Errno 110] Connection timed out


Exception: URL fetch failure on https://s3.amazonaws.com/img-datasets/mnist.pkl.gz: None -- [Errno 110] Connection timed out



It should be related to the network environment I am using. My question is that, are there any function or code that can let me directly import mnist data set that has been manually downloaded. Thanks.



This is the modified approach


import sys
import pickle
import gzip
f = gzip.open('/data/mnist.pkl.gz', 'rb')
if sys.version_info < (3,):
data = pickle.load(f)
else:
data = pickle.load(f, encoding='bytes')
f.close()
import numpy as np
(x_train, _), (x_test, _) = data



Then I get the following error message


Traceback (most recent call last):
File "test.py", line 45, in <module>
(x_train, _), (x_test, _) = data
ValueError: too many values to unpack (expected 2)




2 Answers
2



Well, the keras.datasets.mnist file is really short. You can manually simulate the same action, that is:


keras.datasets.mnist



.


import gzip
f = gzip.open('mnist.pkl.gz', 'rb')
if sys.version_info < (3,):
data = cPickle.load(f)
else:
data = cPickle.load(f, encoding='bytes')
f.close()
(x_train, _), (x_test, _) = data





Hi sygi, thanks for the suggestion. However, I got error message as shown in the updated post. The only thing being different with yours is that I use pickle. Looks like it did not give me error during loading the data.
– user785099
Nov 19 '16 at 20:13





I have checked and it works on my system, with both pickle and cPickle and both python 2 and 3. Are you sure you have the same file (md5 b39289ebd4f8755817b1352c8488b486)?
– sygi
Nov 19 '16 at 20:27





It works, do not know why it had error message previously. Thanks a lot.
– user785099
Nov 20 '16 at 5:31



You do not need additional code for that but can tell load_data to load a local version in the first place:


load_data


~/.keras/datasets/


load_data(path='mnist.npz')






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered