DeepLearning101 commited on
Commit
a2fef5f
1 Parent(s): 5894ace

Upload 2 files

Browse files
Files changed (2) hide show
  1. models/adversarial.py +29 -0
  2. models/semeval7.py +93 -0
models/adversarial.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ # @Time : 2022/1/7 11:02 上午
3
+ # @Author : JianingWang
4
+ # @File : adversarial.py
5
+ import torch
6
+
7
+
8
+ class FGM:
9
+ def __init__(self, model):
10
+ self.model = model
11
+ self.backup = {}
12
+
13
+ def attack(self, epsilon=1., emb_name="word_embeddings"):
14
+ # emb_name这个参数要换成你模型中embedding的参数名
15
+ for name, param in self.model.named_parameters():
16
+ if param.requires_grad and emb_name in name:
17
+ self.backup[name] = param.data.clone()
18
+ norm = torch.norm(param.grad)
19
+ if norm != 0:
20
+ r_at = epsilon * param.grad / norm
21
+ param.data.add_(r_at)
22
+
23
+ def restore(self, emb_name="word_embeddings"):
24
+ # emb_name这个参数要换成你模型中embedding的参数名
25
+ for name, param in self.model.named_parameters():
26
+ if param.requires_grad and emb_name in name:
27
+ assert name in self.backup
28
+ param.data = self.backup[name]
29
+ self.backup = {}
models/semeval7.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ # @Time : 2022/1/28 5:38 下午
3
+ # @Author : JianingWang
4
+ # @File : semeval7.py
5
+ import torch
6
+ from torch import nn
7
+ from torch.nn import CrossEntropyLoss, MSELoss
8
+ from transformers.activations import ACT2FN
9
+ from transformers.modeling_outputs import SequenceClassifierOutput
10
+ from transformers.models.deberta_v2.modeling_deberta_v2 import ContextPooler, DebertaV2Model, DebertaV2PreTrainedModel, StableDropout
11
+
12
+
13
+ class DebertaV2ForSemEval7MultiTask(DebertaV2PreTrainedModel):
14
+ def __init__(self, config):
15
+ super().__init__(config)
16
+ self.deberta = DebertaV2Model(config)
17
+ self.pooler = ContextPooler(config)
18
+ output_dim = self.pooler.output_dim
19
+ self.num_labels = 3
20
+ self.dense = nn.Linear(config.pooler_hidden_size*2, config.pooler_hidden_size)
21
+ self.classifier = nn.Linear(output_dim, self.num_labels)
22
+ self.regression = nn.Linear(output_dim, 1)
23
+ drop_out = getattr(config, "cls_dropout", None)
24
+ drop_out = self.config.hidden_dropout_prob if drop_out is None else drop_out
25
+
26
+ self.dropout = StableDropout(drop_out)
27
+ self.post_init()
28
+
29
+ def get_input_embeddings(self):
30
+ return self.deberta.get_input_embeddings()
31
+
32
+ def set_input_embeddings(self, new_embeddings):
33
+ self.deberta.set_input_embeddings(new_embeddings)
34
+
35
+ def forward(
36
+ self,
37
+ input_ids=None,
38
+ attention_mask=None,
39
+ token_type_ids=None,
40
+ position_ids=None,
41
+ inputs_embeds=None,
42
+ labels=None,
43
+ output_attentions=None,
44
+ output_hidden_states=None,
45
+ return_dict=None,
46
+ score=None
47
+ ):
48
+ r"""
49
+ labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
50
+ Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
51
+ config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
52
+ `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
53
+ """
54
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
55
+
56
+ outputs = self.deberta(
57
+ input_ids,
58
+ token_type_ids=token_type_ids,
59
+ attention_mask=attention_mask,
60
+ position_ids=position_ids,
61
+ inputs_embeds=inputs_embeds,
62
+ output_attentions=output_attentions,
63
+ output_hidden_states=output_hidden_states,
64
+ return_dict=return_dict,
65
+ )
66
+ w = torch.logical_and(input_ids >= min(self.config.start_token_ids), input_ids <= max(self.config.start_token_ids))
67
+ start_index = w.nonzero()[:, 1].view(-1, 2)
68
+ # <start_entity> + <end_entity> 进分类
69
+ pooler_output = torch.cat([torch.cat([x[y[0], :], x[y[1], :]]).unsqueeze(0) for x, y in zip(outputs.last_hidden_state, start_index)])
70
+ # [CLS] + <start_entity> + <end_entity> 进分类
71
+ # pooler_output = torch.cat([torch.cat([z, x[y[0], :], x[y[1], :]]).unsqueeze(0)
72
+ # for x, y, z in zip(outputs.last_hidden_state, start_index, outputs.last_hidden_state[:, 0])])
73
+
74
+ context_token = self.dropout(pooler_output)
75
+ pooled_output = self.dense(context_token)
76
+ pooled_output = ACT2FN[self.config.pooler_hidden_act](pooled_output)
77
+ pooled_output = self.dropout(pooled_output)
78
+ re_logits = self.regression(pooled_output)
79
+ cls_logits = self.classifier(pooled_output)
80
+
81
+ loss = None
82
+ if labels is not None:
83
+ re_loss_func = MSELoss()
84
+ re_loss = re_loss_func(re_logits.squeeze(), score.squeeze())
85
+
86
+ cls_loss_func = CrossEntropyLoss()
87
+ cls_loss = cls_loss_func(cls_logits.view(-1, self.num_labels), labels.view(-1))
88
+
89
+ loss = re_loss + cls_loss
90
+
91
+ return SequenceClassifierOutput(
92
+ loss=loss, logits=torch.cat((cls_logits, re_logits), 1), hidden_states=outputs.hidden_states, attentions=outputs.attentions
93
+ )