[LAB 12 · CLIP延伸:缓存与检索(近似索引选读)](/course/notebooks/921eeaf784284ce19568c6ca7619b55a.html) # 2 · 精确搜索:模型给分,评价定义相关 · 实现代码 [独立阅读](/course/notebook-blocks/e05d4164e12144fea000c0bf4d6b7e2f.html) 并入相关学习路径 同一CLIP缓存、exact/IVF搜索、延迟/扫描量、区域交互基线 展开完整实现代码 ``` X,Q=a['gallery'],a['queries'] assert np.allclose(np.linalg.norm(X,axis=1),1,atol=2e-6) labels=np.array([r['label'] for r in manifest['gallery']]) truth=[r['label'] for r in manifest['queries']] S=Q@X.T exact=np.stack([cr.stable_rank(row,k=3) for row in S]) for i,row in enumerate(exact): print(manifest['queries'][i]['text'],'->',[(manifest['gallery'][j]['id'],round(float(S[i,j]),4)) for j in row]) hit3=np.mean([any(labels[j]==truth[i] for j in row) for i,row in enumerate(exact)]) print('exact semantic Hit@3:',hit3) # 反方向:每张图对 12 条文字排序。每类有两条同义候选。 i2t=np.stack([cr.stable_rank(row,k=1) for row in S.T]) print('image-to-text Hit@1:',np.mean([truth[row[0]]==labels[i] for i,row in enumerate(i2t)])) ```