[LAB 13 · 看图生成一句话:训练、解码与消融](/course/notebooks/3da47f3ee3554ee0a36e0bb6a2fdc61f.html) # 先拆开两个容易混淆的问题 · 实现代码 [独立阅读](/course/notebook-blocks/3e80e4f0e33b470ba563baeb509e47c9.html) 按问题继续 真实CNN–GRU条件训练与greedy/beam生成、遮图/错图消融 展开完整实现代码 ``` # Detached readonly runs write only to an isolated ephemeral directory. import os, tempfile _LAB_WORKDIR = tempfile.mkdtemp(prefix="lecture03-lab13-") os.chdir(_LAB_WORKDIR) print("ephemeral workdir:", _LAB_WORKDIR) seqs=[[1,4,5,2],[1,6,2,0]] print('warm-up input',[s[:-1] for s in seqs]) print('warm-up target',[s[1:] for s in seqs]) print('warm-up loss mask',[[int(t!=0) for t in s[1:]] for s in seqs]) # Every outgoing distribution sums to one. Both paths terminate after two tokens. tree={():{'A':.55,'B':.45},('A',):{'X':.45,'Z':.55},('B',):{'Y':.80,'Z':.20}} assert all(abs(sum(p.values())-1)<1e-9 for p in tree.values()) greedy_path=();greedy_probability=1. for _ in range(2): token=max(tree[greedy_path],key=tree[greedy_path].get) greedy_probability*=tree[greedy_path][token];greedy_path+= (token,) beams=[((),1.)] for _ in range(2): beams=sorted([(seq+(word,),p*conditional) for seq,p in beams for word,conditional in tree[seq].items()],key=lambda b:b[1],reverse=True)[:2] print('actual tree greedy:',greedy_path,greedy_probability,'beam2:',beams[0]) ```