d2l-ai/d2l-zh · error · AssertionError
f"{name} 不存在于 {DATA_HUB}"
Error message
f"{name} 不存在于 {DATA_HUB}" What it means
AssertionError from d2l.torch.download: the requested dataset name must be a key in the module-level DATA_HUB dict (populated at import with entries like 'kaggle_house_train', 'kaggle_house_test', 'fra_mu', etc.). The message interpolates the whole registry so the valid names are visible. It exists to fail fast instead of guessing a URL and hitting a 404.
Source
Thrown at d2l/torch.py:374
"""评估给定数据集上模型的损失
Defined in :numref:`sec_model_selection`"""
metric = d2l.Accumulator(2) # 损失的总和,样本数量
for X, y in data_iter:
out = net(X)
y = d2l.reshape(y, out.shape)
l = loss(out, 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
- Read the DATA_HUB dump in the assertion message and pass the exact key, e.g. d2l.download('kaggle_house_train')
- Fix casing/spelling of the dataset name against d2l.torch.DATA_HUB
- Register your own entry first: DATA_HUB['mydata'] = (DATA_URL + 'mydata.csv', sha1); then d2l.download('mydata')
- pip install -U d2l to pick up newer dataset registrations
Example fix
# before
d2l.download('kaggle_house_prediction') # AssertionError: ... 不存在于 {...}
# after
DATA_HUB = d2l.torch.DATA_HUB
d2l.download('kaggle_house_train')
# or register your own
DATA_HUB['myset'] = (d2l.torch.DATA_URL + 'myset.zip', '<sha1>')
d2l.download('myset') Defensive patterns
Strategy: type-guard
Validate before calling
import d2l.torch as d2l
if name not in d2l.DATA_HUB:
raise KeyError(f'{name} not in DATA_HUB; available: {sorted(d2l.DATA_HUB)}')
fname = d2l.download(name) Type guard
def is_registered_dataset(name: str, hub=d2l.DATA_HUB) -> bool:
return name in hub Prevention
- Pass registry keys, not URLs, to download
- Dump sorted(DATA_HUB) when uncertain about names
- Register custom (url, sha1) entries before downloading
- Match d2l package version to your notebook edition
When it happens
Trigger: Calling d2l.download('kaggle_house_prediction') (typo/unregistered); assuming download accepts a URL (it takes a registry key); calling download for a dataset constant that exists in a different d2l edition or was added after your installed version; calling before extending DATA_HUB with your own (url, sha1_hash) tuple.
Common situations: Copy-pasting from a newer D2L notebook whose dataset key differs from the installed d2l release; using d2l.torch.download with a name registered only in d2l.mxnet.DATA_HUB (keys largely overlap but custom additions do not); stale pip install of d2l.
Related errors
- f"{name} 不存在于 {DATA_HUB}"
- 只有zip/tar文件可以被解压缩
- len(centers) == len(contexts) == len(negatives)
- f"{name} 不存在于 {DATA_HUB}"
- f"{name} 不存在于 {DATA_HUB}"
AI-assisted analysis of d2l-ai/d2l-zh@e6b18ccea7 (2026-08-14).
Data as JSON: /api/errors/2ac6c65df234d107.
Report an issue: GitHub.