d2l-ai/d2l-zh · error · AssertionError
f"{name} 不存在于 {DATA_HUB}"
Error message
f"{name} 不存在于 {DATA_HUB}" What it means
An AssertionError in d2l.tensorflow.download stating that the requested dataset name is not a key in DATA_HUB. DATA_HUB is a module-level dict mapping dataset names to (url, sha1) pairs; download() refuses to fetch anything not registered there because it needs the pinned URL and checksum. The Chinese message reads '{name} does not exist in {DATA_HUB}'.
Source
Thrown at d2l/tensorflow.py:363
def evaluate_loss(net, data_iter, loss):
"""评估给定数据集上模型的损失
Defined in :numref:`sec_model_selection`"""
metric = d2l.Accumulator(2) # 损失的总和,样本数量
for X, y in data_iter:
l = loss(net(X), y)
metric.add(d2l.reduce_sum(l), d2l.size(l))
return metric[0] / metric[1]
DATA_HUB = dict()
DATA_URL = 'http://d2l-data.s3-accelerate.amazonaws.com/'
def download(name, cache_dir=os.path.join('..', 'data')):
"""下载一个DATA_HUB中的文件,返回本地文件名
Defined in :numref:`sec_kaggle_house`"""
assert name in DATA_HUB, f"{name} 不存在于 {DATA_HUB}"
url, sha1_hash = DATA_HUB[name]
os.makedirs(cache_dir, exist_ok=True)
fname = os.path.join(cache_dir, url.split('/')[-1])
if os.path.exists(fname):
sha1 = hashlib.sha1()
with open(fname, 'rb') as f:
while True:
data = f.read(1048576)
if not data:
break
sha1.update(data)
if sha1.hexdigest() == sha1_hash:
return fname # 命中缓存
print(f'正在从{url}下载{fname}...')
r = requests.get(url, stream=True, verify=True)
with open(fname, 'wb') as f:
f.write(r.content)
return fnameView on GitHub (pinned to e6b18ccea7)
Solutions
- Register the dataset before downloading: DATA_HUB['my_dataset'] = (DATA_URL + 'my_file.csv', '<sha1>'); download('my_dataset').
- Use one of the pre-registered names at the bottom of the module, e.g. d2l.download('kaggle_house_train') or 'kaggle_house_test'.
- For real Kaggle competitions, download manually from kaggle.com and place files in the ../data cache_dir; download() only serves the D2L mirror.
- Compute the SHA1 with: python -c "import hashlib;print(hashlib.sha1(open('file','rb').read()).hexdigest())" when registering a custom entry.
Example fix
# before
d2l.download('kaggle_house_prediction') # AssertionError: not in DATA_HUB
# after
d2l.DATA_HUB['kaggle_house_train'] = (
d2l.DATA_URL + 'kaggle_house_pred_train.csv',
'020e2b8f8f8c6f6f6f6f6f6f6f6f6f6f6f6f6f6f')
d2l.download('kaggle_house_train') Defensive patterns
Strategy: validation
Validate before calling
def safe_download(name):
if name not in d2l.DATA_HUB:
available = ', '.join(sorted(d2l.DATA_HUB))
raise KeyError(f'{name!r} not registered. Available: {available}')
return d2l.download(name) Type guard
def is_registered(name: str) -> bool:
return isinstance(name, str) and name in d2l.DATA_HUB Try / catch
try:
d2l.download(name)
except AssertionError:
raise KeyError(f'{name!r} not in DATA_HUB; register it or pick from {sorted(d2l.DATA_HUB)}') Prevention
- List registered datasets first: print(sorted(d2l.DATA_HUB)).
- Register custom entries with DATA_HUB[name] = (url, sha1) before calling download.
- For real Kaggle data, use the Kaggle CLI and place files in the ../data cache dir.
When it happens
Trigger: Calling d2l.download('my_dataset') or download_extract/download_all with a name never registered; requesting 'kaggle_house_train' from a fresh interpreter where the DATA_HUB[...] assignment lines at module bottom were not executed (e.g. partially imported module); typos in the name string.
Common situations: Users assuming d2l can download arbitrary Kaggle files (the book's sec_kaggle_house registers only the mirrored kaggle_house_train/kaggle_house_test CSVs on d2l-data.s3); copying download() into a notebook but not the DATA_HUB registration lines; name case or underscore mismatches like 'kaggle-house-train'.
Related errors
- f"{name} 不存在于 {DATA_HUB}"
- f"{name} 不存在于 {DATA_HUB}"
- f"{name} 不存在于 {DATA_HUB}"
- 只有zip/tar文件可以被解压缩
- 只有zip/tar文件可以被解压缩
AI-assisted analysis of d2l-ai/d2l-zh@e6b18ccea7 (2026-08-14).
Data as JSON: /api/errors/b3c9bda87e44390c.
Report an issue: GitHub.