forked from LongxingTan/open-retrievals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_pairwise.py
65 lines (53 loc) · 2.16 KB
/
train_pairwise.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Pairwise sentence embedding fine-tuning"""
import torch.nn as nn
from datasets import load_dataset
from transformers import (
AdamW,
AutoTokenizer,
TrainingArguments,
get_linear_schedule_with_warmup,
)
from retrievals import AutoModelForEmbedding, PairCollator, RetrievalTrainer
from retrievals.losses import InfoNCE, SimCSE, TripletLoss
model_name_or_path: str = "sentence-transformers/paraphrase-multilingual-mpnet-base-v2"
batch_size: int = 32
epochs: int = 3
output_dir: str = './checkpoints'
def train():
train_dataset = load_dataset('shibing624/nli_zh', 'STS-B')['train']
train_dataset = train_dataset.rename_columns({'sentence1': 'query', 'sentence2': 'positive'})
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, use_fast=False)
model = AutoModelForEmbedding.from_pretrained(model_name_or_path, pooling_method="mean")
model = model.set_train_type('pairwise')
optimizer = AdamW(model.parameters(), lr=5e-5)
num_train_steps = int(len(train_dataset) / batch_size * epochs)
scheduler = get_linear_schedule_with_warmup(
optimizer, num_warmup_steps=0.05 * num_train_steps, num_training_steps=num_train_steps
)
training_args = TrainingArguments(
output_dir=output_dir,
num_train_epochs=epochs,
per_device_train_batch_size=batch_size,
remove_unused_columns=False,
)
trainer = RetrievalTrainer(
model=model,
args=training_args,
train_dataset=train_dataset,
data_collator=PairCollator(tokenizer, query_max_length=128, document_max_length=128),
loss_fn=InfoNCE(nn.CrossEntropyLoss(label_smoothing=0.05)),
)
trainer.optimizer = optimizer
trainer.scheduler = scheduler
trainer.train()
model.save_pretrained(training_args.output_dir)
if trainer.is_world_process_zero():
tokenizer.save_pretrained(training_args.output_dir)
def predict():
model = AutoModelForEmbedding.from_pretrained(output_dir, pooling_method="cls")
sentences = ['A dog is chasing car.', 'A man is playing a guitar.']
embeddings = model.encode(sentences)
print(embeddings)
if __name__ == '__main__':
train()
predict()