electra large discriminator squad2 512

提供商ahotrod
分类question-answering
许可证Apache-2.0
下载量836.8K
星标0

简介

这是一个基于 ELECTRA-Large 架构并在 SQuAD 2.0 数据集上微调的判别器模型,专注于高质量的阅读理解与问答任务。与传统的 BERT 不同,它采用了更高效的判别式预训练,使其在捕捉文本细微语义差异方面表现出色。该模型支持最高 512 个 token 的输入长度,非常适合用于构建本地知识库问答、文档自动化分析等场景。对于开发者而言,它能无缝集成到 Hugging Face 生态中,上手门槛低,是替代通用大模型处理特定抽取式问答任务、降低推理成本的理想选择。

核心亮点

  • 专为 SQuAD 2.0 优化,擅长从文本中精准提取答案
  • 基于 ELECTRA 架构,语义理解能力强且推理效率高
  • 支持 512 token 上下文,适配大多数短文阅读场景
  • Apache-2.0 协议,方便企业级私有化部署与商用

使用方法

安装依赖
# 安装 Hugging Face transformers
pip install transformers torch
SDK 使用
# 使用 transformers 加载模型
from transformers import AutoModel, AutoTokenizer

model = AutoModel.from_pretrained("ahotrod/electra_large_discriminator_squad2_512")
tokenizer = AutoTokenizer.from_pretrained("ahotrod/electra_large_discriminator_squad2_512")

Hugging Face 下载

我们推荐使用命令行或者 Hugging Face Hub SDK 来进行模型的下载。

操作指引:在下载前,请先通过如下命令安装 huggingface_hub:

操作指引
pip install -U huggingface_hub

命令行下载

下载完整模型库

下载完整模型库
huggingface-cli download ahotrod/electra_large_discriminator_squad2_512

下载单个文件到指定本地文件夹(以下载 config.json 到当前路径下 ./dir 目录为例)

下载单个文件到指定本地文件夹(以下载 config.json 到当前路径下 ./dir 目录为例)
huggingface-cli download ahotrod/electra_large_discriminator_squad2_512 config.json --local-dir ./dir

更多命令行下载选项,可参见官方文档

SDK 下载

SDK 下载
# 模型下载
from huggingface_hub import snapshot_download
model_dir = snapshot_download('ahotrod/electra_large_discriminator_squad2_512')

Git 下载

请确保 lfs 已经被正确安装

Git 下载
git lfs install
git clone https://huggingface.co/ahotrod/electra_large_discriminator_squad2_512

如果您希望跳过 lfs 大文件下载,可以使用如下命令

跳过 LFS
GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/ahotrod/electra_large_discriminator_squad2_512

模型文件托管在 Hugging Face Hub,使用 HF CLI / SDK / Git 直接下载,不经过本站。

PyTorch / Transformers 使用

安装 Transformers

安装 Transformers
pip install -U transformers torch

模型加载和推理

模型加载和推理
from transformers import AutoModelForCausalLM, AutoTokenizer

model = AutoModelForCausalLM.from_pretrained('ahotrod/electra_large_discriminator_squad2_512')
tokenizer = AutoTokenizer.from_pretrained('ahotrod/electra_large_discriminator_squad2_512')

完整文档

来源: HuggingFace

ELECTRA_large_discriminator language model fine-tuned on SQuAD2.0

with the following results:

code
"exact": 87.09677419354838,
  "f1": 89.98343832723452,
  "total": 11873,
  "HasAns_exact": 84.66599190283401,
  "HasAns_f1": 90.44759839056285,
  "HasAns_total": 5928,
  "NoAns_exact": 89.52060555088309,
  "NoAns_f1": 89.52060555088309,
  "NoAns_total": 5945,
  "best_exact": 87.09677419354838,
  "best_exact_thresh": 0.0,
  "best_f1": 89.98343832723432,
  "best_f1_thresh": 0.0

from script:

code
python ${EXAMPLES}/run_squad.py \
  --model_type electra \
  --model_name_or_path google/electra-large-discriminator \
  --do_train \
  --do_eval \
  --train_file ${SQUAD}/train-v2.0.json \
  --predict_file ${SQUAD}/dev-v2.0.json \
  --version_2_with_negative \
  --do_lower_case \
  --num_train_epochs 3 \
  --warmup_steps 306 \
  --weight_decay 0.01 \
  --learning_rate 3e-5 \
  --max_grad_norm 0.5 \
  --adam_epsilon 1e-6 \
  --max_seq_length 512 \
  --doc_stride 128 \
  --per_gpu_train_batch_size 8 \
  --gradient_accumulation_steps 16 \
  --per_gpu_eval_batch_size 128 \
  --fp16 \
  --fp16_opt_level O1 \
  --threads 12 \
  --logging_steps 50 \
  --save_steps 1000 \
  --overwrite_output_dir \
  --output_dir ${MODEL_PATH}

using the following system & software:

code
Transformers: 2.11.0
PyTorch: 1.5.0
TensorFlow: 2.2.0
Python: 3.8.1
OS/Platform: Linux-5.3.0-59-generic-x86_64-with-glibc2.10
CPU/GPU: Intel i9-9900K / NVIDIA Titan RTX 24GB