Views
No views yet
1 from transformers import AutoTokenizer, AutoModelForSequenceClassification
2
3# Load the fine-tuned model
4
5
6model = AutoModelForSequenceClassification.from_pretrained("./paraphraser_model")
7tokenizer = AutoTokenizer.from_pretrained("./paraphraser_model")
8
9sentences = ["The quick brown fox jumps over the lazy dog.", "A fast dark-colored fox leaps over a sleeping dog."]
10encoded_input = tokenizer(sentences[0], sentences[1], return_tensors="pt", truncation=True, padding='max_length', max_length=128)
11
12# Compute Similarity Score:
13
14import torch
15import torch.nn.functional as F
16
17# Perform inference
18with torch.no_grad():
19 model_output = model(**encoded_input)
20 logits = model_output.logits
21 similarity_score = F.sigmoid(logits).item()
22
23print(f"Similarity score between the two sentences: {similarity_score}")
24
25# Mean Pooling Function:
26
27If using the model for generating sentence embeddings, you can use the following mean pooling function:
28 def mean_pooling(model_output, attention_mask):
29 token_embeddings = model_output[0] # First element of model_output contains the token embeddings
30 input_mask_expanded = attention_mask.unsqueeze(-1).float()
31 sum_embeddings = torch.sum(token_embeddings * input_mask_expanded, dim=1)
32 sum_mask = torch.clamp(input_mask_expanded.sum(dim=1), min=1e-9)
33 return sum_embeddings / sum_mask
34
35# Limitations
36Domain Specificity: The model is fine-tuned on the mteb/stsbenchmark-sts dataset and may perform differently on other types of text or datasets.
37Biases: As with any model trained on human language data, it may inherit and reflect biases present in the training data.
38
39# Future Work
40Potential improvements include fine-tuning on additional datasets, experimenting with different architectures or hyperparameters, and incorporating additional training techniques to improve performance and robustness.
41
42Citation
43If you use this model in your research, please cite it as follows:
44 @inproceedings{your_paper,
45 title={Fine-Tuned Paraphrase-Multilingual-MiniLM-L12-v2 for Sentence Similarity},
46 author={Your Name},
47 year={2024},
48 publisher={Your Institution}
49}
50
51
52# License
53This model is licensed under the MIT License. See the LICENSE file for more information.