{"version":2,"relationshipURLBase":"https://codingai-lec03.pages.dev/course/notebook-blocks/7f07825ae8a84326b6a7a768b26c7caf.html","relationshipResolution":"Resolve relative relationship URLs against relationshipURLBase; complete records and absolute URLs are in blocks.","title":"3. 模型、损失与公平比较范围 · 实现代码","url":"https://codingai-lec03.pages.dev/course/notebook-blocks/7f07825ae8a84326b6a7a768b26c7caf.html","markdown":"https://codingai-lec03.pages.dev/course/notebook-blocks/7f07825ae8a84326b6a7a768b26c7caf.md","pages":[{"id":"reading-opening","title":"3. 模型、损失与公平比较范围 · 实现代码","unit":"3. 模型、损失与公平比较范围 · 实现代码","unitId":"notebook-blocks/7f07825ae8a84326b6a7a768b26c7caf","chapter":"course","number":1}],"aliases":[{"oldId":"b-d48ec20ddf5edd6656297500","targetId":"b-d48ec20ddf5edd6656297500","page":"reading-opening","operation":"move","beforeSHA256":"926fc2429c1a5c837765e5152b3b61fda31c10537785a439d49dec46e82a8536","afterSHA256":"926fc2429c1a5c837765e5152b3b61fda31c10537785a439d49dec46e82a8536","reason":"Same source body in authored semantic page"},{"oldId":"b-fd148406324182699fcb8da0","targetId":"b-fd148406324182699fcb8da0","page":"reading-opening","operation":"move","beforeSHA256":"6322203b303efdb226b2518cfd1cb30534250145590cd8921a38d36fe71eecc8","afterSHA256":"6322203b303efdb226b2518cfd1cb30534250145590cd8921a38d36fe71eecc8","reason":"Same source body in authored semantic page"},{"oldId":"b-7a04f65d6772d1d028ea2771","targetId":"b-7a04f65d6772d1d028ea2771","page":"reading-opening","operation":"move","beforeSHA256":"e91f24e4e4ca40e2099a0d81d30db6568f4bf2074b499eeee6e3959617bd97ac","afterSHA256":"e91f24e4e4ca40e2099a0d81d30db6568f4bf2074b499eeee6e3959617bd97ac","reason":"Same source body in authored semantic page"},{"oldId":"b-4518bd60ee2c9a359c49050f","targetId":"b-4518bd60ee2c9a359c49050f","page":"reading-opening","operation":"move","beforeSHA256":"5790b0afb52508f0174b1bc581f7847035a51c5c2a46e5c821bcd9d5d61bcbb0","afterSHA256":"5790b0afb52508f0174b1bc581f7847035a51c5c2a46e5c821bcd9d5d61bcbb0","reason":"Same source body in authored semantic page"},{"oldId":"b-16bc0c175346c9a01f05db8b","targetId":"b-16bc0c175346c9a01f05db8b","page":"reading-opening","operation":"move","beforeSHA256":"701155870a1e8d1c9076048a018c3eac278a5c5ce5d1530da54c7367d1727177","afterSHA256":"701155870a1e8d1c9076048a018c3eac278a5c5ce5d1530da54c7367d1727177","reason":"Same source body in authored semantic page"}],"blocks":[{"id":"b-d48ec20ddf5edd6656297500","type":"p","scope":"document","url":"https://codingai-lec03.pages.dev/course/notebook-blocks/7f07825ae8a84326b6a7a768b26c7caf.html#b-d48ec20ddf5edd6656297500","content":"[LAB 14 · 图像问答：视觉条件与问题先验](/course/notebooks/11d9f74871544a77b42024a9fe06b81a.html)","sha256":"926fc2429c1a5c837765e5152b3b61fda31c10537785a439d49dec46e82a8536","chapter":{"id":"course","title":"3. 模型、损失与公平比较范围 · 实现代码"},"unit":{"id":"notebook-blocks/7f07825ae8a84326b6a7a768b26c7caf","title":"3. 模型、损失与公平比较范围 · 实现代码"},"page":{"id":"reading-opening","title":"3. 模型、损失与公平比较范围 · 实现代码","number":1},"sectionTitle":"3. 模型、损失与公平比较范围 · 实现代码","currentQuestion":"3. 模型、损失与公平比较范围 · 实现代码","sourceSlides":[],"knowledgeBlock":null,"formulas":[],"previousBlock":null,"nextBlock":"b-fd148406324182699fcb8da0","prerequisites":[],"relatedBlocks":[]},{"id":"b-fd148406324182699fcb8da0","type":"h1","scope":"document","url":"https://codingai-lec03.pages.dev/course/notebook-blocks/7f07825ae8a84326b6a7a768b26c7caf.html#b-fd148406324182699fcb8da0","content":"# 3. 模型、损失与公平比较范围 · 实现代码","sha256":"6322203b303efdb226b2518cfd1cb30534250145590cd8921a38d36fe71eecc8","chapter":{"id":"course","title":"3. 模型、损失与公平比较范围 · 实现代码"},"unit":{"id":"notebook-blocks/7f07825ae8a84326b6a7a768b26c7caf","title":"3. 模型、损失与公平比较范围 · 实现代码"},"page":{"id":"reading-opening","title":"3. 模型、损失与公平比较范围 · 实现代码","number":1},"sectionTitle":"3. 模型、损失与公平比较范围 · 实现代码","currentQuestion":"3. 模型、损失与公平比较范围 · 实现代码","sourceSlides":[],"knowledgeBlock":null,"formulas":[],"previousBlock":"b-d48ec20ddf5edd6656297500","nextBlock":"b-7a04f65d6772d1d028ea2771","prerequisites":[{"id":"b-d48ec20ddf5edd6656297500","url":"#b-d48ec20ddf5edd6656297500","role":"problem-context"}],"relatedBlocks":[]},{"id":"b-7a04f65d6772d1d028ea2771","type":"p","scope":"document","url":"https://codingai-lec03.pages.dev/course/notebook-blocks/7f07825ae8a84326b6a7a768b26c7caf.html#b-7a04f65d6772d1d028ea2771","content":"按问题继续","sha256":"e91f24e4e4ca40e2099a0d81d30db6568f4bf2074b499eeee6e3959617bd97ac","chapter":{"id":"course","title":"3. 模型、损失与公平比较范围 · 实现代码"},"unit":{"id":"notebook-blocks/7f07825ae8a84326b6a7a768b26c7caf","title":"3. 模型、损失与公平比较范围 · 实现代码"},"page":{"id":"reading-opening","title":"3. 模型、损失与公平比较范围 · 实现代码","number":1},"sectionTitle":"3. 模型、损失与公平比较范围 · 实现代码","currentQuestion":"3. 模型、损失与公平比较范围 · 实现代码","sourceSlides":[],"knowledgeBlock":null,"formulas":[],"previousBlock":"b-fd148406324182699fcb8da0","nextBlock":"b-4518bd60ee2c9a359c49050f","prerequisites":[{"id":"b-fd148406324182699fcb8da0","url":"#b-fd148406324182699fcb8da0","role":"section-title"},{"id":"b-d48ec20ddf5edd6656297500","url":"#b-d48ec20ddf5edd6656297500","role":"problem-context"}],"relatedBlocks":[]},{"id":"b-4518bd60ee2c9a359c49050f","type":"p","scope":"document","url":"https://codingai-lec03.pages.dev/course/notebook-blocks/7f07825ae8a84326b6a7a768b26c7caf.html#b-4518bd60ee2c9a359c49050f","content":"RGB+问题输入、question-only对照、同图换问与同问换图","sha256":"5790b0afb52508f0174b1bc581f7847035a51c5c2a46e5c821bcd9d5d61bcbb0","chapter":{"id":"course","title":"3. 模型、损失与公平比较范围 · 实现代码"},"unit":{"id":"notebook-blocks/7f07825ae8a84326b6a7a768b26c7caf","title":"3. 模型、损失与公平比较范围 · 实现代码"},"page":{"id":"reading-opening","title":"3. 模型、损失与公平比较范围 · 实现代码","number":1},"sectionTitle":"3. 模型、损失与公平比较范围 · 实现代码","currentQuestion":"3. 模型、损失与公平比较范围 · 实现代码","sourceSlides":[],"knowledgeBlock":null,"formulas":[],"previousBlock":"b-7a04f65d6772d1d028ea2771","nextBlock":"b-16bc0c175346c9a01f05db8b","prerequisites":[{"id":"b-fd148406324182699fcb8da0","url":"#b-fd148406324182699fcb8da0","role":"section-title"},{"id":"b-d48ec20ddf5edd6656297500","url":"#b-d48ec20ddf5edd6656297500","role":"problem-context"}],"relatedBlocks":[]},{"id":"b-16bc0c175346c9a01f05db8b","type":"pre","scope":"document","url":"https://codingai-lec03.pages.dev/course/notebook-blocks/7f07825ae8a84326b6a7a768b26c7caf.html#b-16bc0c175346c9a01f05db8b","content":"```\nclass VQA(nn.Module):\n    def __init__(self,visual):\n        super().__init__();self.visual=visual\n        self.question=nn.Embedding(len(QUESTIONS),12)\n        if visual:\n            self.cnn=nn.Sequential(nn.Conv2d(3,12,3,padding=1),nn.ReLU(),nn.MaxPool2d(2),nn.Conv2d(12,24,3,padding=1),nn.ReLU(),nn.MaxPool2d(2),nn.Flatten(),nn.Linear(24*8*10,64),nn.ReLU())\n        self.answer=nn.Sequential(nn.Linear(64+12 if visual else 12,64),nn.ReLU(),nn.Linear(64,len(ANSWERS)))\n    def forward(self,pixels,question_id):\n        q=self.question(question_id)\n        features=torch.cat([self.cnn(pixels),q],-1) if self.visual else q\n        return self.answer(features)\n\nfull=VQA(True);question_only=VQA(False)\ncurve=[]\nfor label,net in [('image+question',full),('question-only',question_only)]:\n    optimizer=torch.optim.Adam(net.parameters(),lr=.002)\n    for epoch in range(28):\n        order=torch.randperm(Ytr.numel());net.train();loss_sum=0.\n        for pair_ids in order.split(72):\n            image_ids=pair_ids//3;qids=pair_ids%3\n            logits=net(Xtr[image_ids],qids);target=Ytr[image_ids,qids]\n            loss=F.cross_entropy(logits,target)\n            optimizer.zero_grad();loss.backward();optimizer.step();loss_sum+=float(loss.detach())*len(pair_ids)\n        if epoch in [0,5,13,27]:print(label,'epoch',epoch+1,'CE',round(loss_sum/Ytr.numel(),4))\n        curve.append({'model':label,'epoch':epoch+1,'train_ce':loss_sum/Ytr.numel()})\n    torch.save(net.state_dict(),OUT/('lab14-'+('visual' if net.visual else 'question-only')+'-weights.pt'))\n```","sha256":"701155870a1e8d1c9076048a018c3eac278a5c5ce5d1530da54c7367d1727177","chapter":{"id":"course","title":"3. 模型、损失与公平比较范围 · 实现代码"},"unit":{"id":"notebook-blocks/7f07825ae8a84326b6a7a768b26c7caf","title":"3. 模型、损失与公平比较范围 · 实现代码"},"page":{"id":"reading-opening","title":"3. 模型、损失与公平比较范围 · 实现代码","number":1},"sectionTitle":"3. 模型、损失与公平比较范围 · 实现代码","currentQuestion":"3. 模型、损失与公平比较范围 · 实现代码","sourceSlides":[],"knowledgeBlock":null,"formulas":[],"previousBlock":"b-4518bd60ee2c9a359c49050f","nextBlock":null,"prerequisites":[{"id":"b-fd148406324182699fcb8da0","url":"#b-fd148406324182699fcb8da0","role":"section-title"},{"id":"b-d48ec20ddf5edd6656297500","url":"#b-d48ec20ddf5edd6656297500","role":"problem-context"}],"relatedBlocks":[]}]}
