从代码的角度看Pointer-Generator Networks【上】
- 1、ModuleNotFoundError: No module named 'data_util'
- 2、ModuleNotFoundError: No module named 'Queue'
- 3、ModuleNotFoundError: No module named 'config'
- 4、SyntaxError: Missing parentheses in call to 'print'. Did you mean print('Warning: incorrectly formatted line in vocabulary file: %s\n' % line)?
- 5、FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\92458\\ptr_nw/cnn-dailymail-master/finished_files/vocab'
- 6、UnicodeDecodeError: 'gbk' codec can't decode byte 0xa2 in position 7012: illegal multibyte sequence
- 7、NameError: name 'xrange' is not defined
- 8、AttributeError: 'generator' object has no attribute 'next'
- 9、TypeError: argument should be integer or bytes-like object, not 'str'
- 10、TypeError: sequence item 0: expected str instance, bytes found
Paper: Get To The Point: Summarization with Pointer-Generator Networks
Code: pointer_summarizer
这篇blog post记录了我把代码跑起来的过程中所遇到的10个BUG。代码原来的环境:Python2.7 +Pytorch0.4,我的环境:Python3.7+Pytorch1.7。
如果您有一些问题,欢迎评论~
1、ModuleNotFoundError: No module named ‘data_util’

报错信息:模块未被发现错误:没有‘data_util’模块
解决过程:
先定位到脚本中出错的位置from data_util import config,再看这个Project的目录,发现data_util存在,只是在这个脚本下找不到它。
观察data_util是在model.py脚本所在文件夹training_ptr_gen的同一目录下,猜测应该是找不到data_util所在路径,所以在当前脚本下将data_util所在路径加入到可搜索的路径列表里,加入的方式如下:
import os
base_dir = os.path.dirname(__file__) #获取当前脚本所在路径
import sys
sys.path.append(os.path.join(base_dir,'../'))#构建data_util所在路径,并将其添加到可搜索的路径列表里
然后,这个bug就解决了~
2、ModuleNotFoundError: No module named ‘Queue’

报错信息:模块未被发现错误:没有‘Queue’模块
原因: python2中队列用的是Queue模块,而在python3中队列用的是queue模块。
解决方法: 将Queue改为queue即可。
3、ModuleNotFoundError: No module named ‘config’

报错信息:模块未被发现错误:没有‘config’模块
解决过程: 同标题1的解决过程,将config所在文件夹的路径加入到可搜索的路径列表里即可。加入方式:
import os
base_dir = os.path.dirname(__file__)
import sys
sys.path.append(base_dir)
4、SyntaxError: Missing parentheses in call to ‘print’. Did you mean print(‘Warning: incorrectly formatted line in vocabulary file: %s\n’ % line)?

报错信息:语法错误:在调用‘print’的时候丢失了括号. 你是否要print('Warning: incorrectly formatted line in vocabulary file: %s\n' % line)?
原因: 语法问题,python2中调用print的时候不带括号,而python3中带括号。
解决方法: 加上括号即可。
5、FileNotFoundError: [Errno 2] No such file or directory: ‘C:\Users\92458\ptr_nw/cnn-dailymail-master/finished_files/vocab’

报错信息:文件未被发现错误:没有文件或目录'C:\\Users\\92458\\ptr_nw/cnn-dailymail-master/finished_files/vocab'
原因: 找不到路径
解决方法: 找到配置文件config.py,将路径改成自己的。Note: 对于其他报错的路径,某些文件夹可能需要自己手动创建。
6、UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xa2 in position 7012: illegal multibyte sequence

报错信息:统一的字符编码标准Unicode解码错误:‘gbk’编解码器无法解码7012位置上0xa2字节:无效的多字节序列'
**原因:**字符的编码格式的问题
**解决方法:**指定读取的字符编码格式为utf-8。将第35行的with open(vocab_file, 'r') as vocab_f:改为with open(vocab_file, 'r', encoding='utf-8') as vocab_f:。
7、NameError: name ‘xrange’ is not defined

报错信息:名字错误:'xrange'没有被定义
原因: 语法问题,python2中用的是xrange,python3中用的是range
解决方法: 将xrange改为range即可。
8、AttributeError: ‘generator’ object has no attribute ‘next’

报错信息:属性错误:'generator'对象没有属性'next'
原因: 语法问题,python2的用法是input_gen.next()(成员函数),python3的用法是next(input_gen)(内置函数)。
解决方法: 将input_gen.next()改为next(input_gen)。
9、TypeError: argument should be integer or bytes-like object, not ‘str’

报错信息:类型错误:参数应该是integer或者bytes类型的对象,而不是str类型的
原因: 函数abstract.index()中的参数SENTENCE_START应该是一个integer或者bytes类型的对象,但实际上它是一个str类型的。
解决方法:将参数SENTENCE_START进行类型转换。转换方式:SENTENCE_START.encode()。
10、TypeError: sequence item 0: expected str instance, bytes found

报错信息:类型错误:序列项0:期望是str类型的实例,但是发现了bytes类型的
原因:参数abstract_sentences应该是一个str类型的,但实际上是一个bytes类型的。
解决方法:将参数abstract_sentences进行类型转换,转换方式:str(abstract_sentences)
这篇博客详细记录了将Pointer-Generator Networks的代码从Python2迁移到Python3过程中遇到的10个错误及解决方案。包括ModuleNotFoundError(如data_util、Queue、config),SyntaxError(print函数括号缺失),FileNotFoundError(路径问题),UnicodeDecodeError(编码格式问题),NameError(xrange未定义),AttributeError(generator的next方法),以及TypeError(与str和bytes类型相关的错误)。每个错误都给出了具体的解决办法,涉及路径添加、模块名更改、语法调整和类型转换等。

3万+

被折叠的 条评论
为什么被折叠?



