如何处理 TensorFlow 中的错误 "certificate verify failed: certificate has expired"?

How can I handle the error "certificate verify failed: certificate has expired" in TensorFlow?

我刚刚开始使用 CIFAR10 数据集在 Python 中使用 TensorFlow 创建图像分类程序,遵循 this 教程。到目前为止,这是我的代码:

import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
import numpy as np
datasets.cifar10.load_data()

我收到如下错误:

  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 1346, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1257, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1303, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1252, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1012, in _send_output
    self.send(msg)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 952, in send
    self.connect()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1426, in connect
    self.sock = self._context.wrap_socket(self.sock,
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1040, in _create
    self.do_handshake()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1309, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1129) 

此错误是什么意思,我该如何解决?我是 TensorFlow 的初学者,未能找到解决此问题的确切方法。

最近在使用 python 3.7 时遇到了类似的错误,只是像这样关闭了验证:

import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
import numpy as np
import ssl

ssl._create_default_https_context = ssl._create_unverified_context

datasets.cifar10.load_data()

查看此 issue 了解更多信息。您当然可以尝试为您的 Python 版本安装缺少的证书。我可以确认您的代码适用于 Google Colab。所以这肯定是因为缺少一些证书。