【问题标题】:Why new lines aren't generated with my fine-tuned DistilGPT2 model?为什么我的微调 DistilGPT2 模型没有生成新行?
【发布时间】:2021-03-16 14:56:30
【问题描述】:

我目前正在尝试微调 DistilGPT-2(使用 Pytorch 和 HuggingFace 转换器库)以完成代码完成任务。我的语料库排列如下示例:

<|startoftext|>
public class FindCityByIdService {
    private CityRepository cityRepository = ...
<|endoftext|>

我的第一次尝试是从转换器库中运行以下script

python run_clm.py 
     --model_type=gpt2 \
     --model_name_or_path distilgpt2 \
     --do_train \
     --train_file $TRAIN_FILE \
     --num_train_epochs 100 \
     --output_dir $OUTPUT_DIR \
     --overwrite_output_dir \
     --save_steps 20000 \
     --per_device_train_batch_size 4 \

在进行了一些生成测试后,我意识到该模型并未针对任何给定上下文预测 \ n。我想缺少一些预处理阶段或类似的东西。但无论如何,我应该怎么做才能使\ n 被预测为预期?

HF Forum question

谢谢!!

【问题讨论】:

  • 您是否尝试在训练数据中添加“\n”?我想模型只有在训练数据中看到它才能学会预测它。
  • 在尝试微调 gpt2 时遇到同样的问题。我的训练文件中有换行符,但我从生成的模型生成的任何内容都没有换行符。似乎换行符从训练数据中被剥离了?但我目前在代码中找不到任何证据。

标签: pytorch huggingface-transformers gpt-2


【解决方案1】:

我想我为此找到了一个 hacky 解决方案。

run_clm.py更改:

    def tokenize_function(examples):
        return tokenizer(examples[text_column_name])

到:

    def tokenize_function(examples):
        return tokenizer([example + "\n" for example in examples[text_column_name]])

最初构建数据集时,它会按行将其拆分,而不会在每行上保留换行符。然后group_texts 方法将它们连接成批次而不添加换行符。因此,将 tokenize_function 更改为将 \n 附加到每一行都会给我们这些换行符。

刚刚在我的微调工作中测试了这个变化,它奏效了!在生成的模型中生成换行符。

【讨论】:

  • 也可以通过将csv 文件传递​​给run_clm.py 而不是txt 来解决。如果其text 列中的文本包含换行符,它们也会出现在生成的输出中。此外,如果数据集由不相关的短文本组成,这可能会减少验证损失,因为示例将更自然地分割。
猜你喜欢
  • 2015-06-09
  • 1970-01-01
  • 2011-09-12
  • 2014-06-15
  • 1970-01-01
  • 2013-03-18
  • 2021-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多