-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodeling_QE.py
196 lines (168 loc) · 6.98 KB
/
modeling_QE.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import torch
from torch import nn
from transformers import (
BertModel,
XLMModel,
XLMRobertaModel,
DistilBertModel,
MBartModel,
MarianModel,
BertPreTrainedModel,
RobertaPreTrainedModel,
DistilBertPreTrainedModel,
MBartPreTrainedModel,
)
from transformers.models.bart.modeling_bart import shift_tokens_right
from transformers.models.marian.modeling_marian import MarianPreTrainedModel
import pdb
class QEBaseClass(object):
# 所有的子类均应该继承这个类,而且是通过多继承的方式,并且将这个类放置在第一个继承的位置,从而获得forward方法
def forward(
self,
input_ids,
attention_mask=None,
token_type_ids=None,
decoder_input_ids=None,
decoder_attention_mask=None,
additional_feature=None,
**kwargs,
):
if self.model_type in ['xlm-tlm', 'xlm-mlm']:
outputs = self.pretrained_model(
input_ids,
langs=token_type_ids,
attention_mask=attention_mask,
)
sequence_outputs = self.dropout(outputs[0])
pooled_outputs = sequence_outputs[:, 0, :]
elif self.model_type == 'bert':
outputs = self.bert(
input_ids,
token_type_ids=token_type_ids,
attention_mask=attention_mask,
)
sequence_outputs = self.dropout(outputs[0])
pooled_outputs = sequence_outputs[:, 0, :]
elif self.model_type == 'xlmr':
outputs = self.roberta(
input_ids,
attention_mask=attention_mask,
)
sequence_outputs = self.dropout(outputs[0])
pooled_outputs = sequence_outputs[:, 0, :]
elif self.model_type == 'distilbert':
outputs = self.distilbert(
input_ids,
attention_mask=attention_mask,
)
sequence_outputs = self.dropout(outputs[0])
pooled_outputs = sequence_outputs[:, 0, :]
elif self.model_type in ['mbart', 'opus-mt']:
eos_mask = decoder_input_ids.eq(self.config.eos_token_id)
eos_indices = eos_mask.nonzero(as_tuple=True)[1]
decoder_input_ids = shift_tokens_right(decoder_input_ids, self.config.pad_token_id, self.config.decoder_start_token_id)
outputs = self.model(
input_ids,
attention_mask=attention_mask,
decoder_input_ids=decoder_input_ids,
decoder_attention_mask=decoder_attention_mask,
)
sequence_outputs = self.dropout(outputs[0])
pooled_outputs = sequence_outputs[eos_mask, :].view(sequence_outputs.size(0), -1, sequence_outputs.size(-1))[
:, -1, :
]
else:
raise Exception('Please check your model_type!')
if additional_feature is not None:
pooled_outputs = torch.cat([pooled_outputs, additional_feature], dim=-1)
sent_outputs = self.sent_classifier(pooled_outputs).contiguous().view(-1)
word_outputs = self.word_classifier(sequence_outputs)
return sent_outputs, word_outputs
class BertPreTrainedModelForQE(QEBaseClass, BertPreTrainedModel):
def __init__(self, config, args):
super().__init__(config)
# config.num_labels = 1 # 在config里默认是2,但是我们这里需要设置成1
self.model_type = args.model_type
self.bert = BertModel(config=config)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
if args.has_additional_feature:
self.word_classifier = nn.Linear(config.hidden_size, 2)
self.sent_classifier = nn.Linear(config.hidden_size + args.additional_feature_size, 1)
else:
self.word_classifier = nn.Linear(config.hidden_size, 2)
self.sent_classifier = nn.Linear(config.hidden_size, 1)
self.init_weights()
class XLMRobertaPreTrainedModelForQE(QEBaseClass, RobertaPreTrainedModel):
def __init__(self, config, args):
super().__init__(config)
# config.num_labels = 1 # 在config里默认是2,但是我们这里需要设置成1
self.model_type = args.model_type
self.roberta = XLMRobertaModel(config)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
if args.has_additional_feature:
self.word_classifier = nn.Linear(config.hidden_size, 2)
self.sent_classifier = nn.Linear(config.hidden_size + args.additional_feature_size, 1)
else:
self.word_classifier = nn.Linear(config.hidden_size, 2)
self.sent_classifier = nn.Linear(config.hidden_size, 1)
self.init_weights()
class XLMPreTrainedModelForQE(QEBaseClass, BertPreTrainedModel):
def __init__(self, config, args):
super().__init__(config)
config.num_labels = 1 # 在config里默认是2,但是我们这里需要设置成1
self.pretrained_model= XLMModel(config)
self.dropout = nn.Dropout(config.dropout) #参考huggingface的BertForSequenceClassification
self.use_bigru = args.use_bigru
self.use_sigmoid = args.use_sigmoid
self.bad_weight = args.bad_weight
self.model_type = args.model_type
self.bigru_dropout = self.dropout
if args.has_additional_feature:
self.word_classifier = nn.Linear(config.hidden_size, 2)
self.sent_classifier = nn.Linear(config.hidden_size + args.additional_feature_size, 1)
else:
self.word_classifier = nn.Linear(config.hidden_size, 2)
self.sent_classifier = nn.Linear(config.hidden_size, 1)
self.init_weights()
class DistilBertPreTrainedModelForQE(QEBaseClass, DistilBertPreTrainedModel):
def __init__(self, config, args):
super().__init__(config)
# config.num_labels = 1 # 在config里默认是2,但是我们这里需要设置成1
self.model_type = args.model_type
self.distilbert = DistilBertModel(config)
self.dropout = nn.Dropout(config.dropout)
if args.has_additional_feature:
self.word_classifier = nn.Linear(config.hidden_size, 2)
self.sent_classifier = nn.Linear(config.hidden_size + args.additional_feature_size, 1)
else:
self.word_classifier = nn.Linear(config.hidden_size, 2)
self.sent_classifier = nn.Linear(config.hidden_size, 1)
self.init_weights()
class MBartPreTrainedModelForQE(QEBaseClass, MBartPreTrainedModel):
def __init__(self, config, args):
super().__init__(config)
# config.num_labels = 1 # 在config里默认是2,但是我们这里需要设置成1
self.model_type = args.model_type
self.model = MBartModel(config=config)
self.dropout = nn.Dropout(config.dropout)
if args.has_additional_feature:
self.word_classifier = nn.Linear(config.hidden_size, 2)
self.sent_classifier = nn.Linear(config.hidden_size + args.additional_feature_size, 1)
else:
self.word_classifier = nn.Linear(config.hidden_size, 2)
self.sent_classifier = nn.Linear(config.hidden_size, 1)
self.init_weights()
class MarianPreTrainedModelForQE(QEBaseClass, MarianPreTrainedModel):
def __init__(self, config, args):
super().__init__(config)
# config.num_labels = 1 # 在config里默认是2,但是我们这里需要设置成1
self.model_type = args.model_type
self.model = MarianModel(config=config)
self.dropout = nn.Dropout(config.dropout)
if args.has_additional_feature:
self.word_classifier = nn.Linear(config.hidden_size, 2)
self.sent_classifier = nn.Linear(config.hidden_size + args.additional_feature_size, 1)
else:
self.word_classifier = nn.Linear(config.hidden_size, 2)
self.sent_classifier = nn.Linear(config.hidden_size, 1)
self.init_weights()