如何解决如何在构建数据集时处理内存错误
我有一个大小超过 7.02 GB 的文本文件。我已经基于这个文本文件构建了一个分词器:
from tokenizers.implementations import ByteLevelBPETokenizer
tokenizer = ByteLevelBPETokenizer(
"tokenizer model/vocab.json","tokenizer model/merges.txt",)
我想构建如下数据集:
from transformers import LineByLineTextDataset
dataset = LineByLineTextDataset(
tokenizer=tokenizer,file_path="data.txt",block_size=128,)
由于我的数据量很大,会出现内存错误。 这是源代码:
with open(file_path,encoding="utf-8") as f:
lines = [line for line in f.read().splitlines() if (len(line) > 0 and not line.isspace())]
batch_encoding = tokenizer(lines,add_special_tokens=True,truncation=True,max_length=block_size)
self.examples = batch_encoding["input_ids"]
self.examples = [{"input_ids": torch.tensor(e,dtype=torch.long)} for e in self.examples]
出于这个原因,由于无法同时读取整个数据并且无法将行写入列表,因为列表太大以至于程序无法处理它,因此我更改了源代码如下:
for line in open(file_path,encoding="utf-8"):
if (len(line) > 0 and not line.isspace()):
new_line = line.split()
batch_encoding = tokenizer(new_line,max_length=block_size)
self.examples = batch_encoding["input_ids"]
self.examples = [{"input_ids": torch.tensor(e,dtype=torch.long)} for e in self.examples]
但是,它给了我以下错误:
Traceback (most recent call last):
File dfgd.py",line 21,in <module>
file_path="data.txt",#max_length = int(1e30)
File "C:\Users\DSP\AppData\Roaming\Python\Python37\site-packages\transformers\data\datasets\language_modeling.py",line 140,in __init__
batch_encoding = tokenizer(new_line,max_length=block_size)
TypeError: 'ByteLevelBPETokenizer' object is not callable
我该如何处理这个问题?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。