{"version":2,"relationshipURLBase":"https://codingai-lec03.pages.dev/course/notebook-blocks/e56644fbd8a5464084c3e83da669726c.html","relationshipResolution":"Resolve relative relationship URLs against relationshipURLBase; complete records and absolute URLs are in blocks.","title":"1 · 没有类别标签，词典到底学了什么？ · 实现代码","url":"https://codingai-lec03.pages.dev/course/notebook-blocks/e56644fbd8a5464084c3e83da669726c.html","markdown":"https://codingai-lec03.pages.dev/course/notebook-blocks/e56644fbd8a5464084c3e83da669726c.md","pages":[{"id":"reading-opening","title":"1 · 没有类别标签，词典到底学了什么？ · 实现代码","unit":"1 · 没有类别标签，词典到底学了什么？ · 实现代码","unitId":"notebook-blocks/e56644fbd8a5464084c3e83da669726c","chapter":"course","number":1}],"aliases":[{"oldId":"b-8150350513294e3e75d8a189","targetId":"b-8150350513294e3e75d8a189","page":"reading-opening","operation":"move","beforeSHA256":"b3bb3626e5e17c0995867f42bedbcb72020e0decf77093d796851d5f10c78af6","afterSHA256":"b3bb3626e5e17c0995867f42bedbcb72020e0decf77093d796851d5f10c78af6","reason":"Same source body in authored semantic page"},{"oldId":"b-d98c880fec59dd2e8893ac45","targetId":"b-d98c880fec59dd2e8893ac45","page":"reading-opening","operation":"move","beforeSHA256":"f596349706498127dfaf236b67b32876085aea53c44e7e0f29f6d435d3b0a59a","afterSHA256":"f596349706498127dfaf236b67b32876085aea53c44e7e0f29f6d435d3b0a59a","reason":"Same source body in authored semantic page"},{"oldId":"b-a84c92c67a18031edf48e742","targetId":"b-a84c92c67a18031edf48e742","page":"reading-opening","operation":"move","beforeSHA256":"e91f24e4e4ca40e2099a0d81d30db6568f4bf2074b499eeee6e3959617bd97ac","afterSHA256":"e91f24e4e4ca40e2099a0d81d30db6568f4bf2074b499eeee6e3959617bd97ac","reason":"Same source body in authored semantic page"},{"oldId":"b-ccea055d9a7540b9980f07d7","targetId":"b-ccea055d9a7540b9980f07d7","page":"reading-opening","operation":"move","beforeSHA256":"74f0ef9a0e12c18b5c762e1998a58610fd064887211de86861e624d8f17a1e93","afterSHA256":"74f0ef9a0e12c18b5c762e1998a58610fd064887211de86861e624d8f17a1e93","reason":"Same source body in authored semantic page"},{"oldId":"b-0e430e1b09ac1bc737dfd2e9","targetId":"b-0e430e1b09ac1bc737dfd2e9","page":"reading-opening","operation":"move","beforeSHA256":"27c38f4dc9ef22b94d1816133be947b45d9e20692856c41f56df4d76419bc2ec","afterSHA256":"27c38f4dc9ef22b94d1816133be947b45d9e20692856c41f56df4d76419bc2ec","reason":"Same source body in authored semantic page"}],"blocks":[{"id":"b-8150350513294e3e75d8a189","type":"p","scope":"document","url":"https://codingai-lec03.pages.dev/course/notebook-blocks/e56644fbd8a5464084c3e83da669726c.html#b-8150350513294e3e75d8a189","content":"[LAB 05 · 像素 patch 词典：拟合与重建](/course/notebooks/de39b998077f44cca4b8407085547766.html)","sha256":"b3bb3626e5e17c0995867f42bedbcb72020e0decf77093d796851d5f10c78af6","chapter":{"id":"course","title":"1 · 没有类别标签，词典到底学了什么？ · 实现代码"},"unit":{"id":"notebook-blocks/e56644fbd8a5464084c3e83da669726c","title":"1 · 没有类别标签，词典到底学了什么？ · 实现代码"},"page":{"id":"reading-opening","title":"1 · 没有类别标签，词典到底学了什么？ · 实现代码","number":1},"sectionTitle":"1 · 没有类别标签，词典到底学了什么？ · 实现代码","currentQuestion":"1 · 没有类别标签，词典到底学了什么？ · 实现代码","sourceSlides":[],"knowledgeBlock":null,"formulas":[],"previousBlock":null,"nextBlock":"b-d98c880fec59dd2e8893ac45","prerequisites":[],"relatedBlocks":[]},{"id":"b-d98c880fec59dd2e8893ac45","type":"h1","scope":"document","url":"https://codingai-lec03.pages.dev/course/notebook-blocks/e56644fbd8a5464084c3e83da669726c.html#b-d98c880fec59dd2e8893ac45","content":"# 1 · 没有类别标签，词典到底学了什么？ · 实现代码","sha256":"f596349706498127dfaf236b67b32876085aea53c44e7e0f29f6d435d3b0a59a","chapter":{"id":"course","title":"1 · 没有类别标签，词典到底学了什么？ · 实现代码"},"unit":{"id":"notebook-blocks/e56644fbd8a5464084c3e83da669726c","title":"1 · 没有类别标签，词典到底学了什么？ · 实现代码"},"page":{"id":"reading-opening","title":"1 · 没有类别标签，词典到底学了什么？ · 实现代码","number":1},"sectionTitle":"1 · 没有类别标签，词典到底学了什么？ · 实现代码","currentQuestion":"1 · 没有类别标签，词典到底学了什么？ · 实现代码","sourceSlides":[],"knowledgeBlock":null,"formulas":[],"previousBlock":"b-8150350513294e3e75d8a189","nextBlock":"b-a84c92c67a18031edf48e742","prerequisites":[{"id":"b-8150350513294e3e75d8a189","url":"#b-8150350513294e3e75d8a189","role":"problem-context"}],"relatedBlocks":[]},{"id":"b-a84c92c67a18031edf48e742","type":"p","scope":"document","url":"https://codingai-lec03.pages.dev/course/notebook-blocks/e56644fbd8a5464084c3e83da669726c.html#b-a84c92c67a18031edf48e742","content":"按问题继续","sha256":"e91f24e4e4ca40e2099a0d81d30db6568f4bf2074b499eeee6e3959617bd97ac","chapter":{"id":"course","title":"1 · 没有类别标签，词典到底学了什么？ · 实现代码"},"unit":{"id":"notebook-blocks/e56644fbd8a5464084c3e83da669726c","title":"1 · 没有类别标签，词典到底学了什么？ · 实现代码"},"page":{"id":"reading-opening","title":"1 · 没有类别标签，词典到底学了什么？ · 实现代码","number":1},"sectionTitle":"1 · 没有类别标签，词典到底学了什么？ · 实现代码","currentQuestion":"1 · 没有类别标签，词典到底学了什么？ · 实现代码","sourceSlides":[],"knowledgeBlock":null,"formulas":[],"previousBlock":"b-d98c880fec59dd2e8893ac45","nextBlock":"b-ccea055d9a7540b9980f07d7","prerequisites":[{"id":"b-d98c880fec59dd2e8893ac45","url":"#b-d98c880fec59dd2e8893ac45","role":"section-title"},{"id":"b-8150350513294e3e75d8a189","url":"#b-8150350513294e3e75d8a189","role":"problem-context"}],"relatedBlocks":[]},{"id":"b-ccea055d9a7540b9980f07d7","type":"p","scope":"document","url":"https://codingai-lec03.pages.dev/course/notebook-blocks/e56644fbd8a5464084c3e83da669726c.html#b-ccea055d9a7540b9980f07d7","content":"真实MNIST patch的Lloyd拟合、词典中心、保留误差与历史输出","sha256":"74f0ef9a0e12c18b5c762e1998a58610fd064887211de86861e624d8f17a1e93","chapter":{"id":"course","title":"1 · 没有类别标签，词典到底学了什么？ · 实现代码"},"unit":{"id":"notebook-blocks/e56644fbd8a5464084c3e83da669726c","title":"1 · 没有类别标签，词典到底学了什么？ · 实现代码"},"page":{"id":"reading-opening","title":"1 · 没有类别标签，词典到底学了什么？ · 实现代码","number":1},"sectionTitle":"1 · 没有类别标签，词典到底学了什么？ · 实现代码","currentQuestion":"1 · 没有类别标签，词典到底学了什么？ · 实现代码","sourceSlides":[],"knowledgeBlock":null,"formulas":[],"previousBlock":"b-a84c92c67a18031edf48e742","nextBlock":"b-0e430e1b09ac1bc737dfd2e9","prerequisites":[{"id":"b-d98c880fec59dd2e8893ac45","url":"#b-d98c880fec59dd2e8893ac45","role":"section-title"},{"id":"b-8150350513294e3e75d8a189","url":"#b-8150350513294e3e75d8a189","role":"problem-context"}],"relatedBlocks":[]},{"id":"b-0e430e1b09ac1bc737dfd2e9","type":"pre","scope":"document","url":"https://codingai-lec03.pages.dev/course/notebook-blocks/e56644fbd8a5464084c3e83da669726c.html#b-0e430e1b09ac1bc737dfd2e9","content":"```\n# Actual Lloyd updates on image patches. Labels are deliberately absent from fit().\nN_WORDS=16\nPATCH=7\npatches=x.reshape(640,1,4,PATCH,4,PATCH).transpose(0,2,4,1,3,5).reshape(640,16,PATCH*PATCH)\nfit_patches=patches[train].reshape(-1,PATCH*PATCH)\nfit_patches=fit_patches[fit_patches.mean(1)>.01].astype(np.float64)\ndef fit_lloyd_unlabeled(a,k,seed=20260918,max_iter=80):\n    rng=np.random.default_rng(seed)\n    centers=a[rng.choice(len(a),k,replace=False)].copy()\n    trace=[]\n    for iteration in range(max_iter):\n        distances=((a[:,None,:]-centers[None,:,:])**2).sum(2)\n        assignments=distances.argmin(1)\n        new=np.stack([a[assignments==j].mean(0) if np.any(assignments==j) else centers[j] for j in range(k)])\n        new_distances=((a[:,None,:]-new[None,:,:])**2).sum(2)\n        sse=float(new_distances.min(1).sum())\n        trace.append(sse)\n        if np.allclose(new,centers,rtol=0,atol=1e-10):\n            centers=new; break\n        centers=new\n    assert np.all(np.diff(trace)<=1e-7)\n    return centers,trace\ncenters,trace=fit_lloyd_unlabeled(fit_patches,N_WORDS)\nfrozen_centers=centers.copy()\ntest_patches=patches[heldout].reshape(-1,49)\ntest_dist=((test_patches[:,None,:]-frozen_centers[None,:,:])**2).sum(2)\ntest_assignment=test_dist.argmin(1)\nassert np.array_equal(centers,frozen_centers)\nkmeans_result={'identity':'L-A true MNIST pixel-patch Lloyd fit','labels_used_in_fit':False,\n 'patch_shape':[7,7],'n_words':N_WORDS,'train_images':len(train),'train_nonblank_patches':len(fit_patches),\n 'fit_sample_ids':sample_ids[train].tolist(),'evaluation_sample_ids':sample_ids[heldout].tolist(),\n 'train_sse_trace':trace,'heldout_mean_squared_pixel_error':float(test_dist.min(1).mean()/49),\n 'warning':'train excludes nearblank patches; heldout covers all patches, so these objectives are not identically weighted'}\nnp.savez_compressed(OUT/'kmeans-centers.npz',centers=centers)\n(OUT/'kmeans-results.json').write_text(json.dumps(kmeans_result,indent=2))\nfig=plt.figure(figsize=(12,4.4),layout='constrained')\nouter=fig.add_gridspec(1,2,width_ratios=[1,1.3],wspace=.12)\ngrid=outer[0].subgridspec(int(np.ceil(N_WORDS/4)),4,wspace=.06,hspace=.12)\nfor j,c in enumerate(centers):\n    ax=fig.add_subplot(grid[j//4,j%4]);ax.imshow(c.reshape(7,7),cmap='gray',vmin=0,vmax=1);ax.set_title(f'word {j}',fontsize=8);ax.axis('off')\nax=fig.add_subplot(outer[1]);ax.plot(np.arange(1,len(trace)+1),trace,color=BLUE,marker='.',ms=4)\nax.set(xlabel='Lloyd iteration',ylabel='Training sum of squared distances',title='Real image patches; no digit labels')\nsavefig('02-unlabeled-fit.png')\nprint('Fitted nonblank patches:',len(fit_patches),'iterations:',len(trace))\nprint('Held-out pixel reconstruction MSE:',kmeans_result['heldout_mean_squared_pixel_error'])\n```","sha256":"27c38f4dc9ef22b94d1816133be947b45d9e20692856c41f56df4d76419bc2ec","chapter":{"id":"course","title":"1 · 没有类别标签，词典到底学了什么？ · 实现代码"},"unit":{"id":"notebook-blocks/e56644fbd8a5464084c3e83da669726c","title":"1 · 没有类别标签，词典到底学了什么？ · 实现代码"},"page":{"id":"reading-opening","title":"1 · 没有类别标签，词典到底学了什么？ · 实现代码","number":1},"sectionTitle":"1 · 没有类别标签，词典到底学了什么？ · 实现代码","currentQuestion":"1 · 没有类别标签，词典到底学了什么？ · 实现代码","sourceSlides":[],"knowledgeBlock":null,"formulas":[],"previousBlock":"b-ccea055d9a7540b9980f07d7","nextBlock":null,"prerequisites":[{"id":"b-d98c880fec59dd2e8893ac45","url":"#b-d98c880fec59dd2e8893ac45","role":"section-title"},{"id":"b-8150350513294e3e75d8a189","url":"#b-8150350513294e3e75d8a189","role":"problem-context"}],"relatedBlocks":[]}]}
