模型:
这是EMNLP 2021长篇论文 Phrase-BERT: Improved Phrase Embeddings from BERT with an Application to Corpus Exploration 的官方存储库。除了论文中使用的数据集外,我们还提供了用于训练和评估Phrase-BERT的 code 。
当您安装完 sentence-transformers 后,使用此模型变得很简单:
pip install -U sentence-transformers
我们的模型在pytorch=1.9.0,tranformers=4.8.1,sentence-tranformers=2.1.0 TODO下进行了测试
然后可以像这样使用模型:
from sentence_transformers import SentenceTransformer
phrase_list = [ 'play an active role', 'participate actively', 'active lifestyle']
model = SentenceTransformer('whaleloops/phrase-bert')
phrase_embs = model.encode( phrase_list )
[p1, p2, p3] = phrase_embs
与Sentence-BERT一样,默认输出是一组numpy数组:
for phrase, embedding in zip(phrase_list, phrase_embs):
print("Phrase:", phrase)
print("Embedding:", embedding)
print("")
计算短语嵌入的点积的示例:
import numpy as np
print(f'The dot product between phrase 1 and 2 is: {np.dot(p1, p2)}')
print(f'The dot product between phrase 1 and 3 is: {np.dot(p1, p3)}')
print(f'The dot product between phrase 2 and 3 is: {np.dot(p2, p3)}')
计算短语嵌入的余弦相似度的示例:
import torch
from torch import nn
cos_sim = nn.CosineSimilarity(dim=0)
print(f'The cosine similarity between phrase 1 and 2 is: {cos_sim( torch.tensor(p1), torch.tensor(p2))}')
print(f'The cosine similarity between phrase 1 and 3 is: {cos_sim( torch.tensor(p1), torch.tensor(p3))}')
print(f'The cosine similarity between phrase 2 and 3 is: {cos_sim( torch.tensor(p2), torch.tensor(p3))}')
输出应该类似于:
The dot product between phrase 1 and 2 is: 218.43600463867188 The dot product between phrase 1 and 3 is: 165.48483276367188 The dot product between phrase 2 and 3 is: 160.51708984375 The cosine similarity between phrase 1 and 2 is: 0.8142536282539368 The cosine similarity between phrase 1 and 3 is: 0.6130303144454956 The cosine similarity between phrase 2 and 3 is: 0.584893524646759
考虑到缺乏统一的短语嵌入评估基准,我们收集了以下五个短语语义评估任务,这些任务在我们的论文中有进一步的描述:
根据您的目录更改config/model_path.py中的模型路径并
对Turney进行评估,请运行python eval_turney.py
对BiRD进行评估,请运行python eval_bird.py
对PPDB / PPDB-filtered / PAWS-short进行评估,请运行eval_ppdb_paws.py,其中:
nohup python -u eval_ppdb_paws.py \
--full_run_mode \
--task <task-name> \
--data_dir <input-data-dir> \
--result_dir <result-storage-dr> \
>./output.txt 2>&1 &
如果您想自己训练Phrase-BERT,可以使用您感兴趣的域中的数据进行训练。请参考phrase-bert/phrase_bert_finetune.py
我们用于微调Phrase-BERT的数据集在此处: training data csv file 和 validation data csv file 。
要重现训练的Phrase-BERT,请运行:
export INPUT_DATA_PATH=<directory-of-phrasebert-finetuning-data>
export TRAIN_DATA_FILE=<training-data-filename.csv>
export VALID_DATA_FILE=<validation-data-filename.csv>
export INPUT_MODEL_PATH=bert-base-nli-stsb-mean-tokens
export OUTPUT_MODEL_PATH=<directory-of-saved-model>
python -u phrase_bert_finetune.py \
--input_data_path $INPUT_DATA_PATH \
--train_data_file $TRAIN_DATA_FILE \
--valid_data_file $VALID_DATA_FILE \
--input_model_path $INPUT_MODEL_PATH \
--output_model_path $OUTPUT_MODEL_PATH
如果您发现以上内容有用,请引用我们:
@inproceedings{phrasebertwang2021,
author={Shufan Wang and Laure Thompson and Mohit Iyyer},
Booktitle = {Empirical Methods in Natural Language Processing},
Year = "2021",
Title={Phrase-BERT: Improved Phrase Embeddings from BERT with an Application to Corpus Exploration}
}