{"record":{"id":"62d856b2bdd92d25","repo":"d2l-ai/d2l-zh","slug":"f-name-data-hub","errorCode":null,"errorMessage":"f\"{name} 不存在于 {DATA_HUB}\"","messagePattern":"f\"(.+?) 不存在于 (.+?)\"","errorType":"validation","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"d2l/mxnet.py","lineNumber":347,"sourceCode":"\ndef evaluate_loss(net, data_iter, loss):\n    \"\"\"评估给定数据集上模型的损失\n\n    Defined in :numref:`sec_model_selection`\"\"\"\n    metric = d2l.Accumulator(2)  # 损失的总和,样本数量\n    for X, y in data_iter:\n        l = loss(net(X), 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":329,"sourceCodeEnd":365,"githubUrl":"https://github.com/d2l-ai/d2l-zh/blob/e6b18ccea71451a55fcd861d7b96fddf2587b09a/d2l/mxnet.py#L329-L365","documentation":"AssertionError from d2l.mxnet.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', 'time_machine', etc.). The message interpolates the entire DATA_HUB contents so you can see exactly which names are registered. It exists to stop a KeyError/HTTP 404 later when a URL is guessed.","triggerScenarios":"Calling d2l.download('kaggle_house_prediction') (unregistered name/typo), calling download on a dataset constant defined in d2l.torch.DATA_HUB but using d2l.mxnet.download (the dicts hold the same keys in this repo, but custom or newer datasets are not present), or calling download before DATA_HUB is extended with your own (url, sha1) tuple.","commonSituations":"Copy-pasting notebook code from a newer D2L edition whose dataset name differs; users who assume any URL string can be passed (download takes a registry key, not a URL); stale d2l version missing a recently added dataset entry.","solutions":["Inspect the printed DATA_HUB in the error message and use the exact registered key (e.g. 'kaggle_house_train')","Check spelling/case of the dataset name against d2l.mxnet.DATA_HUB keys","If you genuinely need a new file, register it first: DATA_HUB['mydata'] = (DATA_URL + 'mydata.csv', '<sha1>') then call download('mydata')","Upgrade the d2l package to the edition matching your notebook"],"exampleFix":"# before\nd2l.download('kaggle_house_prediction')  # AssertionError: ... 不存在于 {...}\n# after\nd2l.download('kaggle_house_train')\n# or register your own\nDATA_HUB['myset'] = (DATA_URL + 'myset.csv', 'da14dd1caee0dd2d91c3a35a4c7f5f6e')\nd2l.download('myset')","handlingStrategy":"type-guard","validationCode":"import d2l.mxnet as d2l\nname = 'kaggle_house_train'\nif name not in d2l.DATA_HUB:\n    raise KeyError(f'{name} not registered; 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":["Treat download() as a registry API: pass keys, never URLs","Print sorted(DATA_HUB) once when unsure of valid names","For custom data, register (url, sha1) before calling download","Keep d2l version aligned with the notebook edition you follow"],"tags":["d2l","mxnet","assertion","dataset","data-hub","download"],"backgroundTag":null,"analyzedSha":"e6b18ccea71451a55fcd861d7b96fddf2587b09a","analyzedAt":"2026-08-14T20:05:26.414Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}