{"record":{"id":"2ac6c65df234d107","repo":"d2l-ai/d2l-zh","slug":"f-name-data-hub-2ac6c6","errorCode":null,"errorMessage":"f\"{name} 不存在于 {DATA_HUB}\"","messagePattern":"f\"(.+?) 不存在于 (.+?)\"","errorType":"validation","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"d2l/torch.py","lineNumber":374,"sourceCode":"    \"\"\"评估给定数据集上模型的损失\n\n    Defined in :numref:`sec_model_selection`\"\"\"\n    metric = d2l.Accumulator(2)  # 损失的总和,样本数量\n    for X, y in data_iter:\n        out = net(X)\n        y = d2l.reshape(y, out.shape)\n        l = loss(out, y)\n        metric.add(d2l.reduce_sum(l), d2l.size(l))\n    return metric[0] / metric[1]\n\nDATA_HUB = dict()\nDATA_URL = 'http://d2l-data.s3-accelerate.amazonaws.com/'\n\ndef download(name, cache_dir=os.path.join('..', 'data')):\n    \"\"\"下载一个DATA_HUB中的文件，返回本地文件名\n\n    Defined in :numref:`sec_kaggle_house`\"\"\"\n    assert name in DATA_HUB, f\"{name} 不存在于 {DATA_HUB}\"\n    url, sha1_hash = DATA_HUB[name]\n    os.makedirs(cache_dir, exist_ok=True)\n    fname = os.path.join(cache_dir, url.split('/')[-1])\n    if os.path.exists(fname):\n        sha1 = hashlib.sha1()\n        with open(fname, 'rb') as f:\n            while True:\n                data = f.read(1048576)\n                if not data:\n                    break\n                sha1.update(data)\n        if sha1.hexdigest() == sha1_hash:\n            return fname  # 命中缓存\n    print(f'正在从{url}下载{fname}...')\n    r = requests.get(url, stream=True, verify=True)\n    with open(fname, 'wb') as f:\n        f.write(r.content)\n    return fname","sourceCodeStart":356,"sourceCodeEnd":392,"githubUrl":"https://github.com/d2l-ai/d2l-zh/blob/e6b18ccea71451a55fcd861d7b96fddf2587b09a/d2l/torch.py#L356-L392","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"# before\nd2l.download('kaggle_house_prediction')  # AssertionError: ... 不存在于 {...}\n# after\nDATA_HUB = d2l.torch.DATA_HUB\nd2l.download('kaggle_house_train')\n# or register your own\nDATA_HUB['myset'] = (d2l.torch.DATA_URL + 'myset.zip', '<sha1>')\nd2l.download('myset')","handlingStrategy":"type-guard","validationCode":"import d2l.torch as d2l\nif name not in d2l.DATA_HUB:\n    raise KeyError(f'{name} not in DATA_HUB; available: {sorted(d2l.DATA_HUB)}')\nfname = d2l.download(name)","typeGuard":"def is_registered_dataset(name: str, hub=d2l.DATA_HUB) -> bool:\n    return name in hub","tryCatchPattern":null,"preventionTips":["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"],"tags":["d2l","pytorch","assertion","dataset","data-hub","download"],"backgroundTag":null,"analyzedSha":"e6b18ccea71451a55fcd861d7b96fddf2587b09a","analyzedAt":"2026-08-14T20:05:26.414Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}