{"record":{"id":"b3c9bda87e44390c","repo":"d2l-ai/d2l-zh","slug":"f-name-data-hub-b3c9bd","errorCode":null,"errorMessage":"f\"{name} 不存在于 {DATA_HUB}\"","messagePattern":"f\"(.+?) 不存在于 (.+?)\"","errorType":"validation","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"d2l/tensorflow.py","lineNumber":363,"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":345,"sourceCodeEnd":381,"githubUrl":"https://github.com/d2l-ai/d2l-zh/blob/e6b18ccea71451a55fcd861d7b96fddf2587b09a/d2l/tensorflow.py#L345-L381","documentation":"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}'.","triggerScenarios":"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.","commonSituations":"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'.","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."],"exampleFix":"# before\nd2l.download('kaggle_house_prediction')  # AssertionError: not in DATA_HUB\n# after\nd2l.DATA_HUB['kaggle_house_train'] = (\n    d2l.DATA_URL + 'kaggle_house_pred_train.csv',\n    '020e2b8f8f8c6f6f6f6f6f6f6f6f6f6f6f6f6f6f')\nd2l.download('kaggle_house_train')","handlingStrategy":"validation","validationCode":"def safe_download(name):\n    if name not in d2l.DATA_HUB:\n        available = ', '.join(sorted(d2l.DATA_HUB))\n        raise KeyError(f'{name!r} not registered. Available: {available}')\n    return d2l.download(name)","typeGuard":"def is_registered(name: str) -> bool:\n    return isinstance(name, str) and name in d2l.DATA_HUB","tryCatchPattern":"try:\n    d2l.download(name)\nexcept AssertionError:\n    raise KeyError(f'{name!r} not in DATA_HUB; register it or pick from {sorted(d2l.DATA_HUB)}')","preventionTips":["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."],"tags":["d2l","tensorflow","download","data-hub","kaggle","assertion"],"backgroundTag":null,"analyzedSha":"e6b18ccea71451a55fcd861d7b96fddf2587b09a","analyzedAt":"2026-08-14T20:05:26.414Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}