🚀 MobileBERT在SQuAD v2上微調
本項目基於MobileBERT在SQuAD v2數據集上進行微調,用於問答任務。MobileBERT是BERT_LARGE的精簡版本,配備了瓶頸結構,並在自注意力機制和前饋網絡之間進行了精心設計的平衡。
🚀 快速開始
示例代碼
from transformers import pipeline
qa_pipeline = pipeline(
"question-answering",
model="csarron/mobilebert-uncased-squad-v2",
tokenizer="csarron/mobilebert-uncased-squad-v2"
)
predictions = qa_pipeline({
'context': "The game was played on February 7, 2016 at Levi's Stadium in the San Francisco Bay Area at Santa Clara, California.",
'question': "What day was the game played on?"
})
print(predictions)
✨ 主要特性
- 模型精簡:MobileBERT是BERT_LARGE的精簡版本,在保持性能的同時減少了模型大小。
- 微調優化:在SQuAD v2數據集上進行微調,適用於問答任務。
📦 安裝指南
環境要求
- Python:
3.7.5
- 機器配置:
- CPU: Intel(R) Core(TM) i7-6800K CPU @ 3.40GHz
- 內存: 32 GiB
- GPU: 2 GeForce GTX 1070,每塊顯存8GiB
- GPU驅動: 418.87.01,CUDA: 10.1
安裝步驟
# 安裝https://github.com/huggingface/transformers
cd examples/question-answering
mkdir -p data
wget -O data/train-v2.0.json https://rajpurkar.github.io/SQuAD-explorer/dataset/train-v2.0.json
wget -O data/dev-v2.0.json https://rajpurkar.github.io/SQuAD-explorer/dataset/dev-v2.0.json
export SQUAD_DIR=`pwd`/data
python run_squad.py \
--model_type mobilebert \
--model_name_or_path google/mobilebert-uncased \
--do_train \
--do_eval \
--do_lower_case \
--version_2_with_negative \
--train_file $SQUAD_DIR/train-v2.0.json \
--predict_file $SQUAD_DIR/dev-v2.0.json \
--per_gpu_train_batch_size 16 \
--per_gpu_eval_batch_size 16 \
--learning_rate 4e-5 \
--num_train_epochs 5.0 \
--max_seq_length 320 \
--doc_stride 128 \
--warmup_steps 1400 \
--save_steps 2000 \
--output_dir $SQUAD_DIR/mobilebert-uncased-warmup-squad_v2 2>&1 | tee train-mobilebert-warmup-squad_v2.log
微調大約需要3.5小時完成。
📚 詳細文檔
數據集詳情
數據集 |
劃分 |
樣本數量 |
SQuAD2.0 |
訓練集 |
130k |
SQuAD2.0 |
評估集 |
12.3k |
微調結果
指標 |
數值 |
原始值(表5) |
EM |
75.2 |
76.2 |
F1 |
78.8 |
79.2 |
請注意,上述結果未進行任何超參數搜索。
📄 許可證
本項目採用MIT許可證。
由 Qingqing Cao 創建 | GitHub | Twitter
於紐約用心打造。