首页 > 解决方案 > 在 Colab 上微调 Huggingface GPT-2 时遇到问题——断言错误

问题描述

我希望根据我自己的文本数据微调 Huggingface 的 GPT-2 转换器模型。我想在 Google Colab 笔记本上执行此操作。但是,我有两个问题。首先是它似乎不起作用。

我通过 Colab 安装了各种零碎的东西:

!git clone https://github.com/huggingface/transformers
%cd transformers
!pip install .
!pip install -r ./examples/requirements.txt

按照示例,我将建议的 WikiText 示例数据上传到用于训练并在笔记本中运行建议的 CLI 命令。

!export TRAIN_FILE=wiki.train.raw
!export TEST_FILE=wiki.test.raw

!python run_lm_finetuning.py \
    --output_dir=output \
    --model_type=gpt2 \
    --model_name_or_path=gpt2 \
    --do_train \
    --train_data_file=$TRAIN_FILE \
    --do_eval \
    --eval_data_file=$TEST_FILE

这会持续一段时间,但后来我得到一个断言错误:

Traceback (most recent call last):
  File "run_lm_finetuning.py", line 790, in <module>
    main()
  File "run_lm_finetuning.py", line 735, in main
    train_dataset = load_and_cache_examples(args, tokenizer, evaluate=False)
  File "run_lm_finetuning.py", line 149, in load_and_cache_examples
    return TextDataset(tokenizer, args, file_path=file_path, block_size=args.block_size)
  File "run_lm_finetuning.py", line 88, in __init__
    assert os.path.isfile(file_path)
AssertionError

我认为这与我的训练数据有关吗?请注意,这两个文件与 lm_finetuning.py 脚本位于同一文件夹中,因此我不确定 os.path 问题可能是什么。

benchmarks.py     run_generation.py   summarization
contrib       run_glue.py         test_examples.py
distillation      run_lm_finetuning.py    tests_samples
hans          run_multiple_choice.py  utils_multiple_choice.py
mm-imdb       run_ner.py          utils_ner.py
pplm          run_squad.py        wiki.test.raw
README.md     run_tf_glue.py      wiki.test.tokens
requirements.txt  run_tf_ner.py       wiki.train.raw
run_bertology.py  run_xnli.py         wiki.train.tokens

我的第二个问题是,即使微调确实有效,我也不知道如何用我自己的文本数据复制结果。我无法打开 WikiText 原始文件,所以我不知道它们是什么格式。是普通的纯文本吗?它们是否以某种方式被标记化了?如果有人能就此启发我,将不胜感激!

标签: python-3.xnlpartificial-intelligencehuggingface-transformers

解决方案


如果其他人来寻找解决方案,答案是 bash 中的环境变量需要在 google colab 中%而不是!在 google colab 中指定。所以脚本应该是:

%env TRAIN_FILE=/content/wiki.train.raw
%env TEST_FILE=/content/wiki.test.raw

!python /content/transformers/examples/run_lm_finetuning.py \
    --output_dir=output \
    --model_type=gpt2 \
    --model_name_or_path=gpt2 \
    --do_train \
    --train_data_file=/content/wiki.train.raw \
    --do_eval \
    --eval_data_file=/content/wiki.test.raw

还要注意,使用绝对文件路径比相对文件路径更容易。向 hugginface GitHub repo 上的 cronoik 致敬,指出所有这些。


推荐阅读