Upload folder using huggingface_hub
Browse files- README.md +114 -3
- config.json +1 -0
- modeling_cnn_sent.py +46 -0
- sent_pytorch_model.bin +3 -0
- sentencepiece.bpe.model +3 -0
- special_tokens_map.json +51 -0
- tokenizer.json +0 -0
- tokenizer_config.json +56 -0
README.md
CHANGED
@@ -1,3 +1,114 @@
|
|
1 |
-
---
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
tags:
|
3 |
+
- pytorch
|
4 |
+
- sentiment-analysis
|
5 |
+
- yoruba
|
6 |
+
- cnn
|
7 |
+
- afriberta
|
8 |
+
---
|
9 |
+
|
10 |
+
# Yoruba Sentiment Analysis with CNN and Afriberta
|
11 |
+
|
12 |
+
This repository contains a PyTorch model for sentiment analysis of Yoruba text. The model utilizes a Convolutional Neural Network (CNN) architecture on top of a pre-trained Afriberta model, specifically "Davlan/naija-twitter-sentiment-afriberta-large".
|
13 |
+
|
14 |
+
## Model Description
|
15 |
+
|
16 |
+
The model consists of the following components:
|
17 |
+
|
18 |
+
- **Afriberta Base:** The pre-trained Afriberta model serves as a powerful feature extractor for Yoruba text.
|
19 |
+
- **CNN Layers:** Multiple 1D convolutional layers with varying kernel sizes capture local patterns and n-gram features from the Afriberta embeddings.
|
20 |
+
- **Max Pooling:** Max pooling layers extract the most salient features from the convolutional outputs.
|
21 |
+
- **Dropout:** Dropout regularization helps prevent overfitting.
|
22 |
+
- **Fully Connected Layer:** A final fully connected layer maps the concatenated pooled features to sentiment classes.
|
23 |
+
|
24 |
+
## Intended Uses & Limitations
|
25 |
+
|
26 |
+
This model is designed for sentiment analysis of Yoruba text and can be applied to various use cases, such as:
|
27 |
+
|
28 |
+
- **Social Media Monitoring:** Analyze sentiment expressed in Yoruba tweets or social media posts.
|
29 |
+
- **Customer Feedback Analysis:** Understand customer sentiment towards products or services in Yoruba.
|
30 |
+
- **Opinion Mining:** Extract opinions and sentiments from Yoruba text data.
|
31 |
+
|
32 |
+
**Limitations:**
|
33 |
+
|
34 |
+
- The model's performance may be limited by the size and quality of the training data.
|
35 |
+
- It may not generalize well to domains significantly different from the training data.
|
36 |
+
- As with any language model, there's a risk of bias and potential for misuse.
|
37 |
+
|
38 |
+
## Training and Evaluation Data
|
39 |
+
|
40 |
+
The model was trained on a dataset of Yoruba tweets annotated with sentiment labels. The dataset was split into training, validation, and test sets.
|
41 |
+
|
42 |
+
## Training Procedure
|
43 |
+
|
44 |
+
The model was trained using the following steps:
|
45 |
+
|
46 |
+
1. **Data Preprocessing:** Text data was tokenized using the Afriberta tokenizer.
|
47 |
+
2. **Model Initialization:** The SentimentCNNModel was initialized with the pre-trained Afriberta model and CNN layers.
|
48 |
+
3. **Optimization:** The model was trained using the Adam optimizer and cross-entropy loss.
|
49 |
+
4. **Early Stopping:** Training was stopped early based on validation loss to prevent overfitting.
|
50 |
+
|
51 |
+
## Evaluation Results
|
52 |
+
|
53 |
+
The model achieved the following performance on the test set:
|
54 |
+
|
55 |
+
- **Test Loss:** [0.6707]
|
56 |
+
- **F1-Score:** [0.8095]
|
57 |
+
|
58 |
+
## How to Use
|
59 |
+
|
60 |
+
1. **Install Dependencies:** Ensure you have PyTorch and Transformers installed:
|
61 |
+
|
62 |
+
```bash
|
63 |
+
pip install torch transformers
|
64 |
+
```
|
65 |
+
|
66 |
+
2. **Load the Model:** You can load the model using the Hugging Face `transformers` library:
|
67 |
+
|
68 |
+
```python
|
69 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
70 |
+
|
71 |
+
model_name = "Testys/cnn_sent_yor"
|
72 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
73 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
74 |
+
```
|
75 |
+
|
76 |
+
3. **Make Predictions:** Use the tokenizer to prepare your input text and the model to get predictions:
|
77 |
+
|
78 |
+
```python
|
79 |
+
def predict(text):
|
80 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=128)
|
81 |
+
outputs = model(**inputs)
|
82 |
+
return torch.softmax(outputs.logits, dim=1).items()
|
83 |
+
|
84 |
+
sample_text = "Your Yoruba text here"
|
85 |
+
prediction = predict(sample_text)
|
86 |
+
print("Sentiment:", prediction)
|
87 |
+
```
|
88 |
+
|
89 |
+
## Citing the Model
|
90 |
+
|
91 |
+
If you use this model in your research, please cite it using the following format:
|
92 |
+
|
93 |
+
```bibtex
|
94 |
+
@misc{your_model_name,
|
95 |
+
author = {Your Name},
|
96 |
+
title = {Yoruba Sentiment Analysis with CNN and Afriberta},
|
97 |
+
year = {2024},
|
98 |
+
publisher = {Hugging Face's Model Hub},
|
99 |
+
journal = {Hugging Face's Model Hub},
|
100 |
+
howpublished = {\\url{https://huggingface.co/your_model_name}}
|
101 |
+
}
|
102 |
+
```
|
103 |
+
|
104 |
+
## License
|
105 |
+
|
106 |
+
This model is open-sourced under the MIT license. The license allows commercial use, modification, distribution, and private use.
|
107 |
+
|
108 |
+
## Contact Information
|
109 |
+
|
110 |
+
For any queries regarding the model, feel free to reach out via GitHub or direct email:
|
111 |
+
- **GitHub:** [https://github.com/dev-tyta]
|
112 |
+
- **Email:** [[email protected]]
|
113 |
+
|
114 |
+
```
|
config.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"model_type": "CNNForSentimentAnalysis", "num_classes": 2, "max_length": 128, "pretrained_model_name": "Davlan/naija-twitter-sentiment-afriberta-large"}
|
modeling_cnn_sent.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import torch
|
3 |
+
import torch.nn as nn
|
4 |
+
import torch.nn.functional as F
|
5 |
+
from transformers import AutoModel
|
6 |
+
|
7 |
+
class SentimentCNNModel(nn.Module):
|
8 |
+
def __init__(self, transformer_model_name, num_classes, cnn_out_channels=100, cnn_kernel_sizes=[3, 5, 7]):
|
9 |
+
super(SentimentCNNModel, self).__init__()
|
10 |
+
# Load pre-trained transformer model
|
11 |
+
self.transformer = AutoModel.from_pretrained(transformer_model_name)
|
12 |
+
|
13 |
+
# CNN layers with multiple kernel sizes
|
14 |
+
self.convs = nn.ModuleList([
|
15 |
+
nn.Conv1d(in_channels=self.transformer.config.hidden_size,
|
16 |
+
out_channels=cnn_out_channels,
|
17 |
+
kernel_size=k)
|
18 |
+
for k in cnn_kernel_sizes
|
19 |
+
])
|
20 |
+
|
21 |
+
# Dropout layer
|
22 |
+
self.dropout = nn.Dropout(0.5)
|
23 |
+
|
24 |
+
# Fully connected layer
|
25 |
+
self.fc = nn.Linear(len(cnn_kernel_sizes) * cnn_out_channels, num_classes)
|
26 |
+
|
27 |
+
def forward(self, input_ids, attention_mask):
|
28 |
+
# Get hidden states from the transformer model
|
29 |
+
transformer_outputs = self.transformer(input_ids=input_ids, attention_mask=attention_mask)
|
30 |
+
hidden_states = transformer_outputs.last_hidden_state # Shape: (batch_size, seq_len, hidden_size)
|
31 |
+
|
32 |
+
# Transpose for CNN input: (batch_size, hidden_size, seq_len)
|
33 |
+
hidden_states = hidden_states.transpose(1, 2)
|
34 |
+
|
35 |
+
# Apply convolution and pooling
|
36 |
+
conv_outputs = [torch.relu(conv(hidden_states)) for conv in self.convs]
|
37 |
+
pooled_outputs = [torch.max(output, dim=2)[0] for output in conv_outputs]
|
38 |
+
|
39 |
+
# Concatenate pooled outputs and apply dropout
|
40 |
+
cat_output = torch.cat(pooled_outputs, dim=1)
|
41 |
+
cat_output = self.dropout(cat_output)
|
42 |
+
|
43 |
+
# Final classification
|
44 |
+
logits = self.fc(cat_output)
|
45 |
+
|
46 |
+
return logits
|
sent_pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1f9eb93cf4592a046678a953616e4281e7bba91703e19a563cb7074ed7c69d33
|
3 |
+
size 507210373
|
sentencepiece.bpe.model
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6419b3044bff45e94e0553cbb81425fd06046e9294b33555e23fdc69377dba6f
|
3 |
+
size 1554839
|
special_tokens_map.json
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bos_token": {
|
3 |
+
"content": "<s>",
|
4 |
+
"lstrip": false,
|
5 |
+
"normalized": false,
|
6 |
+
"rstrip": false,
|
7 |
+
"single_word": false
|
8 |
+
},
|
9 |
+
"cls_token": {
|
10 |
+
"content": "<s>",
|
11 |
+
"lstrip": false,
|
12 |
+
"normalized": false,
|
13 |
+
"rstrip": false,
|
14 |
+
"single_word": false
|
15 |
+
},
|
16 |
+
"eos_token": {
|
17 |
+
"content": "</s>",
|
18 |
+
"lstrip": false,
|
19 |
+
"normalized": false,
|
20 |
+
"rstrip": false,
|
21 |
+
"single_word": false
|
22 |
+
},
|
23 |
+
"mask_token": {
|
24 |
+
"content": "<mask>",
|
25 |
+
"lstrip": false,
|
26 |
+
"normalized": false,
|
27 |
+
"rstrip": false,
|
28 |
+
"single_word": false
|
29 |
+
},
|
30 |
+
"pad_token": {
|
31 |
+
"content": "<pad>",
|
32 |
+
"lstrip": false,
|
33 |
+
"normalized": false,
|
34 |
+
"rstrip": false,
|
35 |
+
"single_word": false
|
36 |
+
},
|
37 |
+
"sep_token": {
|
38 |
+
"content": "</s>",
|
39 |
+
"lstrip": false,
|
40 |
+
"normalized": false,
|
41 |
+
"rstrip": false,
|
42 |
+
"single_word": false
|
43 |
+
},
|
44 |
+
"unk_token": {
|
45 |
+
"content": "<unk>",
|
46 |
+
"lstrip": false,
|
47 |
+
"normalized": false,
|
48 |
+
"rstrip": false,
|
49 |
+
"single_word": false
|
50 |
+
}
|
51 |
+
}
|
tokenizer.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tokenizer_config.json
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"added_tokens_decoder": {
|
3 |
+
"0": {
|
4 |
+
"content": "<s>",
|
5 |
+
"lstrip": false,
|
6 |
+
"normalized": false,
|
7 |
+
"rstrip": false,
|
8 |
+
"single_word": false,
|
9 |
+
"special": true
|
10 |
+
},
|
11 |
+
"1": {
|
12 |
+
"content": "<pad>",
|
13 |
+
"lstrip": false,
|
14 |
+
"normalized": false,
|
15 |
+
"rstrip": false,
|
16 |
+
"single_word": false,
|
17 |
+
"special": true
|
18 |
+
},
|
19 |
+
"2": {
|
20 |
+
"content": "</s>",
|
21 |
+
"lstrip": false,
|
22 |
+
"normalized": false,
|
23 |
+
"rstrip": false,
|
24 |
+
"single_word": false,
|
25 |
+
"special": true
|
26 |
+
},
|
27 |
+
"4": {
|
28 |
+
"content": "<unk>",
|
29 |
+
"lstrip": false,
|
30 |
+
"normalized": false,
|
31 |
+
"rstrip": false,
|
32 |
+
"single_word": false,
|
33 |
+
"special": true
|
34 |
+
},
|
35 |
+
"70005": {
|
36 |
+
"content": "<mask>",
|
37 |
+
"lstrip": false,
|
38 |
+
"normalized": false,
|
39 |
+
"rstrip": false,
|
40 |
+
"single_word": false,
|
41 |
+
"special": true
|
42 |
+
}
|
43 |
+
},
|
44 |
+
"bos_token": "<s>",
|
45 |
+
"clean_up_tokenization_spaces": true,
|
46 |
+
"cls_token": "<s>",
|
47 |
+
"do_lower_case": false,
|
48 |
+
"eos_token": "</s>",
|
49 |
+
"mask_token": "<mask>",
|
50 |
+
"model_max_length": 1000000000000000019884624838656,
|
51 |
+
"pad_token": "<pad>",
|
52 |
+
"sep_token": "</s>",
|
53 |
+
"sp_model_kwargs": {},
|
54 |
+
"tokenizer_class": "XLMRobertaTokenizer",
|
55 |
+
"unk_token": "<unk>"
|
56 |
+
}
|