jinmang2 commited on
Commit
838b72c
1 Parent(s): 5e3f780

Create modeling_roberta_ner.py

Browse files
Files changed (1) hide show
  1. modeling_roberta_ner.py +92 -0
modeling_roberta_ner.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch.nn as nn
2
+ from transformers import RobertaPreTrainedModel, RobertaModel
3
+ from transformers.modeling_outputs import TokenClassifierOutput
4
+
5
+
6
+ class RobertaClassificationHead(nn.Module):
7
+ """Head for sentence-level classification tasks."""
8
+
9
+ def __init__(self, config):
10
+ super().__init__()
11
+ self.dense = nn.Linear(config.hidden_size, config.hidden_size)
12
+ classifier_dropout = (
13
+ config.classifier_dropout if config.classifier_dropout is not None else config.hidden_dropout_prob
14
+ )
15
+ self.dropout = nn.Dropout(classifier_dropout)
16
+ self.out_proj = nn.Linear(config.hidden_size, config.num_labels)
17
+
18
+ def forward(self, x, **kwargs):
19
+ x = self.dropout(x)
20
+ x = self.dense(x)
21
+ x = torch.tanh(x)
22
+ x = self.dropout(x)
23
+ x = self.out_proj(x)
24
+ return x
25
+
26
+
27
+ class RobertaForCharNER(RobertaPreTrainedModel):
28
+ _keys_to_ignore_on_load_unexpected = [r"pooler"]
29
+ _keys_to_ignore_on_load_missing = [r"position_ids"]
30
+
31
+ def __init__(self, config):
32
+ super().__init__(config)
33
+ self.num_labels = config.num_labels
34
+
35
+ self.roberta = RobertaModel(config, add_pooling_layer=False)
36
+ self.classifier = RobertaClassificationHead(config)
37
+
38
+ self.init_weights()
39
+
40
+ def forward(
41
+ self,
42
+ input_ids=None,
43
+ attention_mask=None,
44
+ token_type_ids=None,
45
+ position_ids=None,
46
+ head_mask=None,
47
+ inputs_embeds=None,
48
+ labels=None,
49
+ output_attentions=None,
50
+ output_hidden_states=None,
51
+ return_dict=None,
52
+ ):
53
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
54
+
55
+ outputs = self.roberta(
56
+ input_ids,
57
+ attention_mask=attention_mask,
58
+ token_type_ids=token_type_ids,
59
+ position_ids=position_ids,
60
+ head_mask=head_mask,
61
+ inputs_embeds=inputs_embeds,
62
+ output_attentions=output_attentions,
63
+ output_hidden_states=output_hidden_states,
64
+ return_dict=return_dict,
65
+ )
66
+ sequence_output = outputs[0]
67
+ logits = self.classifier(sequence_output)
68
+
69
+ loss = None
70
+ if labels is not None:
71
+ loss_fct = nn.CrossEntropyLoss()
72
+ # Only keep active parts of the loss
73
+ if attention_mask is not None:
74
+ active_loss = attention_mask.view(-1) == 1
75
+ active_logits = logits.view(-1, self.num_labels)
76
+ active_labels = torch.where(
77
+ active_loss, labels.view(-1), torch.tensor(loss_fct.ignore_index).type_as(labels)
78
+ )
79
+ loss = loss_fct(active_logits, active_labels)
80
+ else:
81
+ loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
82
+
83
+ if not return_dict:
84
+ output = (logits,) + outputs[2:]
85
+ return ((loss,) + output) if loss is not None else output
86
+
87
+ return TokenClassifierOutput(
88
+ loss=loss,
89
+ logits=logits,
90
+ hidden_states=outputs.hidden_states,
91
+ attentions=outputs.attentions,
92
+ )