知识卡片
重载权重需要同时恢复架构与词表
用state_dict恢复参数时,层类型、宽度、层数、词表编号与输出映射应匹配。仅因张量形状能加载,不保证文字含义不变;修改类后新建模型也应保存版本和配置,不能只留model.pt便称可复现。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- · 查看原书依据
>>> with open('model.pt', 'rb') as f: ... orig_model = torch.load(f, map_location='cpu') #1 >>> model = RNNModel('GRU', vocab=corpus.dictionary, num_layers=1) #2 >>> model.load_state_dict(orig_model.state_dict()) >>> words = generate_words( ... model=model, vocab=vocab, prompt='The', temperature=.1) #3>>> print(' '.join(w for w in words)) #3 ... = = Valkyria Valkyria Valkyria Valkyria = = The kakapo is a common↪ ↪ starling , and the of the of the , ... #1 Loads
参考来源
- 位置:《Natural Language Processing in Action, Second Edition》第08章《循环网络与长短期记忆》(来源项目:a19c7933-4eda-4176-aebd-30fe7e1ad292;源文件:extracted/Natural Language Processing in Action, Second Edition/_epub-src/OEBPS/Text/chapter-8.html)
- 结论依据:摘录中关于重载权重需要同时恢复架构与词表的定义、比较或条件支撑卡片;原书表述不严谨的范围已在覆盖记录逐项限定,不把案例结果外推为普遍结论。
- 原始内容:>>> with open('model.pt', 'rb') as f: ... orig_model = torch.load(f, map_location='cpu') #1 >>> model = RNNModel('GRU', vocab=corpus.dictionary, num_layers=1) #2 >>> model.load_state_dict(orig_model.state_dict()) >>> words = generate_words( ... model=model, vocab=vocab, prompt='The', temperature=.1) #3>>> print(' '.join(w for w in words)) #3 ... = = Valkyria Valkyria Valkyria Valkyria = = The kakapo is a common↪ ↪ starling , and the of the of the , ... #1 Loads