代码报错
来源:7-5 knn数字识别3

异次元游人
2019-01-24
代码(和视频里一致):
import tensorflow as tf
import numpy as np
import random
from tensorflow.examples.tutorials.mnist import input_data
#load data
mnist=input_data.read_data_sets(‘MNIST_data’,one_hot=True)
#属性设置
trainNum=55000
testNum=10000
trainSize=500
testSize=5
k=4
#data 分解 生成0-trainNum间的随机数,trainSize个,不可重复
trainIndex=np.random.choice(trainNum,trainSize,replace=False)
testIndex=np.random.choice(testNum,testSize,replace=False)
trainData=mnist.train.images[trainIndex]#训练图片
trainLabel=mnist.train.labels[trainIndex]#训练标签
testData=mnist.test.images[testIndex]
testLabel=mnist.test.labels[testIndex]
print(‘trainData.shape=’,trainData.shape)
print(‘trainLabel.shape=’,trainLabel.shape)
报错内容:
TimeoutError Traceback (most recent call last)
~\AppData\Local\conda\conda\envs\tensorflow36\lib\urllib\request.py in do_open(self, http_class, req, **http_conn_args)
1317 h.request(req.get_method(), req.selector, req.data, headers,
-> 1318 encode_chunked=req.has_header(‘Transfer-encoding’))
1319 except OSError as err: # timeout error
~\AppData\Local\conda\conda\envs\tensorflow36\lib\http\client.py in request(self, method, url, body, headers, encode_chunked)
1238 “”“Send a complete request to the server.”""
-> 1239 self._send_request(method, url, body, headers, encode_chunked)
1240
~\AppData\Local\conda\conda\envs\tensorflow36\lib\http\client.py in _send_request(self, method, url, body, headers, encode_chunked)
1284 body = _encode(body, ‘body’)
-> 1285 self.endheaders(body, encode_chunked=encode_chunked)
1286
~\AppData\Local\conda\conda\envs\tensorflow36\lib\http\client.py in endheaders(self, message_body, encode_chunked)
1233 raise CannotSendHeader()
-> 1234 self._send_output(message_body, encode_chunked=encode_chunked)
1235
~\AppData\Local\conda\conda\envs\tensorflow36\lib\http\client.py in _send_output(self, message_body, encode_chunked)
1025 del self._buffer[:]
-> 1026 self.send(msg)
1027
~\AppData\Local\conda\conda\envs\tensorflow36\lib\http\client.py in send(self, data)
963 if self.auto_open:
–> 964 self.connect()
965 else:
~\AppData\Local\conda\conda\envs\tensorflow36\lib\http\client.py in connect(self)
1391
-> 1392 super().connect()
1393
~\AppData\Local\conda\conda\envs\tensorflow36\lib\http\client.py in connect(self)
935 self.sock = self._create_connection(
–> 936 (self.host,self.port), self.timeout, self.source_address)
937 self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
~\AppData\Local\conda\conda\envs\tensorflow36\lib\socket.py in create_connection(address, timeout, source_address)
723 if err is not None:
–> 724 raise err
725 else:
~\AppData\Local\conda\conda\envs\tensorflow36\lib\socket.py in create_connection(address, timeout, source_address)
712 sock.bind(source_address)
–> 713 sock.connect(sa)
714 # Break explicitly a reference cycle
TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
During handling of the above exception, another exception occurred:
URLError Traceback (most recent call last)
in
4 from tensorflow.examples.tutorials.mnist import input_data
5 #load data
----> 6 mnist=input_data.read_data_sets(‘MNIST_data’,one_hot=True)
7 #属性设置
8 trainNum=55000
~\AppData\Local\conda\conda\envs\tensorflow36\lib\site-packages\tensorflow\python\util\deprecation.py in new_func(*args, **kwargs)
304 ‘in a future version’ if date is None else (‘after %s’ % date),
305 instructions)
–> 306 return func(*args, **kwargs)
307 return tf_decorator.make_decorator(
308 func, new_func, ‘deprecated’,
~\AppData\Local\conda\conda\envs\tensorflow36\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py in read_data_sets(train_dir, fake_data, one_hot, dtype, reshape, validation_size, seed, source_url)
258
259 local_file = base.maybe_download(TRAIN_IMAGES, train_dir,
–> 260 source_url + TRAIN_IMAGES)
261 with gfile.Open(local_file, ‘rb’) as f:
262 train_images = extract_images(f)
~\AppData\Local\conda\conda\envs\tensorflow36\lib\site-packages\tensorflow\python\util\deprecation.py in new_func(*args, **kwargs)
304 ‘in a future version’ if date is None else (‘after %s’ % date),
305 instructions)
–> 306 return func(*args, **kwargs)
307 return tf_decorator.make_decorator(
308 func, new_func, ‘deprecated’,
~\AppData\Local\conda\conda\envs\tensorflow36\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\base.py in maybe_download(filename, work_directory, source_url)
250 filepath = os.path.join(work_directory, filename)
251 if not gfile.Exists(filepath):
–> 252 temp_file_name, _ = urlretrieve_with_retry(source_url)
253 gfile.Copy(temp_file_name, filepath)
254 with gfile.GFile(filepath) as f:
~\AppData\Local\conda\conda\envs\tensorflow36\lib\site-packages\tensorflow\python\util\deprecation.py in new_func(*args, **kwargs)
304 ‘in a future version’ if date is None else (‘after %s’ % date),
305 instructions)
–> 306 return func(*args, **kwargs)
307 return tf_decorator.make_decorator(
308 func, new_func, ‘deprecated’,
~\AppData\Local\conda\conda\envs\tensorflow36\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\base.py in wrapped_fn(*args, **kwargs)
203 for delay in delays():
204 try:
–> 205 return fn(*args, **kwargs)
206 except Exception as e: # pylint: disable=broad-except
207 if is_retriable is None:
~\AppData\Local\conda\conda\envs\tensorflow36\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\base.py in urlretrieve_with_retry(url, filename)
231 @_internal_retry(initial_delay=1.0, max_delay=16.0, is_retriable=_is_retriable)
232 def urlretrieve_with_retry(url, filename=None):
–> 233 return urllib.request.urlretrieve(url, filename)
234
235
~\AppData\Local\conda\conda\envs\tensorflow36\lib\urllib\request.py in urlretrieve(url, filename, reporthook, data)
246 url_type, path = splittype(url)
247
–> 248 with contextlib.closing(urlopen(url, data)) as fp:
249 headers = fp.info()
250
~\AppData\Local\conda\conda\envs\tensorflow36\lib\urllib\request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
221 else:
222 opener = _opener
–> 223 return opener.open(url, data, timeout)
224
225 def install_opener(opener):
~\AppData\Local\conda\conda\envs\tensorflow36\lib\urllib\request.py in open(self, fullurl, data, timeout)
524 req = meth(req)
525
–> 526 response = self._open(req, data)
527
528 # post-process response
~\AppData\Local\conda\conda\envs\tensorflow36\lib\urllib\request.py in _open(self, req, data)
542 protocol = req.type
543 result = self._call_chain(self.handle_open, protocol, protocol +
–> 544 ‘_open’, req)
545 if result:
546 return result
~\AppData\Local\conda\conda\envs\tensorflow36\lib\urllib\request.py in _call_chain(self, chain, kind, meth_name, *args)
502 for handler in handlers:
503 func = getattr(handler, meth_name)
–> 504 result = func(*args)
505 if result is not None:
506 return result
~\AppData\Local\conda\conda\envs\tensorflow36\lib\urllib\request.py in https_open(self, req)
1359 def https_open(self, req):
1360 return self.do_open(http.client.HTTPSConnection, req,
-> 1361 context=self._context, check_hostname=self.check_hostname)
1362
1363 https_request = AbstractHTTPHandler.do_request
~\AppData\Local\conda\conda\envs\tensorflow36\lib\urllib\request.py in do_open(self, http_class, req, **http_conn_args)
1318 encode_chunked=req.has_header(‘Transfer-encoding’))
1319 except OSError as err: # timeout error
-> 1320 raise URLError(err)
1321 r = h.getresponse()
1322 except:
URLError: <urlopen error [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。>
另外,还有一些警告,代码已过时,怎么解决?请优先解决报错谢谢!
1回答
-
Jones
2019-02-16
你好,就目前代码看确实看不什么太大问题。可以采用二分法,先定位问题到底出在哪一行代码,不如删减代码,先让他可以跑起来,再一点点排除。
00
相似问题