vahidthegreat commited on
Commit
937277a
1 Parent(s): b821c51

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +142 -3
README.md CHANGED
@@ -1,3 +1,142 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ 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
13
+
14
+ ## Model Overview
15
+
16
+ This model is fine-tuned on top of `sentence-transformers/all-mpnet-base-v2` to differentiate between opposing viewpoints on the same topic. Traditional sentence transformers group topically similar texts together but struggle to recognize nuanced differences in stance—such as differentiating the opinions "I love pineapple on pizza" and "I hate pineapple on pizza." This stance-aware sentence transformer addresses this limitation by fine-tuning with arguments both for and against controversial topics, making it especially useful for applications in social computing, such as **opinion mining** and **stance detection**.
17
+
18
+ ### Research Background
19
+
20
+ As explained in our [EMNLP 2024 paper](https://example-link-to-paper), this model was fine-tuned using contrastive learning on human-generated arguments for and against various claims. This technique enhances sentence transformers' ability to capture sentiment and stance in opinionated texts while retaining computational efficiency over classification-based approaches.
21
+
22
+ ## Model Use and Code Instructions
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 is a step-by-step guide on loading and applying the LoRA weights to `sentence-transformers/all-mpnet-base-v2`.
27
+
28
+ #### Requirements
29
+ First, ensure you have the required libraries installed:
30
+
31
+ ```bash
32
+ pip install peft transformers sentence-transformers torch
33
+
34
+
35
+ from transformers import AutoModel, AutoTokenizer
36
+ from peft import PeftModel
37
+
38
+ # Load the base model
39
+ base_model_name = "sentence-transformers/all-mpnet-base-v2"
40
+ base_model = AutoModel.from_pretrained(base_model_name)
41
+
42
+ # Load the tokenizer
43
+ tokenizer = AutoTokenizer.from_pretrained(base_model_name)
44
+
45
+ # Load and apply LoRA weights
46
+ lora_model = PeftModel.from_pretrained(base_model, "vahidthegreat/StanceAware-SBERT")
47
+
48
+
49
+
50
+
51
+ ## Using the Model with the Siamese Network Class
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
+
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
+ # Initialize with LoRA-applied model
65
+ self.model = AutoModel.from_pretrained(model_name)
66
+ self.model = PeftModel.from_pretrained(self.model, "username/LoRA_MPNet_contriplet_weights")
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 # Token embeddings
74
+ embeddings = torch.sum(last_hidden_states * attention_mask.unsqueeze(-1), 1) / torch.clamp(attention_mask.sum(1, keepdim=True), min=1e-9) # Mean pooling
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
+
84
+ ## Example Usage for Two-Sentence Similarity
85
+ The following example shows how to use the Siamese network with two input sentences, calculating cosine similarity to compare stances.
86
+
87
+
88
+ from sklearn.metrics.pairwise import cosine_similarity
89
+
90
+ def two_sentence_similarity(model, tokenizer, text1, text2):
91
+ # Tokenize both texts
92
+ tokens1 = tokenizer(text1, return_tensors="pt", max_length=128, truncation=True, padding="max_length")
93
+ tokens2 = tokenizer(text2, return_tensors="pt", max_length=128, truncation=True, padding="max_length")
94
+
95
+ # Generate embeddings
96
+ embeddings1 = model(**tokens1).detach().cpu().numpy()
97
+ embeddings2 = model(**tokens2).detach().cpu().numpy()
98
+
99
+ # Compute cosine similarity
100
+ similarity = cosine_similarity(embeddings1, embeddings2)
101
+ print(f"Cosine Similarity: {similarity[0][0]}")
102
+ return similarity[0][0]
103
+
104
+ # Example sentences
105
+ text1 = "I love pineapple on pizza"
106
+ text2 = "I hate pineapple on pizza"
107
+
108
+ # Instantiate model and tokenizer
109
+ stance_model = SiameseNetworkMPNet(model_name=base_model_name, tokenizer=tokenizer)
110
+ two_sentence_similarity(stance_model, tokenizer, text1, text2)
111
+
112
+
113
+
114
+
115
+ ## Key Applications
116
+
117
+ This stance-aware sentence transformer model can be applied to various fields within social computing and opinion analysis. Here are some key applications:
118
+
119
+ - **Opinion Mining**: Extracting stances or sentiments on topics from a large corpus of texts.
120
+ - **Stance Detection**: Identifying whether statements are in favor of or against a specific claim.
121
+ - **Social and Political Discourse Analysis**: Useful for studying polarizing issues in social science research, particularly for nuanced or controversial topics.
122
+
123
+ ## Limitations
124
+
125
+ While this model enhances stance detection capabilities, it may still encounter challenges with:
126
+ - **Nuanced or Ambiguous Statements**: For extremely context-dependent or subtle differences in stance, additional fine-tuning may be required.
127
+ - **Complex Multi-Sentence Arguments**: In cases where multiple arguments or perspectives are embedded within a single text, further customization or model adjustments may improve accuracy.
128
+
129
+ ## Citation
130
+
131
+ If you use this model in your research, please cite our paper:
132
+
133
+ ```bibtex
134
+ @inproceedings{ghafouri2024stance,
135
+ title={I love pineapple on pizza != I hate pineapple on pizza: Stance-Aware Sentence Transformers for Opinion Mining},
136
+ author={Ghafouri, Vahid and Such, Jose and Suarez-Tangil, Guillermo},
137
+ booktitle={Proceedings of the 2024 Conference on Empirical Methods in Natural Language Processing (EMNLP)},
138
+ year={2024},
139
+ month={November 14},
140
+ url={https://hdl.handle.net/20.500.12761/1851},
141
+ note={Available online at https://hdl.handle.net/20.500.12761/1851}
142
+ }