Tom Aarsen
commited on
Commit
•
09c7bda
1
Parent(s):
44e7110
Update safetensors keys; update README usage
Browse files- README.md +56 -29
- adapter_config.json +3 -3
- adapter_model.safetensors +2 -2
README.md
CHANGED
@@ -3,10 +3,13 @@ language: en
|
|
3 |
license: apache-2.0
|
4 |
tags:
|
5 |
- sentence-transformers
|
|
|
6 |
- opinion-mining
|
7 |
- stance-detection
|
8 |
- social-computing
|
9 |
- LoRA
|
|
|
|
|
10 |
---
|
11 |
|
12 |
# Stance-Aware Sentence Transformers for Opinion Mining
|
@@ -23,43 +26,72 @@ As explained in our [EMNLP 2024 paper](https://scholar.google.com/citations?view
|
|
23 |
|
24 |
### Loading and Using the Model with LoRA Weights
|
25 |
|
26 |
-
Since this model leverages LoRA (Low-Rank Adaptation) fine-tuning, you only need to download the lightweight LoRA weights and apply them to the base model. Below
|
27 |
|
28 |
-
####
|
29 |
-
First, ensure you have the required libraries installed:
|
30 |
|
31 |
```bash
|
32 |
-
|
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 |
-
import torch
|
45 |
-
import torch.nn as nn
|
46 |
-
import torch.nn.functional as F
|
47 |
```
|
48 |
|
|
|
|
|
|
|
49 |
|
|
|
|
|
50 |
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
class SiameseNetworkMPNet(nn.Module):
|
56 |
def __init__(self, model_name, tokenizer, normalize=True):
|
57 |
super(SiameseNetworkMPNet, self).__init__()
|
58 |
|
59 |
-
self.model = AutoModel.from_pretrained(model_name)
|
60 |
self.normalize = normalize
|
61 |
self.tokenizer = tokenizer
|
62 |
|
|
|
|
|
|
|
|
|
|
|
63 |
def forward(self, **inputs):
|
64 |
model_output = self.model(**inputs)
|
65 |
attention_mask = inputs['attention_mask']
|
@@ -80,18 +112,13 @@ base_model = SiameseNetworkMPNet(model_name=base_model_name, tokenizer=tokenizer
|
|
80 |
|
81 |
# Load and apply LoRA weights
|
82 |
lora_model = SiameseNetworkMPNet(model_name=base_model_name, tokenizer=tokenizer)
|
83 |
-
lora_model
|
84 |
-
lora_model = lora_model.merge_and_unload()
|
85 |
-
|
86 |
-
base_model.eval()
|
87 |
-
lora_model.eval()
|
88 |
```
|
89 |
|
90 |
-
|
91 |
-
## Example Usage for Two-Sentence Similarity
|
92 |
The following example shows how to use the Siamese network with two input sentences, calculating cosine similarity to compare stances.
|
93 |
|
94 |
-
```
|
95 |
from sklearn.metrics.pairwise import cosine_similarity
|
96 |
|
97 |
def two_sentence_similarity(model, tokenizer, text1, text2):
|
|
|
3 |
license: apache-2.0
|
4 |
tags:
|
5 |
- sentence-transformers
|
6 |
+
- transformers
|
7 |
- opinion-mining
|
8 |
- stance-detection
|
9 |
- social-computing
|
10 |
- LoRA
|
11 |
+
base_model: sentence-transformers/all-mpnet-base-v2
|
12 |
+
library_name: peft
|
13 |
---
|
14 |
|
15 |
# Stance-Aware Sentence Transformers for Opinion Mining
|
|
|
26 |
|
27 |
### Loading and Using the Model with LoRA Weights
|
28 |
|
29 |
+
Since this model leverages LoRA (Low-Rank Adaptation) fine-tuning, you only need to download the lightweight LoRA weights and apply them to the base model. Below are two guides on loading and applying the LoRA weights to `sentence-transformers/all-mpnet-base-v2` based on Sentence Transformers and Transformers, respectively.
|
30 |
|
31 |
+
#### with Sentence Transformers
|
|
|
32 |
|
33 |
```bash
|
34 |
+
pip install peft sentence-transformers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
```
|
36 |
|
37 |
+
```python
|
38 |
+
from sentence_transformers import SentenceTransformer
|
39 |
+
from peft import PeftModel
|
40 |
|
41 |
+
model = SentenceTransformer("all-mpnet-base-v2")
|
42 |
+
model[0].auto_model = PeftModel.from_pretrained(model[0].auto_model, "vahidthegreat/StanceAware-SBERT")
|
43 |
|
44 |
+
sentences = [
|
45 |
+
"I love pineapple on pizza",
|
46 |
+
"I hate pineapple on pizza",
|
47 |
+
"I like pineapple on pizza",
|
48 |
+
]
|
49 |
+
embeddings = model.encode(sentences)
|
50 |
+
print(embeddings.shape)
|
51 |
+
|
52 |
+
similarity = model.similarity(embeddings, embeddings)
|
53 |
+
print(similarity)
|
54 |
+
```
|
55 |
+
|
56 |
+
```output
|
57 |
+
(3, 768)
|
58 |
+
tensor([[1.0000, 0.5732, 0.9713],
|
59 |
+
[0.5732, 1.0000, 0.5804],
|
60 |
+
[0.9713, 0.5804, 1.0000]])
|
61 |
+
```
|
62 |
+
From these results we can see that the first and third sentence are very similar, while the second sentence is less similar to the other two.
|
63 |
+
|
64 |
+
#### with Transformers
|
65 |
+
First, ensure you have the required libraries installed:
|
66 |
|
|
|
67 |
```bash
|
68 |
+
pip install peft transformers torch
|
69 |
+
```
|
70 |
+
|
71 |
+
#### Using the Model with the Siamese Network Class
|
72 |
+
|
73 |
+
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.
|
74 |
+
|
75 |
+
```python
|
76 |
+
from peft import PeftModel
|
77 |
+
from transformers import AutoModel, AutoTokenizer
|
78 |
+
import torch
|
79 |
+
import torch.nn as nn
|
80 |
+
import torch.nn.functional as F
|
81 |
+
|
82 |
class SiameseNetworkMPNet(nn.Module):
|
83 |
def __init__(self, model_name, tokenizer, normalize=True):
|
84 |
super(SiameseNetworkMPNet, self).__init__()
|
85 |
|
86 |
+
self.model = AutoModel.from_pretrained(model_name)
|
87 |
self.normalize = normalize
|
88 |
self.tokenizer = tokenizer
|
89 |
|
90 |
+
def apply_lora_weights(self, lora_model):
|
91 |
+
self.model = PeftModel.from_pretrained(self.model, lora_model)
|
92 |
+
self.model = self.model.merge_and_unload()
|
93 |
+
return self
|
94 |
+
|
95 |
def forward(self, **inputs):
|
96 |
model_output = self.model(**inputs)
|
97 |
attention_mask = inputs['attention_mask']
|
|
|
112 |
|
113 |
# Load and apply LoRA weights
|
114 |
lora_model = SiameseNetworkMPNet(model_name=base_model_name, tokenizer=tokenizer)
|
115 |
+
lora_model.apply_lora_weights("vahidthegreat/StanceAware-SBERT")
|
|
|
|
|
|
|
|
|
116 |
```
|
117 |
|
118 |
+
#### Example Usage for Two-Sentence Similarity
|
|
|
119 |
The following example shows how to use the Siamese network with two input sentences, calculating cosine similarity to compare stances.
|
120 |
|
121 |
+
```python
|
122 |
from sklearn.metrics.pairwise import cosine_similarity
|
123 |
|
124 |
def two_sentence_similarity(model, tokenizer, text1, text2):
|
adapter_config.json
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
{
|
2 |
"alpha_pattern": {},
|
3 |
"auto_mapping": {
|
4 |
-
"base_model_class": "
|
5 |
-
"parent_library": "
|
6 |
},
|
7 |
-
"base_model_name_or_path":
|
8 |
"bias": "none",
|
9 |
"fan_in_fan_out": false,
|
10 |
"inference_mode": true,
|
|
|
1 |
{
|
2 |
"alpha_pattern": {},
|
3 |
"auto_mapping": {
|
4 |
+
"base_model_class": "MPNetModel",
|
5 |
+
"parent_library": "transformers.models.mpnet.modeling_mpnet"
|
6 |
},
|
7 |
+
"base_model_name_or_path": "sentence-transformers/all-mpnet-base-v2",
|
8 |
"bias": "none",
|
9 |
"fan_in_fan_out": false,
|
10 |
"inference_mode": true,
|
adapter_model.safetensors
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bb14ba34a5419b21cc4abf7bd50f4d3d4c009ef36bd9c95640a251e49d3fca9e
|
3 |
+
size 9449888
|