Commit
·
b3ccb2b
1
Parent(s):
59fb548
Upload NoRefER
Browse files- config.json +30 -0
- model.py +75 -0
- pytorch_model.bin +3 -0
config.json
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "asr-qe",
|
3 |
+
"architectures": [
|
4 |
+
"NoRefER"
|
5 |
+
],
|
6 |
+
"attention_probs_dropout_prob": 0.1,
|
7 |
+
"auto_map": {
|
8 |
+
"AutoModel": "model.NoRefER"
|
9 |
+
},
|
10 |
+
"bos_token_id": 0,
|
11 |
+
"classifier_dropout": null,
|
12 |
+
"eos_token_id": 2,
|
13 |
+
"hidden_act": "gelu",
|
14 |
+
"hidden_dropout_prob": 0.1,
|
15 |
+
"hidden_size": 384,
|
16 |
+
"initializer_range": 0.02,
|
17 |
+
"intermediate_size": 1536,
|
18 |
+
"layer_norm_eps": 1e-05,
|
19 |
+
"max_position_embeddings": 514,
|
20 |
+
"model_type": "xlm-roberta",
|
21 |
+
"num_attention_heads": 12,
|
22 |
+
"num_hidden_layers": 12,
|
23 |
+
"pad_token_id": 1,
|
24 |
+
"position_embedding_type": "absolute",
|
25 |
+
"torch_dtype": "float32",
|
26 |
+
"transformers_version": "4.25.1",
|
27 |
+
"type_vocab_size": 1,
|
28 |
+
"use_cache": true,
|
29 |
+
"vocab_size": 250002
|
30 |
+
}
|
model.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
__author__="thiagocastroferreira"
|
2 |
+
|
3 |
+
import torch.nn as nn
|
4 |
+
from transformers import XLMRobertaModel
|
5 |
+
from transformers.models.xlm_roberta.modeling_xlm_roberta import XLMRobertaPreTrainedModel
|
6 |
+
from transformers.modeling_outputs import SequenceClassifierOutput
|
7 |
+
|
8 |
+
class Smish(nn.Module):
|
9 |
+
def __init__(self):
|
10 |
+
super().__init__()
|
11 |
+
def forward(self, x):
|
12 |
+
return x * (x.sigmoid() + 1).log().tanh()
|
13 |
+
|
14 |
+
class NoRefER(XLMRobertaPreTrainedModel):
|
15 |
+
def __init__(self, config):
|
16 |
+
super().__init__(config)
|
17 |
+
hidden_size = 32
|
18 |
+
self.config = config
|
19 |
+
self.roberta = XLMRobertaModel(config)
|
20 |
+
self.dense = nn.Sequential(
|
21 |
+
nn.Dropout(config.hidden_dropout_prob),
|
22 |
+
nn.Linear(config.hidden_size, hidden_size, bias = False),
|
23 |
+
nn.Dropout(config.hidden_dropout_prob), Smish(),
|
24 |
+
nn.Linear(hidden_size, 1, bias = False)
|
25 |
+
)
|
26 |
+
|
27 |
+
self.post_init()
|
28 |
+
|
29 |
+
def forward(self, positive_input_ids, positive_attention_mask, negative_input_ids, negative_attention_mask, labels, weight=None):
|
30 |
+
# positive processing
|
31 |
+
positive_inputs = {
|
32 |
+
"input_ids": positive_input_ids #, "attention_mask": positive_attention_mask
|
33 |
+
}
|
34 |
+
positive = self.dense(self.roberta(**positive_inputs).pooler_output).squeeze(-1)
|
35 |
+
|
36 |
+
# negative processing
|
37 |
+
negative_inputs = {
|
38 |
+
"input_ids": negative_input_ids #, "attention_mask": negative_attention_mask
|
39 |
+
}
|
40 |
+
negative = self.dense(self.roberta(**negative_inputs).pooler_output).squeeze(-1)
|
41 |
+
|
42 |
+
if weight is None:
|
43 |
+
bce = nn.BCEWithLogitsLoss()
|
44 |
+
else:
|
45 |
+
bs = len(positive)
|
46 |
+
weights = (weight.float() * bs) / weight.sum()
|
47 |
+
bce = nn.BCEWithLogitsLoss(weight = weights)
|
48 |
+
loss = bce(positive - negative, labels.float())
|
49 |
+
return SequenceClassifierOutput(
|
50 |
+
loss=loss,
|
51 |
+
logits=positive.sigmoid()-negative.sigmoid(),
|
52 |
+
)
|
53 |
+
|
54 |
+
def score(
|
55 |
+
self,
|
56 |
+
input_ids,
|
57 |
+
attention_mask=None,
|
58 |
+
token_type_ids=None,
|
59 |
+
position_ids=None,
|
60 |
+
head_mask=None,
|
61 |
+
inputs_embeds=None,
|
62 |
+
labels=None,
|
63 |
+
output_attentions=None,
|
64 |
+
output_hidden_states=None,
|
65 |
+
):
|
66 |
+
h = self.roberta(input_ids,
|
67 |
+
attention_mask=attention_mask,
|
68 |
+
token_type_ids=token_type_ids,
|
69 |
+
position_ids=position_ids,
|
70 |
+
head_mask=head_mask,
|
71 |
+
inputs_embeds=inputs_embeds,
|
72 |
+
output_attentions=output_attentions,
|
73 |
+
output_hidden_states=output_hidden_states,).pooler_output
|
74 |
+
|
75 |
+
return self.dense(h).sigmoid().squeeze(-1)
|
pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:615a33102224ff0ce4a25a462a1b28a2f59ea48ef1288d27078e6e20d0947d07
|
3 |
+
size 470682677
|