| """ |
| The script shows how to train Augmented SBERT (In-Domain) strategy for STSb dataset with Semantic Search Sampling. |
| |
| |
| Methodology: |
| Three steps are followed for AugSBERT data-augmentation strategy with Semantic Search - |
| 1. Fine-tune cross-encoder (BERT) on gold STSb dataset |
| 2. Fine-tuned Cross-encoder is used to label on Sem. Search sampled unlabeled pairs (silver STSb dataset) |
| 3. Bi-encoder (SBERT) is finally fine-tuned on both gold + silver STSb dataset |
| |
| Citation: https://arxiv.org/abs/2010.08240 |
| |
| Usage: |
| python train_sts_indomain_semantic.py |
| |
| OR |
| python train_sts_indomain_semantic.py pretrained_transformer_model_name top_k |
| |
| python train_sts_indomain_semantic.py bert-base-uncased 3 |
| """ |
| from torch.utils.data import DataLoader |
| from sentence_transformers import models, losses, util |
| from sentence_transformers import LoggingHandler, SentenceTransformer |
| from sentence_transformers.cross_encoder import CrossEncoder |
| from sentence_transformers.cross_encoder.evaluation import CECorrelationEvaluator |
| from sentence_transformers.evaluation import EmbeddingSimilarityEvaluator |
| from sentence_transformers.readers import InputExample |
| from datetime import datetime |
| import logging |
| import csv |
| import torch |
| import tqdm |
| import sys |
| import math |
| import gzip |
| import os |
|
|
| |
| logging.basicConfig(format='%(asctime)s - %(message)s', |
| datefmt='%Y-%m-%d %H:%M:%S', |
| level=logging.INFO, |
| handlers=[LoggingHandler()]) |
| |
|
|
|
|
| |
| model_name = sys.argv[1] if len(sys.argv) > 1 else 'bert-base-uncased' |
| top_k = int(sys.argv[2]) if len(sys.argv) > 2 else 3 |
|
|
| batch_size = 16 |
| num_epochs = 1 |
| max_seq_length = 128 |
|
|
| |
|
|
| |
| sts_dataset_path = 'datasets/stsbenchmark.tsv.gz' |
|
|
| if not os.path.exists(sts_dataset_path): |
| util.http_get('https://sbert.net/datasets/stsbenchmark.tsv.gz', sts_dataset_path) |
|
|
| cross_encoder_path = 'output/cross-encoder/stsb_indomain_'+model_name.replace("/", "-")+'-'+datetime.now().strftime("%Y-%m-%d_%H-%M-%S") |
| bi_encoder_path = 'output/bi-encoder/stsb_augsbert_SS_'+model_name.replace("/", "-")+'-'+datetime.now().strftime("%Y-%m-%d_%H-%M-%S") |
|
|
| |
| logging.info("Loading cross-encoder model: {}".format(model_name)) |
| |
| cross_encoder = CrossEncoder(model_name, num_labels=1) |
|
|
|
|
| |
| logging.info("Loading bi-encoder model: {}".format(model_name)) |
| |
| word_embedding_model = models.Transformer(model_name, max_seq_length=max_seq_length) |
|
|
| |
| pooling_model = models.Pooling(word_embedding_model.get_word_embedding_dimension(), |
| pooling_mode_mean_tokens=True, |
| pooling_mode_cls_token=False, |
| pooling_mode_max_tokens=False) |
|
|
| bi_encoder = SentenceTransformer(modules=[word_embedding_model, pooling_model]) |
|
|
|
|
| |
| |
| |
| |
| |
|
|
| logging.info("Step 1: Train cross-encoder: {} with STSbenchmark (gold dataset)".format(model_name)) |
|
|
| gold_samples = [] |
| dev_samples = [] |
| test_samples = [] |
|
|
| with gzip.open(sts_dataset_path, 'rt', encoding='utf8') as fIn: |
| reader = csv.DictReader(fIn, delimiter='\t', quoting=csv.QUOTE_NONE) |
| for row in reader: |
| score = float(row['score']) / 5.0 |
|
|
| if row['split'] == 'dev': |
| dev_samples.append(InputExample(texts=[row['sentence1'], row['sentence2']], label=score)) |
| elif row['split'] == 'test': |
| test_samples.append(InputExample(texts=[row['sentence1'], row['sentence2']], label=score)) |
| else: |
| |
| gold_samples.append(InputExample(texts=[row['sentence1'], row['sentence2']], label=score)) |
| gold_samples.append(InputExample(texts=[row['sentence2'], row['sentence1']], label=score)) |
|
|
|
|
| |
| train_dataloader = DataLoader(gold_samples, shuffle=True, batch_size=batch_size) |
|
|
|
|
| |
| evaluator = CECorrelationEvaluator.from_input_examples(dev_samples, name='sts-dev') |
|
|
| |
| warmup_steps = math.ceil(len(train_dataloader) * num_epochs * 0.1) |
| logging.info("Warmup-steps: {}".format(warmup_steps)) |
|
|
| |
| cross_encoder.fit(train_dataloader=train_dataloader, |
| evaluator=evaluator, |
| epochs=num_epochs, |
| evaluation_steps=1000, |
| warmup_steps=warmup_steps, |
| output_path=cross_encoder_path) |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
| logging.info("Step 2.1: Generate STSbenchmark (silver dataset) using pretrained SBERT \ |
| model and top-{} semantic search combinations".format(top_k)) |
|
|
| silver_data = [] |
| sentences = set() |
|
|
| for sample in gold_samples: |
| sentences.update(sample.texts) |
|
|
| sentences = list(sentences) |
| sent2idx = {sentence: idx for idx, sentence in enumerate(sentences)} |
| duplicates = set((sent2idx[data.texts[0]], sent2idx[data.texts[1]]) for data in gold_samples) |
|
|
|
|
| |
| semantic_model_name = 'paraphrase-MiniLM-L6-v2' |
| semantic_search_model = SentenceTransformer(semantic_model_name) |
| logging.info("Encoding unique sentences with semantic search model: {}".format(semantic_model_name)) |
|
|
| |
| embeddings = semantic_search_model.encode(sentences, batch_size=batch_size, convert_to_tensor=True) |
|
|
| logging.info("Retrieve top-{} with semantic search model: {}".format(top_k, semantic_model_name)) |
|
|
| |
| progress = tqdm.tqdm(unit="docs", total=len(sent2idx)) |
| for idx in range(len(sentences)): |
| sentence_embedding = embeddings[idx] |
| cos_scores = util.cos_sim(sentence_embedding, embeddings)[0] |
| cos_scores = cos_scores.cpu() |
| progress.update(1) |
|
|
| |
| top_results = torch.topk(cos_scores, k=top_k+1) |
| |
| for score, iid in zip(top_results[0], top_results[1]): |
| if iid != idx and (iid, idx) not in duplicates: |
| silver_data.append((sentences[idx], sentences[iid])) |
| duplicates.add((idx,iid)) |
|
|
| progress.reset() |
| progress.close() |
|
|
| logging.info("Length of silver_dataset generated: {}".format(len(silver_data))) |
| logging.info("Step 2.2: Label STSbenchmark (silver dataset) with cross-encoder: {}".format(model_name)) |
| cross_encoder = CrossEncoder(cross_encoder_path) |
| silver_scores = cross_encoder.predict(silver_data) |
|
|
| |
| assert all(0.0 <= score <= 1.0 for score in silver_scores) |
|
|
| |
| |
| |
| |
| |
|
|
| logging.info("Step 3: Train bi-encoder: {} with STSbenchmark (gold + silver dataset)".format(model_name)) |
|
|
| |
| logging.info("Read STSbenchmark gold and silver train dataset") |
| silver_samples = list(InputExample(texts=[data[0], data[1]], label=score) for \ |
| data, score in zip(silver_data, silver_scores)) |
|
|
|
|
| train_dataloader = DataLoader(gold_samples + silver_samples, shuffle=True, batch_size=batch_size) |
| train_loss = losses.CosineSimilarityLoss(model=bi_encoder) |
|
|
| logging.info("Read STSbenchmark dev dataset") |
| evaluator = EmbeddingSimilarityEvaluator.from_input_examples(dev_samples, name='sts-dev') |
|
|
| |
| warmup_steps = math.ceil(len(train_dataloader) * num_epochs * 0.1) |
| logging.info("Warmup-steps: {}".format(warmup_steps)) |
|
|
| |
| bi_encoder.fit(train_objectives=[(train_dataloader, train_loss)], |
| evaluator=evaluator, |
| epochs=num_epochs, |
| evaluation_steps=1000, |
| warmup_steps=warmup_steps, |
| output_path=bi_encoder_path |
| ) |
|
|
| |
| |
| |
| |
| |
|
|
| |
| bi_encoder = SentenceTransformer(bi_encoder_path) |
| test_evaluator = EmbeddingSimilarityEvaluator.from_input_examples(test_samples, name='sts-test') |
| test_evaluator(bi_encoder, output_path=bi_encoder_path) |