Huggingface transformers 在数据集大于RAM容量的地方从头开始培训RoBERTa?

Huggingface transformers 在数据集大于RAM容量的地方从头开始培训RoBERTa?,huggingface-transformers,roberta-language-model,Huggingface Transformers,Roberta Language Model,我有一个16GB的语料库,我的ram大约16GB。如果我加载整个数据集以从头开始训练语言模型RoBERTa,那么我将有一个内存问题。我打算使用Huggingface在其博客文章中的教程提供的脚本来训练我的RoBERTa: 然而,他们的博客文章建议使用LineByLineTextDatase。但是,这会急切地加载数据集 class LineByLineTextDataset(Dataset): """ This will be superseded b

我有一个16GB的语料库,我的ram大约16GB。如果我加载整个数据集以从头开始训练语言模型RoBERTa,那么我将有一个内存问题。我打算使用Huggingface在其博客文章中的教程提供的脚本来训练我的RoBERTa:

然而,他们的博客文章建议使用LineByLineTextDatase。但是,这会急切地加载数据集

class LineByLineTextDataset(Dataset):
    """
    This will be superseded by a framework-agnostic approach
    soon.
    """

    def __init__(self, tokenizer: PreTrainedTokenizer, file_path: str, block_size: int):
        assert os.path.isfile(file_path)
        # Here, we do not cache the features, operating under the assumption
        # that we will soon use fast multithreaded tokenizers from the
        # `tokenizers` repo everywhere =)
        logger.info("Creating features from dataset file at %s", file_path)

        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"]

    def __len__(self):
        return len(self.examples)

    def __getitem__(self, i) -> torch.Tensor:
        return torch.tensor(self.examples[i], dtype=torch.long)

出乎意料的是,我的内核在他们读取行的部分崩溃了。我不知道有没有办法让它懒洋洋地读。如果建议的答案能够在发布的教程中创建最小的代码更改,这将是非常理想的,因为我对Huggingface还比较陌生,并且担心自己无法调试它。

我建议使用Huggingface自己的。文件说:

它提供了从原始文件(CSV/JSON/text)或内存数据(python dict、pandas dataframe)加载和处理数据的非常有效的方法,特别关注内存效率和速度。例如,加载一个18GB的数据集(如English Wikipedia)会在RAM中分配9 MB,您可以在python中以1-2 GBit/s的速度对数据集进行迭代

对于使用您自己的数据创建dataset对象,有很好的解释和代码片段,它还解释了如何训练您自己的模型