[LAB 05 · 像素 patch 词典:拟合与重建](/course/notebooks/de39b998077f44cca4b8407085547766.html) # 1 · 准备与固定输入 · 实现代码 [独立阅读](/course/notebook-blocks/d9b6b1782dd5404895a80b24f6a87f64.html) 按问题继续 真实MNIST patch的Lloyd拟合、词典中心、保留误差与历史输出 展开完整实现代码 ``` # 当前项目中的独立材料:已存在则复用,缺失才从本讲原站准备。 from pathlib import Path import io, zipfile, urllib.request _base = Path.cwd() _folder = 'lab10-patch' _sentinel = 'assets/target.npz' _choices = [_base/_folder, _base/'lecture03-labs'/_folder, _base] _ready = next((p for p in _choices if (p/_sentinel).is_file()), None) if _ready is None: _url = 'https://codingai-lec03.pages.dev/course/materials/' + _folder + '.zip' _payload = urllib.request.urlopen(_url, timeout=60).read() with zipfile.ZipFile(io.BytesIO(_payload)) as _archive: _names = [i.filename for i in _archive.infolist() if not i.is_dir()] _prefix = _folder + '/' _prefixed = all(n.startswith(_prefix) for n in _names) _destination = _base if _prefixed else _base/_folder for _item in _archive.infolist(): _target = (_destination/_item.filename).resolve() if not _target.is_relative_to(_destination.resolve()): raise ValueError('材料包中有越界路径,停止解压。') if _item.is_dir(): _target.mkdir(parents=True, exist_ok=True) elif not _target.exists(): _target.parent.mkdir(parents=True, exist_ok=True) _target.write_bytes(_archive.read(_item)) _ready = next((p for p in _choices if (p/_sentinel).is_file()), None) assert _ready is not None, '请将本讲完整材料包解压到当前项目的 ' + _folder + '/;保留文件结构。' LAB_ROOT = _ready print('材料目录已就绪:', LAB_ROOT) import json, hashlib, platform, sys from pathlib import Path import numpy as np import matplotlib.pyplot as plt ROOT=LAB_ROOT if not (ROOT/'assets').is_dir() and (ROOT/'lab10-patch'/'assets').is_dir():ROOT=ROOT/'lab10-patch' assert (ROOT/'assets').is_dir(), '请解压本实验完整包,并在该目录或其父目录运行。' OUT=ROOT/'runs/current'; OUT.mkdir(parents=True,exist_ok=True) FIG=OUT/'figures'; FIG.mkdir(exist_ok=True) plt.rcParams.update({'figure.dpi':120,'font.size':10,'axes.spines.top':False,'axes.spines.right':False}) BLUE,GOLD,GREEN,PURPLE='#002676','#E6A500','#00856A','#69458D' def sha_file(path):return hashlib.sha256(Path(path).read_bytes()).hexdigest() def savefig(name): if not plt.gcf().get_constrained_layout():plt.tight_layout() plt.savefig(FIG/name,dpi=140,bbox_inches='tight');plt.show() provenance=json.loads((ROOT/'assets/provenance.json').read_text()) assert sha_file(ROOT/'assets/target.npz')==provenance['target_asset_sha256'] with np.load(ROOT/'assets/target.npz',allow_pickle=False) as z: images=z['images'];sample_ids=z['official_train_ids'];split=z['split'] # digit_labels are in the source archive but deliberately never read here. assert len(set(sample_ids))==640 train=np.flatnonzero(split==0);heldout=np.flatnonzero(split==2) assert not set(sample_ids[train])&set(sample_ids[heldout]) partition=np.random.default_rng(20260908).permutation(60000) assert np.array_equal(sample_ids,partition[12000:12640]) x=images.astype(np.float32)/255 shifted=np.zeros_like(x);shifted[:,:,:,2:]=x[:,:,:,:-2];x=shifted assert x.shape==(640,1,28,28) print('无标签 fit 输入:',len(train),'张;保留:',len(heldout),'张。没有导入 torch、加载 CNN 或读取类别标签。') ```