©PaperWeekly 原創 · 作者|程任清、劉世興
單位|騰訊遊戲知幾AI團隊
研究方向|自然語言處理
https://share.weiyun.com/S0CeWKGM (UER版)
https://share.weiyun.com/qqJaHcGZ(huggingface版)
#構建領域詞典
python3 scripts/build_vocab.py --corpus_path ../data/all_qa_log.txt \
--vocab_path ../data/zhiji_word_pre/zhiji_word_vocab.txt \
--workers_num 10 \
--min_count 30 \
--tokenizer space
python3 scripts/dynamic_vocab_adapter.py --old_model_path ../data/wiki_word_model/wiki_bert_word_model.bin \
--old_vocab_path ../data/wiki_word_model/wiki_word_vocab.txt \
--new_vocab_path ../data/zhiji_word_pre/zhiji_word_vocab.txt \
--new_model_path ../data/zhiji_word_pre/zhiji_bert_word_model.bin
#將下行
w = line.strip().split()[0] if line.strip() else " "
#替換為
w = line.strip("\n") if line.strip("\n") else " "
For Chinese models, we need to generate a reference files, because it's tokenized at the character level.
**Q :** Why a reference file?
**A :** Suppose we have a Chinese sentence like: `我喜歡你` The original Chinese-BERT will tokenize it as`['我','喜','歡','你']` (character level). But `喜歡` is a whole word. For whole word masking proxy, we need a result like `['我','喜','##歡','你']`, so we need a reference file to tell the model which position of the BERT original tokenshould be added `##`.
```bash
export TRAIN_FILE=/path/to/dataset/train.txt
export tokens_RESOURCE=/path/to/tokens/tokenizer
export BERT_RESOURCE=/path/to/bert/tokenizer
export SAVE_PATH=/path/to/data/ref.txt
python examples/contrib/run_chinese_ref.py \
--file_name=path_to_train_or_eval_file \
--tokens=path_to_tokens_tokenizer \
--bert=path_to_bert_tokenizer \
--save_path=path_to_reference_file
#訓練數據樣例
我喜歡你
興高採烈表情怎麼購買
我需要一張國慶投票券
#對應生成的reference file
[3]
[2, 3, 4, 5, 6, 8, 10]
[3, 7, 8, 9, 10]
#以 興高採烈表情怎麼購買 為例
分詞後:['興高採烈表情', '怎麼', '購買']
bert分詞後:['[CLS]', '興', '高', '採', '烈', '表', '情', '怎', '麼', '購', '買', '[SEP]']
reference file對應的為:[2, 3, 4, 5, 6, 8, 10] (每個值為對應的bert分詞結果的位置索引,並需要和前字進行合併的)
結合bert分詞和reference file可以生成:['[CLS]', '興', '##高', '##採', '##烈', '##表', '##情', '怎', '##麼', '購', '##買', '[SEP]']
data_dir="/home/tione/notebook/data/qa_log_data/"
train_data=$data_dir"qa_log_train.json"
valid_data=$data_dir"qa_log_val.json"
pretrain_model="/home/tione/notebook/data/chinese-bert-wwm-ext"
python -m torch.distributed.launch --nproc_per_node 8 run_mlm_wwm.py \
--model_name_or_path $pretrain_model \
--model_type bert \
--train_file $train_data \
--do_train \
--do_eval \
--eval_steps 10000 \
--validation_file $valid_data\
--max_seq_length 64 \
--pad_to_max_length \
--num_train_epochs 5.0 \
--per_device_train_batch_size 128 \
--gradient_accumulation_steps 16 \
--save_steps 5000 \
--preprocessing_num_workers 10 \
--learning_rate 5e-5 \
--output_dir ./output_qalog \
--overwrite_output_dir
#如新建spv環境
conda create --prefix=~/notebook/python_envs/spv
conda activate notebook/python_envs/spv/
這個庫需要連接外網來獲取數據讀取腳本(會出現 ConnectionError)。解決的方法是去 datasets 的 git 上下載我們需要的腳本到本地,比如我們需要 text.py 的腳本(即一行一行讀取訓練數據),在下方連結下載 text.py,並修改 run_mlm_wwm.py 代碼。
https://github.com/huggingface/datasets/tree/1.1.2/datasets/text
#將
datasets = load_dataset(extension, data_files=data_files)
#修改為:
datasets = load_dataset("../test.py",data_files=data_files)
#這裡增加cache_file_names,制定cache路徑。
#這裡其實還有一個坑,我們一直以為datasets的是Dataset類,所以指定的參數是cache_file_name,然後它卻是DatasetDict,參數為cache_file_names
tokenized_datasets = datasets.map(
tokenize_function,
batched=True,
num_proc=data_args.preprocessing_num_workers,
remove_columns=[text_column_name],
load_from_cache_file=not data_args.overwrite_cache, cache_file_names={"train":"/home/tione/notebook/wwm/process_data/processed_train.arrow","validation":"/home/tione/notebook/wwm/process_data/processed_validation.arrow"},
)
在使用 reference file 的時候很容易被 kill 掉。調試的時候會卡到第 5 行,然後就被 kill 了。
def add_chinese_references(dataset, ref_file):
with open(ref_file, "r", encoding="utf-8") as f:
refs = [json.loads(line) for line in f.read().splitlines() if (len(line) > 0 and not line.isspace())]
assert len(dataset) == len(refs)
dataset_dict = {c: dataset[c] for c in dataset.column_names}
dataset_dict["chinese_ref"] = refs
return Dataset.from_dict(dataset_dict)
import json
with open("qa_log_data/qa_log_train.json","w") as fout:
with open("qa_log_data/qa_log_train.txt","r") as f1:
with open("qa_log_data/ref_train.txt","r") as f2:
for x,y in zip(f1.readlines(),f2.readlines()):
y = json.loads(y.strip("\n"))
out_dict = {"text":x.strip("\n"),"chinese_ref":y}
out_dict = json.dumps(out_dict)
fout.write(out_dict+"\n")
[1] Gururangan S, Marasović A, Swayamdipta S, et al. Don't Stop Pretraining: Adapt Language Models to Domains and Tasks[J]. arXiv preprint arXiv:2004.10964, 2020.
[2] Devlin J, Chang M W, Lee K, et al. Bert: Pre-training of deep bidirectional transformers for language understanding[J]. arXiv preprint arXiv:1810.04805, 2018.
[3] Li X, Meng Y, Sun X, et al. Is word segmentation necessary for deep learning of Chinese representations?[J]. arXiv preprint arXiv:1905.05526, 2019.
[4] Cui Y, Che W, Liu T, et al. Pre-training with whole word masking for chinese bert[J]. arXiv preprint arXiv:1906.08101, 2019.
[5] Lan Z, Chen M, Goodman S, et al. A Lite BERT for Self-supervised Learning of Language Representations[J]. arXiv preprint arXiv:1909.11942, 2019.
[6] Clark K, Luong M T, Le Q V, et al. Electra: Pre-training text encoders as discriminators rather than generators[J]. arXiv preprint arXiv:2003.10555, 2020.
[7] Li X, Yan H, Qiu X, et al. Flat: Chinese ner using flat-lattice transformer[J]. arXiv preprint arXiv:2004.11795, 2020.
[8] Liu W, Zhou P, Zhao Z, et al. K-bert: Enabling language representation with knowledge graph[C]//Proceedings of the AAAI Conference on Artificial Intelligence. 2020, 34(03): 2901-2908.
如何才能讓更多的優質內容以更短路逕到達讀者群體,縮短讀者尋找優質內容的成本呢?答案就是:你不認識的人。
總有一些你不認識的人,知道你想知道的東西。PaperWeekly 或許可以成為一座橋梁,促使不同背景、不同方向的學者和學術靈感相互碰撞,迸發出更多的可能性。
PaperWeekly 鼓勵高校實驗室或個人,在我們的平臺上分享各類優質內容,可以是最新論文解讀,也可以是學習心得或技術乾貨。我們的目的只有一個,讓知識真正流動起來。
📝 來稿標準:
• 稿件確係個人原創作品,來稿需註明作者個人信息(姓名+學校/工作單位+學歷/職位+研究方向)
• 如果文章並非首發,請在投稿時提醒並附上所有已發布連結
• PaperWeekly 默認每篇文章都是首發,均會添加「原創」標誌
📬 投稿郵箱:
• 投稿郵箱:hr@paperweekly.site
• 所有文章配圖,請單獨在附件中發送
• 請留下即時聯繫方式(微信或手機),以便我們在編輯發布時和作者溝通
🔍
現在,在「知乎」也能找到我們了
進入知乎首頁搜索「PaperWeekly」
點擊「關注」訂閱我們的專欄吧
關於PaperWeekly
PaperWeekly 是一個推薦、解讀、討論、報導人工智慧前沿論文成果的學術平臺。如果你研究或從事 AI 領域,歡迎在公眾號後臺點擊「交流群」,小助手將把你帶入 PaperWeekly 的交流群裡。