Upload 4 files
Browse files- __init__.py +4 -0
- config (2).json +41 -0
- modeling.py +9 -0
- modeling_custom.py +55 -0
__init__.py
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# __init__.py
|
2 |
+
from .modeling_custom import CustomModel
|
3 |
+
|
4 |
+
__all__ = ["CustomModel"]
|
config (2).json
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "gsar78/HellenicSentimentAI",
|
3 |
+
"architectures": [
|
4 |
+
"CustomModel"
|
5 |
+
],
|
6 |
+
"attention_probs_dropout_prob": 0.1,
|
7 |
+
"bos_token_id": 0,
|
8 |
+
"classifier_dropout": null,
|
9 |
+
"eos_token_id": 2,
|
10 |
+
"gradient_checkpointing": false,
|
11 |
+
"hidden_act": "gelu",
|
12 |
+
"hidden_dropout_prob": 0.1,
|
13 |
+
"hidden_size": 768,
|
14 |
+
"id2label": {
|
15 |
+
"0": "negative",
|
16 |
+
"1": "neutral",
|
17 |
+
"2": "positive"
|
18 |
+
},
|
19 |
+
"initializer_range": 0.02,
|
20 |
+
"intermediate_size": 3072,
|
21 |
+
"label2id": {
|
22 |
+
"negative": 0,
|
23 |
+
"neutral": 1,
|
24 |
+
"positive": 2
|
25 |
+
},
|
26 |
+
"layer_norm_eps": 1e-05,
|
27 |
+
"max_position_embeddings": 514,
|
28 |
+
"model_type": "xlm-roberta",
|
29 |
+
"num_attention_heads": 12,
|
30 |
+
"num_hidden_layers": 12,
|
31 |
+
"output_past": true,
|
32 |
+
"pad_token_id": 1,
|
33 |
+
"position_embedding_type": "absolute",
|
34 |
+
"problem_type": "multi_label_classification",
|
35 |
+
"torch_dtype": "float32",
|
36 |
+
"transformers_version": "4.42.3",
|
37 |
+
"type_vocab_size": 1,
|
38 |
+
"use_cache": true,
|
39 |
+
"vocab_size": 250002,
|
40 |
+
"num_emotion_labels": 18 // Custom parameter
|
41 |
+
}
|
modeling.py
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# modeling.py
|
2 |
+
from transformers import XLMRobertaForSequenceClassification, AutoConfig
|
3 |
+
from .modeling_custom import CustomModel
|
4 |
+
|
5 |
+
def from_pretrained(pretrained_model_name_or_path, *model_args, **kwargs):
|
6 |
+
config = AutoConfig.from_pretrained(pretrained_model_name_or_path)
|
7 |
+
num_emotion_labels = config.num_emotion_labels
|
8 |
+
model = CustomModel.from_pretrained(pretrained_model_name_or_path, num_emotion_labels=num_emotion_labels, *model_args, **kwargs)
|
9 |
+
return model
|
modeling_custom.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# modeling_custom.py
|
2 |
+
from transformers import XLMRobertaForSequenceClassification
|
3 |
+
import torch.nn as nn
|
4 |
+
import torch.nn.functional as F
|
5 |
+
|
6 |
+
class CustomModel(XLMRobertaForSequenceClassification):
|
7 |
+
def __init__(self, config, num_emotion_labels):
|
8 |
+
super(CustomModel, self).__init__(config)
|
9 |
+
self.num_emotion_labels = num_emotion_labels
|
10 |
+
|
11 |
+
# Freeze sentiment classifier parameters
|
12 |
+
for param in self.classifier.parameters():
|
13 |
+
param.requires_grad = False
|
14 |
+
|
15 |
+
# Define emotion classifier
|
16 |
+
self.dropout_emotion = nn.Dropout(config.hidden_dropout_prob)
|
17 |
+
self.emotion_classifier = nn.Sequential(
|
18 |
+
nn.Linear(config.hidden_size, 512),
|
19 |
+
nn.Mish(),
|
20 |
+
nn.Dropout(0.3),
|
21 |
+
nn.Linear(512, num_emotion_labels)
|
22 |
+
)
|
23 |
+
|
24 |
+
# Initialize the weights of the new layers
|
25 |
+
self._init_weights(self.emotion_classifier[0])
|
26 |
+
self._init_weights(self.emotion_classifier[3])
|
27 |
+
|
28 |
+
def _init_weights(self, module):
|
29 |
+
if isinstance(module, nn.Linear):
|
30 |
+
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
|
31 |
+
if module.bias is not None:
|
32 |
+
module.bias.data.zero_()
|
33 |
+
|
34 |
+
def forward(self, input_ids=None, attention_mask=None, sentiment=None, labels=None):
|
35 |
+
outputs = self.roberta(input_ids=input_ids, attention_mask=attention_mask)
|
36 |
+
sequence_output = outputs[0]
|
37 |
+
|
38 |
+
# Select the CLS token for emotion classification
|
39 |
+
cls_hidden_states = sequence_output[:, 0, :]
|
40 |
+
cls_hidden_states = self.dropout_emotion(cls_hidden_states)
|
41 |
+
|
42 |
+
emotion_logits = self.emotion_classifier(cls_hidden_states)
|
43 |
+
|
44 |
+
# Sentiment logits from the frozen classifier
|
45 |
+
with torch.no_grad():
|
46 |
+
cls_token_state = sequence_output[:, 0, :].unsqueeze(1)
|
47 |
+
sentiment_logits = self.classifier(cls_token_state).squeeze(1)
|
48 |
+
|
49 |
+
if labels is not None:
|
50 |
+
class_weights = torch.tensor([1.0] * self.num_emotion_labels).to(labels.device)
|
51 |
+
loss_fct = nn.BCEWithLogitsLoss(pos_weight=class_weights)
|
52 |
+
loss = loss_fct(emotion_logits, labels)
|
53 |
+
return {"loss": loss, "emotion_logits": emotion_logits, "sentiment_logits": sentiment_logits}
|
54 |
+
|
55 |
+
return {"emotion_logits": emotion_logits, "sentiment_logits": sentiment_logits}
|