{"version":2,"relationshipURLBase":"https://codingai-lec03.pages.dev/course/notebook-blocks/cb2b6dec25874e6a9654f27808e15529.html","relationshipResolution":"Resolve relative relationship URLs against relationshipURLBase; complete records and absolute URLs are in blocks.","title":"准备：本实验的固定材料 · 实现代码","url":"https://codingai-lec03.pages.dev/course/notebook-blocks/cb2b6dec25874e6a9654f27808e15529.html","markdown":"https://codingai-lec03.pages.dev/course/notebook-blocks/cb2b6dec25874e6a9654f27808e15529.md","pages":[{"id":"reading-opening","title":"准备：本实验的固定材料 · 实现代码","unit":"准备：本实验的固定材料 · 实现代码","unitId":"notebook-blocks/cb2b6dec25874e6a9654f27808e15529","chapter":"course","number":1}],"aliases":[{"oldId":"b-bb4e20021c57c89032355300","targetId":"b-bb4e20021c57c89032355300","page":"reading-opening","operation":"move","beforeSHA256":"f107bd31e942a5cf605f3183c89731efbb5349a4bf8c063087e5d29bebde50b5","afterSHA256":"f107bd31e942a5cf605f3183c89731efbb5349a4bf8c063087e5d29bebde50b5","reason":"Same source body in authored semantic page"},{"oldId":"b-3959e2a871014c7b30355646","targetId":"b-3959e2a871014c7b30355646","page":"reading-opening","operation":"move","beforeSHA256":"5921c0537ec13d0d8bedb07c2d73686e3eec06ccda769319a707c32881e44682","afterSHA256":"5921c0537ec13d0d8bedb07c2d73686e3eec06ccda769319a707c32881e44682","reason":"Same source body in authored semantic page"},{"oldId":"b-6e4bce9a1dfba62441fe2dc4","targetId":"b-6e4bce9a1dfba62441fe2dc4","page":"reading-opening","operation":"move","beforeSHA256":"e91f24e4e4ca40e2099a0d81d30db6568f4bf2074b499eeee6e3959617bd97ac","afterSHA256":"e91f24e4e4ca40e2099a0d81d30db6568f4bf2074b499eeee6e3959617bd97ac","reason":"Same source body in authored semantic page"},{"oldId":"b-d9329186f0872fce1c353188","targetId":"b-d9329186f0872fce1c353188","page":"reading-opening","operation":"move","beforeSHA256":"5f1e2e7a35a6afaca427b0b03cfec448b4aac4324b16108f14671986a2d98b38","afterSHA256":"5f1e2e7a35a6afaca427b0b03cfec448b4aac4324b16108f14671986a2d98b38","reason":"Same source body in authored semantic page"},{"oldId":"b-be6a3e95271d7992245bbd46","targetId":"b-be6a3e95271d7992245bbd46","page":"reading-opening","operation":"move","beforeSHA256":"07984659f22edf1a9718250b393647b37fc539bd8865faf43ca5beac4b98fb65","afterSHA256":"07984659f22edf1a9718250b393647b37fc539bd8865faf43ca5beac4b98fb65","reason":"Same source body in authored semantic page"}],"blocks":[{"id":"b-bb4e20021c57c89032355300","type":"p","scope":"document","url":"https://codingai-lec03.pages.dev/course/notebook-blocks/cb2b6dec25874e6a9654f27808e15529.html#b-bb4e20021c57c89032355300","content":"[LAB 02 · 参数迁移：冻结、局部与全量微调](/course/notebooks/4bd450d54870432db91c80a4df563b8e.html)","sha256":"f107bd31e942a5cf605f3183c89731efbb5349a4bf8c063087e5d29bebde50b5","chapter":{"id":"course","title":"准备：本实验的固定材料 · 实现代码"},"unit":{"id":"notebook-blocks/cb2b6dec25874e6a9654f27808e15529","title":"准备：本实验的固定材料 · 实现代码"},"page":{"id":"reading-opening","title":"准备：本实验的固定材料 · 实现代码","number":1},"sectionTitle":"准备：本实验的固定材料 · 实现代码","currentQuestion":"准备：本实验的固定材料 · 实现代码","sourceSlides":[],"knowledgeBlock":null,"formulas":[],"previousBlock":null,"nextBlock":"b-3959e2a871014c7b30355646","prerequisites":[],"relatedBlocks":[]},{"id":"b-3959e2a871014c7b30355646","type":"h1","scope":"document","url":"https://codingai-lec03.pages.dev/course/notebook-blocks/cb2b6dec25874e6a9654f27808e15529.html#b-3959e2a871014c7b30355646","content":"# 准备：本实验的固定材料 · 实现代码","sha256":"5921c0537ec13d0d8bedb07c2d73686e3eec06ccda769319a707c32881e44682","chapter":{"id":"course","title":"准备：本实验的固定材料 · 实现代码"},"unit":{"id":"notebook-blocks/cb2b6dec25874e6a9654f27808e15529","title":"准备：本实验的固定材料 · 实现代码"},"page":{"id":"reading-opening","title":"准备：本实验的固定材料 · 实现代码","number":1},"sectionTitle":"准备：本实验的固定材料 · 实现代码","currentQuestion":"准备：本实验的固定材料 · 实现代码","sourceSlides":[],"knowledgeBlock":null,"formulas":[],"previousBlock":"b-bb4e20021c57c89032355300","nextBlock":"b-6e4bce9a1dfba62441fe2dc4","prerequisites":[{"id":"b-bb4e20021c57c89032355300","url":"#b-bb4e20021c57c89032355300","role":"problem-context"}],"relatedBlocks":[]},{"id":"b-6e4bce9a1dfba62441fe2dc4","type":"p","scope":"document","url":"https://codingai-lec03.pages.dev/course/notebook-blocks/cb2b6dec25874e6a9654f27808e15529.html#b-6e4bce9a1dfba62441fe2dc4","content":"按问题继续","sha256":"e91f24e4e4ca40e2099a0d81d30db6568f4bf2074b499eeee6e3959617bd97ac","chapter":{"id":"course","title":"准备：本实验的固定材料 · 实现代码"},"unit":{"id":"notebook-blocks/cb2b6dec25874e6a9654f27808e15529","title":"准备：本实验的固定材料 · 实现代码"},"page":{"id":"reading-opening","title":"准备：本实验的固定材料 · 实现代码","number":1},"sectionTitle":"准备：本实验的固定材料 · 实现代码","currentQuestion":"准备：本实验的固定材料 · 实现代码","sourceSlides":[],"knowledgeBlock":null,"formulas":[],"previousBlock":"b-3959e2a871014c7b30355646","nextBlock":"b-d9329186f0872fce1c353188","prerequisites":[{"id":"b-3959e2a871014c7b30355646","url":"#b-3959e2a871014c7b30355646","role":"section-title"},{"id":"b-bb4e20021c57c89032355300","url":"#b-bb4e20021c57c89032355300","role":"problem-context"}],"relatedBlocks":[]},{"id":"b-d9329186f0872fce1c353188","type":"p","scope":"document","url":"https://codingai-lec03.pages.dev/course/notebook-blocks/cb2b6dec25874e6a9654f27808e15529.html#b-d9329186f0872fce1c353188","content":"同源参数的冻结/局部/全量/scratch与BN模式辨析","sha256":"5f1e2e7a35a6afaca427b0b03cfec448b4aac4324b16108f14671986a2d98b38","chapter":{"id":"course","title":"准备：本实验的固定材料 · 实现代码"},"unit":{"id":"notebook-blocks/cb2b6dec25874e6a9654f27808e15529","title":"准备：本实验的固定材料 · 实现代码"},"page":{"id":"reading-opening","title":"准备：本实验的固定材料 · 实现代码","number":1},"sectionTitle":"准备：本实验的固定材料 · 实现代码","currentQuestion":"准备：本实验的固定材料 · 实现代码","sourceSlides":[],"knowledgeBlock":null,"formulas":[],"previousBlock":"b-6e4bce9a1dfba62441fe2dc4","nextBlock":"b-be6a3e95271d7992245bbd46","prerequisites":[{"id":"b-3959e2a871014c7b30355646","url":"#b-3959e2a871014c7b30355646","role":"section-title"},{"id":"b-bb4e20021c57c89032355300","url":"#b-bb4e20021c57c89032355300","role":"problem-context"}],"relatedBlocks":[]},{"id":"b-be6a3e95271d7992245bbd46","type":"pre","scope":"document","url":"https://codingai-lec03.pages.dev/course/notebook-blocks/cb2b6dec25874e6a9654f27808e15529.html#b-be6a3e95271d7992245bbd46","content":"```\nMODEL_PATH=ROOT/'results/cnn-plain-state.pt'\nREPORT_PATH=ROOT/'results/lab02-results.json'\nDATA=ROOT/'data'\nfiles={\n 'train-images-idx3-ubyte.gz':'440fcabf73cc546fa21475e81ea370265605f56be210a4024d2ca8f203523609',\n 'train-labels-idx1-ubyte.gz':'3552534a0a558bbed6aed32b30c495cca23d567ec52cac8be1a0730e8010255c'}\nmissing=[str(p) for p in [MODEL_PATH,REPORT_PATH,*[DATA/n for n in files]] if not p.is_file()]\nif missing:\n    raise RuntimeError('独立材料包不完整，请重新解压 data/ 与 results/：'+', '.join(missing))\nreport=json.loads(REPORT_PATH.read_text())\nprotocol=report['protocol']\nassert protocol['split_seed']==20260908 and protocol['train_n']==12000, '本练习要求 LAB 02 默认源训练划分；改变划分后需要重新检查源/目标重叠。'\nraw={}\nfor name,sha in files.items():\n    raw[name]=(DATA/name).read_bytes()\n    assert hashlib.sha256(raw[name]).hexdigest()==sha, 'MNIST 文件不匹配：'+name\nimages=np.frombuffer(gzip.decompress(raw['train-images-idx3-ubyte.gz']),dtype=np.uint8,offset=16).reshape(60000,1,28,28)\ndigit_labels=np.frombuffer(gzip.decompress(raw['train-labels-idx1-ubyte.gz']),dtype=np.uint8,offset=8)\npartition=np.random.default_rng(20260908).permutation(60000)\nexpected_partition_sha=hashlib.sha256(partition.tobytes()).hexdigest()\nexpected_train_sha=hashlib.sha256(partition[:12000].tobytes()).hexdigest()\nexpected_val_sha=hashlib.sha256(partition[50000:].tobytes()).hexdigest()\nassert protocol['train_ids_sha256']==expected_train_sha\nassert protocol['partition_sha256']==expected_partition_sha and protocol['validation_n']==10000\nstate_bytes=MODEL_PATH.read_bytes()\nmodel_sha256=hashlib.sha256(state_bytes).hexdigest()\nCOURSE_MODEL_SHA256='ba8b738cc67d4ec3cd83d75a5dbe8a70ec548377335ba0abaaa7c23cd14eeba3'\nidentity=report.get('checkpoint_provenance',{}).get('cnn-plain-state.pt')\nif identity is not None:\n    assert identity['sha256']==model_sha256, '模型与 LAB 02 报告不是同一轮保存的文件；请重新运行 LAB 02 第4节和保存结果单元。'\n    source=identity['source_protocol']\n    assert source['split_seed']==20260908 and source['train_n']==12000 and source['validation_n']==10000\n    assert source['partition_sha256']==expected_partition_sha\n    assert source['train_ids_sha256']==expected_train_sha and source['validation_ids_sha256']==expected_val_sha\n    source_files={item['file']:item['sha256'] for item in source['files']}\n    assert all(source_files.get(name)==sha for name,sha in files.items()), '源训练数据身份与本练习不一致。'\n    identity_basis='LAB02 training-time checkpoint hash and source protocol'\nelif model_sha256==COURSE_MODEL_SHA256:\n    # 已追溯的课程旧模型兼容旧报告；此分支仅接受这一份确切文件。\n    identity_basis='known course checkpoint SHA-256 with verified default source split'\nelse:\n    raise RuntimeError('当前模型缺少与报告绑定的训练来源。请重新运行更新后的 LAB 02 第4节及保存结果单元；不要给旧文件补写哈希。')\ntarget_ids=partition[12000:12640]\nassert not set(target_ids)&set(partition[:12000])\nassert not set(target_ids)&set(partition[50000:])\ntarget_buffer=io.BytesIO()\nnp.savez_compressed(target_buffer,images=images[target_ids],digit_labels=digit_labels[target_ids],\n official_train_ids=target_ids,split=np.array([0]*256+[1]*128+[2]*256,dtype=np.uint8))\nprovenance={'dataset':'MNIST official training set','source_task':'ten-class digit classification',\n 'source_checkpoint_sha256':model_sha256,'source_identity_basis':identity_basis,'source_origin':'bundled original LAB02 protected checkpoint; cross-project independent package',\n 'target_task':'digit >= 5 (binary)','source_split_seed':20260908,\n 'source_train_permutation_positions':[0,12000],'source_validation_permutation_positions':[50000,60000],\n 'target_permutation_positions':[12000,12640],'target_split_sizes':{'train':256,'validation':128,'heldout':256},\n 'target_ids_sha256':hashlib.sha256(target_ids.tobytes()).hexdigest(),\n 'limitations':['single small digit task','source/target sample indices disjoint; writer groups unavailable','not a leaf experiment']}\nasset_bytes={'cnn-plain-state.pt':state_bytes,'target.npz':target_buffer.getvalue(),\n 'target-provenance.json':json.dumps(provenance).encode()}\nprint('使用包内已核验模型；目标样本与默认源训练/验证索引不重叠。')\n\n```","sha256":"07984659f22edf1a9718250b393647b37fc539bd8865faf43ca5beac4b98fb65","chapter":{"id":"course","title":"准备：本实验的固定材料 · 实现代码"},"unit":{"id":"notebook-blocks/cb2b6dec25874e6a9654f27808e15529","title":"准备：本实验的固定材料 · 实现代码"},"page":{"id":"reading-opening","title":"准备：本实验的固定材料 · 实现代码","number":1},"sectionTitle":"准备：本实验的固定材料 · 实现代码","currentQuestion":"准备：本实验的固定材料 · 实现代码","sourceSlides":[],"knowledgeBlock":null,"formulas":[],"previousBlock":"b-d9329186f0872fce1c353188","nextBlock":null,"prerequisites":[{"id":"b-3959e2a871014c7b30355646","url":"#b-3959e2a871014c7b30355646","role":"section-title"},{"id":"b-bb4e20021c57c89032355300","url":"#b-bb4e20021c57c89032355300","role":"problem-context"}],"relatedBlocks":[]}]}
