---------------------------------------------------------------------------

1、进行数据读取,加载数据时报错。

2、具体报错信息如下:

ConnectionResetError                      Traceback (most recent call last)
File C:\ProgramData\anaconda3\Lib\urllib\request.py:1348, in AbstractHTTPHandler.do_open(self, http_class, req, **http_conn_args)
   1347 try:
-> 1348     h.request(req.get_method(), req.selector, req.data, headers,
   1349               encode_chunked=req.has_header('Transfer-encoding'))
   1350 except OSError as err: # timeout error

File C:\ProgramData\anaconda3\Lib\http\client.py:1294, in HTTPConnection.request(self, method, url, body, headers, encode_chunked)
   1293 """Send a complete request to the server."""
-> 1294 self._send_request(method, url, body, headers, encode_chunked)

File C:\ProgramData\anaconda3\Lib\http\client.py:1340, in HTTPConnection._send_request(self, method, url, body, headers, encode_chunked)
   1339     body = _encode(body, 'body')
-> 1340 self.endheaders(body, encode_chunked=encode_chunked)

File C:\ProgramData\anaconda3\Lib\http\client.py:1289, in HTTPConnection.endheaders(self, message_body, encode_chunked)
   1288     raise CannotSendHeader()
-> 1289 self._send_output(message_body, encode_chunked=encode_chunked)

File C:\ProgramData\anaconda3\Lib\http\client.py:1048, in HTTPConnection._send_output(self, message_body, encode_chunked)
   1047 del self._buffer[:]
-> 1048 self.send(msg)
   1050 if message_body is not None:
   1051 
   1052     # create a consistent interface to message_body

File C:\ProgramData\anaconda3\Lib\http\client.py:986, in HTTPConnection.send(self, data)
    985 if self.auto_open:
--> 986     self.connect()
    987 else:

File C:\ProgramData\anaconda3\Lib\http\client.py:1466, in HTTPSConnection.connect(self)
   1464     server_hostname = self.host
-> 1466 self.sock = self._context.wrap_socket(self.sock,
   1467                                       server_hostname=server_hostname)

File C:\ProgramData\anaconda3\Lib\ssl.py:517, in SSLContext.wrap_socket(self, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, session)
    511 def wrap_socket(self, sock, server_side=False,
    512                 do_handshake_on_connect=True,
    513                 suppress_ragged_eofs=True,
    514                 server_hostname=None, session=None):
    515     # SSLSocket class handles server_hostname encoding before it calls
    516     # ctx._wrap_socket()
--> 517     return self.sslsocket_class._create(
    518         sock=sock,
    519         server_side=server_side,
    520         do_handshake_on_connect=do_handshake_on_connect,
    521         suppress_ragged_eofs=suppress_ragged_eofs,
    522         server_hostname=server_hostname,
    523         context=self,
    524         session=session
    525     )

File C:\ProgramData\anaconda3\Lib\ssl.py:1108, in SSLSocket._create(cls, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, context, session)
   1107             raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets")
-> 1108         self.do_handshake()
   1109 except (OSError, ValueError):

File C:\ProgramData\anaconda3\Lib\ssl.py:1383, in SSLSocket.do_handshake(self, block)
   1382         self.settimeout(None)
-> 1383     self._sslobj.do_handshake()
   1384 finally:

ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。

During handling of the above exception, another exception occurred:

URLError                                  Traceback (most recent call last)
File ~\AppData\Roaming\Python\Python311\site-packages\keras\src\utils\file_utils.py:311, in get_file(fname, origin, untar, md5_hash, file_hash, cache_subdir, hash_algorithm, extract, archive_format, cache_dir, force_download)
    310 try:
--> 311     urlretrieve(origin, download_target, DLProgbar())
    312 except urllib.error.HTTPError as e:

File C:\ProgramData\anaconda3\Lib\urllib\request.py:241, in urlretrieve(url, filename, reporthook, data)
    239 url_type, path = _splittype(url)
--> 241 with contextlib.closing(urlopen(url, data)) as fp:
    242     headers = fp.info()

File C:\ProgramData\anaconda3\Lib\urllib\request.py:216, in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    215     opener = _opener
--> 216 return opener.open(url, data, timeout)

File C:\ProgramData\anaconda3\Lib\urllib\request.py:519, in OpenerDirector.open(self, fullurl, data, timeout)
    518 sys.audit('urllib.Request', req.full_url, req.data, req.headers, req.get_method())
--> 519 response = self._open(req, data)
    521 # post-process response

File C:\ProgramData\anaconda3\Lib\urllib\request.py:536, in OpenerDirector._open(self, req, data)
    535 protocol = req.type
--> 536 result = self._call_chain(self.handle_open, protocol, protocol +
    537                           '_open', req)
    538 if result:

File C:\ProgramData\anaconda3\Lib\urllib\request.py:496, in OpenerDirector._call_chain(self, chain, kind, meth_name, *args)
    495 func = getattr(handler, meth_name)
--> 496 result = func(*args)
    497 if result is not None:

File C:\ProgramData\anaconda3\Lib\urllib\request.py:1391, in HTTPSHandler.https_open(self, req)
   1390 def https_open(self, req):
-> 1391     return self.do_open(http.client.HTTPSConnection, req,
   1392         context=self._context, check_hostname=self._check_hostname)

File C:\ProgramData\anaconda3\Lib\urllib\request.py:1351, in AbstractHTTPHandler.do_open(self, http_class, req, **http_conn_args)
   1350 except OSError as err: # timeout error
-> 1351     raise URLError(err)
   1352 r = h.getresponse()

URLError: <urlopen error [WinError 10054] 远程主机强迫关闭了一个现有的连接。>

During handling of the above exception, another exception occurred:

Exception                                 Traceback (most recent call last)
Cell In[15], line 1
----> 1 (train_images,train_label),(test_images,test_labels)=mnist.load_data()

File ~\AppData\Roaming\Python\Python311\site-packages\keras\src\datasets\mnist.py:60, in load_data(path)
     11 """Loads the MNIST dataset.
     12 
     13 This is a dataset of 60,000 28x28 grayscale images of the 10 digits,
   (...)
     55     https://creativecommons.org/licenses/by-sa/3.0/)
     56 """
     57 origin_folder = (
     58     "https://storage.googleapis.com/tensorflow/tf-keras-datasets/"
     59 )
---> 60 path = get_file(
     61     fname=path,
     62     origin=origin_folder + "mnist.npz",
     63     file_hash=(  # noqa: E501
     64         "731c5ac602752760c8e48fbffcf8c3b850d9dc2a2aedcf2cc48468fc17b673d1"
     65     ),
     66 )
     67 with np.load(path, allow_pickle=True) as f:
     68     x_train, y_train = f["x_train"], f["y_train"]

File ~\AppData\Roaming\Python\Python311\site-packages\keras\src\utils\file_utils.py:315, in get_file(fname, origin, untar, md5_hash, file_hash, cache_subdir, hash_algorithm, extract, archive_format, cache_dir, force_download)
    313         raise Exception(error_msg.format(origin, e.code, e.msg))
    314     except urllib.error.URLError as e:
--> 315         raise Exception(error_msg.format(origin, e.errno, e.reason))
    316 except (Exception, KeyboardInterrupt):
    317     if os.path.exists(download_target):

Exception: URL fetch failure on https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz: None -- [WinError 10054] 远程主机强迫关闭了一个现有的连接。
 

3、通过查询得知,代码在尝试从互联网上下载 MNIST 数据集时,网络连接被远程服务器(Google 的服务器)强制中断了。

4、重新再次刷新了4次,数据集下载正常,代码运行正常。

Logo

加入社区!打开量化的大门,首批课程上线啦!

更多推荐