Upload README.md with huggingface_hub
Browse files
README.md
CHANGED
@@ -36,6 +36,71 @@ Training parameters included:
|
|
36 |
For detailed information on the training process, please refer to the [model's
|
37 |
paper](https://aclanthology.org/2023.findings-emnlp.441/).
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
# Datasets
|
40 |
The model was trained on the [ISHate dataset](https://huggingface.co/datasets/BenjaminOcampo/ISHate), specifically
|
41 |
the training part of the dataset which focuses on implicit hate speech.
|
|
|
36 |
For detailed information on the training process, please refer to the [model's
|
37 |
paper](https://aclanthology.org/2023.findings-emnlp.441/).
|
38 |
|
39 |
+
# Usage
|
40 |
+
|
41 |
+
First you might need the transformers version 4.30.2.
|
42 |
+
|
43 |
+
```
|
44 |
+
pip install transformers==4.30.2
|
45 |
+
```
|
46 |
+
|
47 |
+
This model was created using pytorch vanilla. In order to load it you have to use the following Model Class.
|
48 |
+
|
49 |
+
```python
|
50 |
+
class ContrastiveModel(nn.Module):
|
51 |
+
def __init__(self, model):
|
52 |
+
super(ContrastiveModel, self).__init__()
|
53 |
+
self.model = model
|
54 |
+
self.embedding_dim = model.config.hidden_size
|
55 |
+
self.fc = nn.Linear(self.embedding_dim, self.embedding_dim)
|
56 |
+
self.classifier = nn.Linear(self.embedding_dim, 2) # Classification layer
|
57 |
+
|
58 |
+
def forward(self, input_ids, attention_mask):
|
59 |
+
outputs = self.model(input_ids, attention_mask)
|
60 |
+
embeddings = outputs.last_hidden_state[:, 0] # Use the CLS token embedding as the representation
|
61 |
+
embeddings = self.fc(embeddings)
|
62 |
+
logits = self.classifier(embeddings) # Apply classification layer
|
63 |
+
|
64 |
+
return embeddings, logits
|
65 |
+
```
|
66 |
+
|
67 |
+
Then, we instantiate the model as:
|
68 |
+
|
69 |
+
```python
|
70 |
+
from transformers import AutoModel, AutoTokenizer, AutoConfig
|
71 |
+
|
72 |
+
repo_name = "BenjaminOcampo/peace_cont_bert"
|
73 |
+
|
74 |
+
config = AutoConfig.from_pretrained(repo_name)
|
75 |
+
contrastive_model = ContrastiveModel(AutoModel.from_config(config))
|
76 |
+
tokenizer = AutoTokenizer.from_pretrained(repo_name)
|
77 |
+
```
|
78 |
+
|
79 |
+
Finally, to load the weights of the model we do as follows:
|
80 |
+
|
81 |
+
```python
|
82 |
+
model_tmp_file = hf_hub_download(repo_id=repo_name, filename="model.pt", token=read_token)
|
83 |
+
|
84 |
+
state_dict = torch.load(model_tmp_file)
|
85 |
+
|
86 |
+
contrastive_model.load_state_dict(state_dict)
|
87 |
+
```
|
88 |
+
|
89 |
+
You can make predictions as any pytorch model:
|
90 |
+
|
91 |
+
```
|
92 |
+
import torch
|
93 |
+
|
94 |
+
text = "Are you sure that Islam is a peaceful religion?"
|
95 |
+
inputs = tokenizer(text, return_tensors="pt")
|
96 |
+
|
97 |
+
with torch.no_grad():
|
98 |
+
_, logits = contrastive_model(inputs["input_ids"], inputs["attention_mask"])
|
99 |
+
|
100 |
+
probabilities = torch.softmax(logits, dim=1)
|
101 |
+
_, predicted_labels = torch.max(probabilities, dim=1)
|
102 |
+
```
|
103 |
+
|
104 |
# Datasets
|
105 |
The model was trained on the [ISHate dataset](https://huggingface.co/datasets/BenjaminOcampo/ISHate), specifically
|
106 |
the training part of the dataset which focuses on implicit hate speech.
|