[LAB 14 · 图像问答:视觉条件与问题先验](/course/notebooks/11d9f74871544a77b42024a9fe06b81a.html) # 4. 逐问题读结果,别只看总准确率 · 实现代码 [独立阅读](/course/notebook-blocks/3ce456f62e2a4489be2b14a56d1b568e.html) 按问题继续 RGB+问题输入、question-only对照、同图换问与同问换图 展开完整实现代码 ``` @torch.no_grad() def answer_all(net,images): net.eval() return torch.stack([net(images,torch.full((len(images),),q)).argmax(-1) for q in range(3)],1) normal=answer_all(full,Xte);qo=answer_all(question_only,Xte) zero=answer_all(full,torch.zeros_like(Xte));shuffled=answer_all(full,Xte[torch.roll(torch.arange(len(Xte)),1)]) train_full=answer_all(full,Xtr);train_qo=answer_all(question_only,Xtr) def score(p,y):return {'overall':float(p.eq(y).float().mean()),**{QUESTIONS[q]:float(p[:,q].eq(y[:,q]).float().mean()) for q in range(3)}} metrics={'test_image_question':score(normal,Yte),'test_question_only':score(qo,Yte),'test_zero_pixels':score(zero,Yte),'test_shuffled_pixels':score(shuffled,Yte),'train_image_question':score(train_full,Ytr),'train_question_only':score(train_qo,Ytr)} print(json.dumps(metrics,indent=2)) # Recreate two images with matched nuisance randomness, only left color differs. control_specs=[(2,'red','square'),(2,'blue','square'),(1,'green','circle')] controls_x=torch.tensor(np.stack([draw_scene(*s,np.random.default_rng(444)) for s in control_specs])).permute(0,3,1,2) controls_pred=answer_all(full,controls_x);controls_qo=answer_all(question_only,controls_x) controls=[{'image_condition':list(s),'question':q,'image_question':ANSWERS[int(controls_pred[i,j])],'question_only':ANSWERS[int(controls_qo[i,j])]} for i,s in enumerate(control_specs) for j,q in enumerate(QUESTIONS)] print('same image / changed question and same question / changed image:') for row in controls:print(row) ```