-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBertModelsCustom.py
268 lines (229 loc) · 10.5 KB
/
BertModelsCustom.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# -*- coding:utf-8 -*-
import torch.nn as nn
import torch
from transformers import BertPreTrainedModel, RobertaModel
from transformers import BertModel
from torch.nn import CrossEntropyLoss
from transformers.modeling_outputs import MultipleChoiceModelOutput
from transformers.models.roberta.modeling_roberta import RobertaPreTrainedModel
class BertForMultipleChoice(BertPreTrainedModel):
def __init__(self, config):
super().__init__(config)
self.bert = BertModel(config)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.classifier = nn.Linear(config.hidden_size, 1)
self.init_weights()
def forward(
self,
input_ids=None,
attention_mask=None,
token_type_ids=None,
position_ids=None,
head_mask=None,
inputs_embeds=None,
labels=None,
):
num_choices = input_ids.shape[1]
input_ids = input_ids.view(-1, input_ids.size(-1))
attention_mask = attention_mask.view(-1, attention_mask.size(-1)) if attention_mask is not None else None
token_type_ids = token_type_ids.view(-1, token_type_ids.size(-1)) if token_type_ids is not None else None
position_ids = position_ids.view(-1, position_ids.size(-1)) if position_ids is not None else None
# 将bert三个输入展平 输入到bertmodel
"""
last_hidden_state: [32=4*batch, seq_len,768]
pooler_ouput: [32=4*batch,768]
"""
outputs = self.bert(
input_ids,
attention_mask=attention_mask,
token_type_ids=token_type_ids,
position_ids=position_ids,
head_mask=head_mask,
inputs_embeds=inputs_embeds,
)
# 隐层输出
pooled_output = outputs[1] # CLS https://www.cnblogs.com/webbery/p/12167552.html
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
reshaped_logits = logits.view(-1, num_choices)
outputs = (reshaped_logits,) + outputs[2:] # add hidden states and attention if they are here
if labels is not None:
loss_fct = CrossEntropyLoss()
loss = loss_fct(reshaped_logits, labels)
outputs = (loss,) + outputs
return outputs # (loss), reshaped_logits, (hidden_states), (attentions)
class BertForMultipleChoice3Linear(BertPreTrainedModel):
def __init__(self, config):
super().__init__(config)
self.bert = BertModel(config)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.classifier3 = nn.Linear(config.hidden_size, 512)
self.classifier2 = nn.Linear(512, 256)
self.classifier1 = nn.Linear(256, 1)
self.init_weights()
def forward(
self,
input_ids=None,
attention_mask=None,
token_type_ids=None,
position_ids=None,
head_mask=None,
inputs_embeds=None,
labels=None,
):
num_choices = input_ids.shape[1]
input_ids = input_ids.view(-1, input_ids.size(-1))
attention_mask = attention_mask.view(-1, attention_mask.size(-1)) if attention_mask is not None else None
token_type_ids = token_type_ids.view(-1, token_type_ids.size(-1)) if token_type_ids is not None else None
position_ids = position_ids.view(-1, position_ids.size(-1)) if position_ids is not None else None
# 将bert三个输入展平 输入到bertmodel
"""
last_hidden_state: [32=4*batch, seq_len,768]
pooler_ouput: [32=4*batch,768]
"""
outputs = self.bert(
input_ids,
attention_mask=attention_mask,
token_type_ids=token_type_ids,
position_ids=position_ids,
head_mask=head_mask,
inputs_embeds=inputs_embeds,
)
# 隐层输出
pooled_output = outputs[1] # CLS https://www.cnblogs.com/webbery/p/12167552.html
# pooled_output = self.dropout(pooled_output)
logits = self.classifier3(pooled_output)
logits = self.classifier2(logits)
logits = self.classifier1(logits)
reshaped_logits = logits.view(-1, num_choices)
outputs = (reshaped_logits,) + outputs[2:] # add hidden states and attention if they are here
if labels is not None:
loss_fct = CrossEntropyLoss()
loss = loss_fct(reshaped_logits, labels)
outputs = (loss,) + outputs
return outputs # (loss), reshaped_logits, (hidden_states), (attentions)
class BertForMultipleChoiceBiLSTM(BertPreTrainedModel):
def __init__(self, config):
super().__init__(config)
self.config = config
self.bert = BertModel(config)
self.lstm = nn.LSTM(config.hidden_size, config.lstm_hidden_size,
num_layers=1, bidirectional=True, batch_first=True)
self.gru = nn.GRU(config.lstm_hidden_size * 2, config.lstm_hidden_size,
num_layers=1, bidirectional=True, batch_first=True)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.classifier = nn.Linear(config.hidden_size * 4, 1)
self.init_weights()
def forward(self, input_ids=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None,
inputs_embeds=None, labels=None):
num_choices = input_ids.shape[1]
input_ids = input_ids.view(-1, input_ids.size(-1))
attention_mask = attention_mask.view(-1, attention_mask.size(-1)) if attention_mask is not None else None
token_type_ids = token_type_ids.view(-1, token_type_ids.size(-1)) if token_type_ids is not None else None
position_ids = position_ids.view(-1, position_ids.size(-1)) if position_ids is not None else None
# 将bert三个输入展平 输入到bertmodel
outputs = self.bert(input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids,
position_ids=position_ids, head_mask=head_mask, inputs_embeds=inputs_embeds)
# 隐层输出
pooled_output = outputs[1] # CLS https://www.cnblogs.com/webbery/p/12167552.html
bert_output = outputs[0]
h_lstm, _ = self.lstm(bert_output) # [batch_size, seq, output*2]
h_gru, hh_gru = self.gru(h_lstm)
hh_gru = hh_gru.view(-1, 2 * self.config.lstm_hidden_size)
avg_pool = torch.mean(h_gru, 1)
max_pool = torch.max(h_gru, 1)
# print(h_gru.shape, avg_pool.shape, hh_gru.shape, max_pool.shape, pooled_output.shape)
h_conc_a = torch.cat((avg_pool, hh_gru, max_pool, pooled_output), 1)
# print(h_conc_a.shape)
output = self.dropout(h_conc_a)
logits = self.classifier(output)
outputs = nn.functional.softmax(logits, -1)
return outputs # (loss), reshaped_logits, (hidden_states), (attentions)
class RobertaForMultipleChoice(RobertaPreTrainedModel):
def __init__(self, config):
super().__init__(config)
self.roberta = RobertaModel(config)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.classifier = nn.Linear(config.hidden_size, 1)
self.init_weights()
def forward(
self,
input_ids=None,
token_type_ids=None,
attention_mask=None,
labels=None,
position_ids=None,
head_mask=None,
inputs_embeds=None,
output_attentions=None,
output_hidden_states=None,
return_dict=None,
):
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
num_choices = input_ids.shape[1] if input_ids is not None else inputs_embeds.shape[1]
flat_input_ids = input_ids.view(-1, input_ids.size(-1)) if input_ids is not None else None
flat_position_ids = position_ids.view(-1, position_ids.size(-1)) if position_ids is not None else None
flat_token_type_ids = token_type_ids.view(-1, token_type_ids.size(-1)) if token_type_ids is not None else None
flat_attention_mask = attention_mask.view(-1, attention_mask.size(-1)) if attention_mask is not None else None
flat_inputs_embeds = (
inputs_embeds.view(-1, inputs_embeds.size(-2), inputs_embeds.size(-1))
if inputs_embeds is not None
else None
)
outputs = self.roberta(
flat_input_ids,
position_ids=flat_position_ids,
token_type_ids=flat_token_type_ids,
attention_mask=flat_attention_mask,
head_mask=head_mask,
inputs_embeds=flat_inputs_embeds,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
pooled_output = outputs[1]
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
reshaped_logits = logits.view(-1, num_choices)
loss = None
if labels is not None:
loss_fct = CrossEntropyLoss()
loss = loss_fct(reshaped_logits, labels)
if not return_dict:
output = (reshaped_logits,) + outputs[2:]
return ((loss,) + output) if loss is not None else output
out = MultipleChoiceModelOutput(
loss=loss,
logits=reshaped_logits,
hidden_states=outputs.hidden_states,
attentions=outputs.attentions,
)
return out
# class bert_classifi(nn.Module):
# def __init__(self, args):
# super(bert_classifi, self).__init__()
#
# self.output_hidden_states = args.output_hidden_states
# self.use_bert_dropout = args.use_bert_dropout
#
# self.bert_model = BertModel.from_pretrained(args.bert_path)
# for param in self.bert_model.parameters():
# param.requires_grad = True
#
# self.bert_dropout = nn.Dropout(args.bert_dropout)
# self.classifier = nn.Linear(768, 1)
#
# def forward(self, input_ids, token_type_ids, attention_mask):
# num_choices = input_ids.shape[1]
# word_vec, sen_vec, hidden_states = self.bert_model(
# input_ids=input_ids, token_type_ids=token_type_ids, attention_mask=attention_mask,
# output_hidden_states=self.output_hidden_states
# )
# if self.use_bert_dropout:
# word_vec = self.bert_dropout(word_vec)
#
# logits = self.classifier(word_vec)
# reshaped_logits = logits.view(-1, num_choices)
# outputs = (reshaped_logits,) + outputs[2:]
#
# return outputs