我发现Google NMT使用编解码器读取输入数据文件。
import codecs
import tensorflow as tf
with codecs.getreader("utf-8")(tf.gfile.GFile(input_file, mode="rb")) as f:
return f.read().splitlines()
我有两个问题。
size more than 5 GB
使用16GB RAM的个人计算机中读取庞大的数据集,而不会出现内存错误tf.gfile.GFile
?我非常感谢能帮助我阅读庞大的语言语料库的解决方案没有出现内存错误
。2.我已经在代码中导入了编解码器,但是为什么会出现此错误"NameError: name 'codecs' is not defined
?
编辑1:
对于2。
OutOfRangeError Traceback (most recent call last) <ipython-input-7-e78786c1f151> in <module>() 6 input_file = os.path.join(source_path) 7 with codecs.getreader("utf-8")(tf.gfile.GFile(input_file, mode="rb")) as f: ----> 8 source_text = f.read().splitlines()
当操作迭代超出有效输入范围时,将引发OutOfRangeError。我怎样才能解决这个问题 ?
如果文件很大,建议逐行处理。下面的代码可以解决问题:
with open("input_file") as infile:
for line in infile:
do_something_with(line)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句