vahidthegreat
commited on
Commit
•
fabd526
1
Parent(s):
86c2ace
Update README.md
Browse files
README.md
CHANGED
@@ -31,19 +31,16 @@ First, ensure you have the required libraries installed:
|
|
31 |
```bash
|
32 |
!pip install peft transformers sentence-transformers torch
|
33 |
|
34 |
-
|
35 |
-
from transformers import
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
# Load and apply LoRA weights
|
46 |
-
lora_model = PeftModel.from_pretrained(base_model, "vahidthegreat/StanceAware-SBERT")
|
47 |
```
|
48 |
|
49 |
|
@@ -52,32 +49,39 @@ lora_model = PeftModel.from_pretrained(base_model, "vahidthegreat/StanceAware-SB
|
|
52 |
|
53 |
The following custom `SiameseNetworkMPNet` class leverages the model for stance detection tasks. It pools embeddings and normalizes them for similarity calculations. This is for the sake of replicability of our exact results. But the model would work without this as well.
|
54 |
```bash
|
55 |
-
import torch
|
56 |
-
import torch.nn as nn
|
57 |
-
import torch.nn.functional as F
|
58 |
-
|
59 |
-
# Custom Siamese Network class
|
60 |
class SiameseNetworkMPNet(nn.Module):
|
61 |
def __init__(self, model_name, tokenizer, normalize=True):
|
62 |
super(SiameseNetworkMPNet, self).__init__()
|
63 |
-
|
64 |
-
|
65 |
-
self.model = AutoModel.from_pretrained(model_name)
|
66 |
-
self.model = PeftModel.from_pretrained(self.model, "vahidthegreat/StanceAware-SBERT")
|
67 |
self.normalize = normalize
|
68 |
self.tokenizer = tokenizer
|
69 |
|
70 |
def forward(self, **inputs):
|
71 |
model_output = self.model(**inputs)
|
72 |
attention_mask = inputs['attention_mask']
|
73 |
-
last_hidden_states = model_output.last_hidden_state #
|
74 |
-
embeddings = torch.sum(last_hidden_states * attention_mask.unsqueeze(-1), 1) / torch.clamp(attention_mask.sum(1, keepdim=True), min=1e-9)
|
75 |
-
|
76 |
if self.normalize:
|
77 |
embeddings = F.layer_norm(embeddings, embeddings.shape[1:])
|
78 |
embeddings = F.normalize(embeddings, p=2, dim=1)
|
79 |
|
80 |
return embeddings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
```
|
82 |
|
83 |
|
@@ -105,9 +109,10 @@ def two_sentence_similarity(model, tokenizer, text1, text2):
|
|
105 |
text1 = "I love pineapple on pizza"
|
106 |
text2 = "I hate pineapple on pizza"
|
107 |
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
111 |
```
|
112 |
|
113 |
|
|
|
31 |
```bash
|
32 |
!pip install peft transformers sentence-transformers torch
|
33 |
|
34 |
+
from peft import LoraConfig, get_peft_model, prepare_model_for_int8_training, TaskType, PeftModel # peft-0.7.1
|
35 |
+
from transformers import (
|
36 |
+
AutoModel,
|
37 |
+
AutoTokenizer,
|
38 |
+
BitsAndBytesConfig,
|
39 |
+
HfArgumentParser,
|
40 |
+
AutoTokenizer,
|
41 |
+
TrainingArguments,
|
42 |
+
AutoConfig,
|
43 |
+
)
|
|
|
|
|
|
|
44 |
```
|
45 |
|
46 |
|
|
|
49 |
|
50 |
The following custom `SiameseNetworkMPNet` class leverages the model for stance detection tasks. It pools embeddings and normalizes them for similarity calculations. This is for the sake of replicability of our exact results. But the model would work without this as well.
|
51 |
```bash
|
|
|
|
|
|
|
|
|
|
|
52 |
class SiameseNetworkMPNet(nn.Module):
|
53 |
def __init__(self, model_name, tokenizer, normalize=True):
|
54 |
super(SiameseNetworkMPNet, self).__init__()
|
55 |
+
|
56 |
+
self.model = AutoModel.from_pretrained(model_name)#, quantization_config=bnb_config, trust_remote_code=True)
|
|
|
|
|
57 |
self.normalize = normalize
|
58 |
self.tokenizer = tokenizer
|
59 |
|
60 |
def forward(self, **inputs):
|
61 |
model_output = self.model(**inputs)
|
62 |
attention_mask = inputs['attention_mask']
|
63 |
+
last_hidden_states = model_output.last_hidden_state # First element of model_output contains all token embeddings
|
64 |
+
embeddings = torch.sum(last_hidden_states * attention_mask.unsqueeze(-1), 1) / torch.clamp(attention_mask.sum(1, keepdim=True), min=1e-9) # mean_pooling
|
|
|
65 |
if self.normalize:
|
66 |
embeddings = F.layer_norm(embeddings, embeddings.shape[1:])
|
67 |
embeddings = F.normalize(embeddings, p=2, dim=1)
|
68 |
|
69 |
return embeddings
|
70 |
+
|
71 |
+
|
72 |
+
base_model_name = "sentence-transformers/all-mpnet-base-v2"
|
73 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_name)
|
74 |
+
|
75 |
+
# Load the base model
|
76 |
+
base_model = SiameseNetworkMPNet(model_name=base_model_name, tokenizer=tokenizer)
|
77 |
+
|
78 |
+
# Load and apply LoRA weights
|
79 |
+
lora_model = SiameseNetworkMPNet(model_name=base_model_name, tokenizer=tokenizer)
|
80 |
+
lora_model = PeftModel.from_pretrained(base_model_copy, "vahidthegreat/StanceAware-SBERT")
|
81 |
+
lora_model = lora_model.merge_and_unload()
|
82 |
+
|
83 |
+
base_model.eval()
|
84 |
+
lora_model.eval()
|
85 |
```
|
86 |
|
87 |
|
|
|
109 |
text1 = "I love pineapple on pizza"
|
110 |
text2 = "I hate pineapple on pizza"
|
111 |
|
112 |
+
print(f"For Base Model sentences: '{text1}' and '{text2}'")
|
113 |
+
two_sentence_similarity(base_model, tokenizer, text1, text2)
|
114 |
+
print(f"\n\nFor FineTuned Model sentences: '{text1}' and '{text2}'")
|
115 |
+
two_sentence_similarity(lora_model, tokenizer, text1, text2)
|
116 |
```
|
117 |
|
118 |
|