You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用tf.keras.utils.get_file报错:TypeError: 'int'与'NoneType'无法比较

解决TensorFlow 2.10.0中tf.keras.utils.get_file下载数据集的TypeError问题

问题重现

使用TensorFlow 2.10.0在Jupyter Notebook中执行以下代码下载free-spoken-digit-dataset数据集时:

if not data_dir.exists():
    tf.keras.utils.get_file('free-spoken-digit-dataset-master.zip',origin="https://codeload.github.com/Jakobovski/free-spoken-digit-dataset/zip/refs/heads/master",extract=True,cache_dir='.',cache_subdir='data')

或另一版本代码:

tf.keras.utils.get_file(origin="https://github.com/Jakobovski/free-spoken-digit-dataset/archive/v1.0.9.tar.gz",extract=True,cache_dir='.',cache_subdir='data')

均触发错误:

TypeError: '<' not supported between instances of 'int' and 'NoneType'

原因分析

这个错误是TensorFlow 2.10版本中tf.keras.utils.get_file的已知bug:当服务器返回的HTTP响应中Content-Length字段为None时,函数内部的长度比较逻辑会抛出类型错误。

解决方案

方案1:升级TensorFlow版本

将TensorFlow升级到2.11及以上版本,官方已在后续版本修复了该问题。执行以下命令升级:

pip install --upgrade tensorflow>=2.11

方案2:绕过tf.keras.utils.get_file,手动下载解压

如果无法升级TensorFlow,可以用Python的requests库或系统命令直接下载并解压:

使用requests库的示例代码

import requests
import zipfile
import os

data_dir = os.path.join('.', 'data')
os.makedirs(data_dir, exist_ok=True)

# 下载数据集压缩包
url = "https://codeload.github.com/Jakobovski/free-spoken-digit-dataset/zip/refs/heads/master"
zip_path = os.path.join(data_dir, 'free-spoken-digit-dataset-master.zip')

response = requests.get(url, stream=True)
with open(zip_path, 'wb') as f:
    for chunk in response.iter_content(chunk_size=1024):
        f.write(chunk)

# 解压压缩包
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
    zip_ref.extractall(data_dir)

使用系统命令(适用于Linux/macOS)

在Notebook中执行魔法命令:

!mkdir -p data && cd data && wget https://codeload.github.com/Jakobovski/free-spoken-digit-dataset/zip/refs/heads/master -O dataset.zip && unzip dataset.zip

内容的提问来源于stack exchange,提问作者Delun Zhang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 11:52:19