Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
language: "en"
|
3 |
+
tags:
|
4 |
+
- stance-detection
|
5 |
+
- election2020
|
6 |
+
license: "gpl-3.0"
|
7 |
+
---
|
8 |
+
|
9 |
+
# Pre-trained BERT on Twitter US Election 2020 for Stance Detection towards Donald Trump (KE-MLM)
|
10 |
+
|
11 |
+
Pre-trained weights for **KE-MLM model** in [Knowledge Enhance Masked Language Model for Stance Detection](https://2021.naacl.org/program/accepted/), NAACL 2021.
|
12 |
+
|
13 |
+
# Training Data
|
14 |
+
|
15 |
+
This model is pre-trained on over 5 million English tweets about the 2020 US Presidential Election. Then fine-tuned using our [stance-labeled data](https://github.com/GU-DataLab/stance-detection-KE-MLM) for stance detection towards Donald Trump.
|
16 |
+
|
17 |
+
# Training Objective
|
18 |
+
|
19 |
+
This model is initialized with BERT-base and trained with normal MLM objective with classification layer fine-tuned for stance detection towards Donald Trump.
|
20 |
+
|
21 |
+
# Usage
|
22 |
+
|
23 |
+
This pre-trained language model is fine-tuned to the stance detection task specifically for Donald Trump.
|
24 |
+
|
25 |
+
Please see the [official repository](https://github.com/GU-DataLab/stance-detection-KE-MLM) for more detail.
|
26 |
+
|
27 |
+
```python
|
28 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
29 |
+
import torch
|
30 |
+
import numpy as np
|
31 |
+
|
32 |
+
# choose GPU if available
|
33 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
34 |
+
|
35 |
+
# select mode path here
|
36 |
+
pretrained_LM_path = "kornosk/bert-election2020-twitter-stance-trump-KE-MLM"
|
37 |
+
|
38 |
+
# load model
|
39 |
+
tokenizer = AutoTokenizer.from_pretrained(pretrained_LM_path)
|
40 |
+
model = AutoModelForSequenceClassification.from_pretrained(pretrained_LM_path)
|
41 |
+
|
42 |
+
id2label = {
|
43 |
+
0: "AGAINST",
|
44 |
+
1: "FAVOR",
|
45 |
+
2: "NONE"
|
46 |
+
}
|
47 |
+
|
48 |
+
##### Prediction Neutral #####
|
49 |
+
sentence = "Hello World."
|
50 |
+
inputs = tokenizer(sentence.lower(), return_tensors="pt")
|
51 |
+
outputs = model(**inputs)
|
52 |
+
predicted_probability = torch.softmax(outputs[0], dim=1)[0].tolist()
|
53 |
+
|
54 |
+
print("Sentence:", sentence)
|
55 |
+
print("Prediction:", id2label[np.argmax(predicted_probability)])
|
56 |
+
print("Against:", predicted_probability[0])
|
57 |
+
print("Favor:", predicted_probability[1])
|
58 |
+
print("Neutral:", predicted_probability[2])
|
59 |
+
|
60 |
+
##### Prediction Favor #####
|
61 |
+
sentence = "Go Go Trump!!!"
|
62 |
+
inputs = tokenizer(sentence.lower(), return_tensors="pt")
|
63 |
+
outputs = model(**inputs)
|
64 |
+
predicted_probability = torch.softmax(outputs[0], dim=1)[0].tolist()
|
65 |
+
|
66 |
+
print("Sentence:", sentence)
|
67 |
+
print("Prediction:", id2label[np.argmax(predicted_probability)])
|
68 |
+
print("Against:", predicted_probability[0])
|
69 |
+
print("Favor:", predicted_probability[1])
|
70 |
+
print("Neutral:", predicted_probability[2])
|
71 |
+
|
72 |
+
##### Prediction Against #####
|
73 |
+
sentence = "Trump is the worst."
|
74 |
+
inputs = tokenizer(sentence.lower(), return_tensors="pt")
|
75 |
+
outputs = model(**inputs)
|
76 |
+
predicted_probability = torch.softmax(outputs[0], dim=1)[0].tolist()
|
77 |
+
|
78 |
+
print("Sentence:", sentence)
|
79 |
+
print("Prediction:", id2label[np.argmax(predicted_probability)])
|
80 |
+
print("Against:", predicted_probability[0])
|
81 |
+
print("Favor:", predicted_probability[1])
|
82 |
+
print("Neutral:", predicted_probability[2])
|
83 |
+
|
84 |
+
# please consider citing our paper if you feel this is useful :)
|
85 |
+
```
|
86 |
+
|
87 |
+
# Reference
|
88 |
+
|
89 |
+
- [Knowledge Enhance Masked Language Model for Stance Detection](https://2021.naacl.org/program/accepted/), NAACL 2021.
|
90 |
+
|
91 |
+
# Citation
|
92 |
+
```bibtex
|
93 |
+
@inproceedings{kawintiranon2021knowledge,
|
94 |
+
title={Knowledge Enhanced Masked Language Model for Stance Detection},
|
95 |
+
author={Kawintiranon, Kornraphop and Singh, Lisa},
|
96 |
+
booktitle={Proceedings of the 2021 Annual Conference of the North American Chapter of the Association for Computational Linguistics (NAACL)},
|
97 |
+
year={2021},
|
98 |
+
url={#}
|
99 |
+
}
|
100 |
+
```
|