在tensorflow教程中,运行下面的代码下载mnist数据集:
#coding:utf-8
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets('/home/zhangkf/tf/tf5/mnist',one_hot=True)
但实际运行时根本无法通过网络下载,网上可以查到一些解决方法,但说得不是很清楚,这里整理一下,大小神请无视。
解决方案就是手工下载数据,然后直接导入使用。
(tf13) zhangkf@Ubuntu2:~/tf/tf5$ python mnist.py
Traceback (most recent call last):
File "mnist.py", line 4, in
mnist=input_data.read_data_sets('/home/zhangkf/tf/tf5/mnist',one_hot=True)
File "/home/zhangkf/.conda/envs/tf13/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py", line 235, in read_data_sets
SOURCE_URL + TRAIN_IMAGES)
File "/home/zhangkf/.conda/envs/tf13/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py", line 208, in maybe_download
temp_file_name, _ = urlretrieve_with_retry(source_url)
File "/home/zhangkf/.conda/envs/tf13/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py", line 165, in wrapped_fn
return fn(*args, **kwargs)
File "/home/zhangkf/.conda/envs/tf13/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py", line 190, in urlretrieve_with_retry
return urllib.request.urlretrieve(url, filename)
File "/home/zhangkf/.conda/envs/tf13/lib/python2.7/urllib.py", line 98, in urlretrieve
return opener.retrieve(url, filename, reporthook, data)
File "/home/zhangkf/.conda/envs/tf13/lib/python2.7/urllib.py", line 245, in retrieve
fp = self.open(url, data)
File "/home/zhangkf/.conda/envs/tf13/lib/python2.7/urllib.py", line 213, in open
return getattr(self, name)(url)
File "/home/zhangkf/.conda/envs/tf13/lib/python2.7/urllib.py", line 443, in open_https
h.endheaders(data)
File "/home/zhangkf/.conda/envs/tf13/lib/python2.7/httplib.py", line 1038, in endheaders
self._send_output(message_body)
File "/home/zhangkf/.conda/envs/tf13/lib/python2.7/httplib.py", line 882, in _send_output
self.send(msg)
File "/home/zhangkf/.conda/envs/tf13/lib/python2.7/httplib.py", line 844, in send
self.connect()
File "/home/zhangkf/.conda/envs/tf13/lib/python2.7/httplib.py", line 1263, in connect
server_hostname=server_hostname)
File "/home/zhangkf/.conda/envs/tf13/lib/python2.7/ssl.py", line 363, in wrap_socket
_context=self)
File "/home/zhangkf/.conda/envs/tf13/lib/python2.7/ssl.py", line 611, in __init__
self.do_handshake()
File "/home/zhangkf/.conda/envs/tf13/lib/python2.7/ssl.py", line 840, in do_handshake
self._sslobj.do_handshake()
IOError: [Errno socket error] EOF occurred in violation of protocol (_ssl.c:661)
一、手工下载地址: http://yann.lecun.com/exdb/mnist/
4个文件,注意下载后不需要解压,不需要解压,不需要解压。
二、导入数据
如果把上述下载的文件放在与运行的.py文件同一个目录下,那么就这样:
#coding:utf-8
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets('./',one_hot=True)
运行成功显示如下,算是解决了上述的问题。
(tf13) zhangkf@Ubuntu2:~/tf/tf5$ python mnist.py
Extracting ./train-images-idx3-ubyte.gz
Extracting ./train-labels-idx1-ubyte.gz
Extracting ./t10k-images-idx3-ubyte.gz
Extracting ./t10k-labels-idx1-ubyte.gz