-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.py
195 lines (163 loc) · 6.87 KB
/
models.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
import torch
import torch.nn as nn
from transformers import AutoConfig, AutoModel
from transformers.modeling_outputs import SequenceClassifierOutput
loss_fn = nn.CrossEntropyLoss()
class MLUBModel(nn.Module):
def __init__(self, model_name_or_path, num_labels, inference=False, linear_dropout=0.1, base_model_dropout=None):
super().__init__()
config = AutoConfig.from_pretrained(model_name_or_path)
config.num_labels = num_labels
if base_model_dropout:
print(f"[important] changing `base_model_dropout` to {base_model_dropout}")
config.hidden_dropout_prob = base_model_dropout
config.attention_probs_dropout_prob = base_model_dropout
if inference:
self.base_model = AutoModel.from_config(config=config)
else:
self.base_model = AutoModel.from_pretrained(model_name_or_path, config=config)
self.linear = nn.Sequential(
nn.Linear(config.hidden_size, config.hidden_size),
nn.Dropout(linear_dropout),
nn.Linear(config.hidden_size, num_labels),
)
def forward(self, input_ids, attention_mask, start_end_mask, labels=None):
outputs = self.base_model(
input_ids=input_ids,
attention_mask=attention_mask)
x = outputs.last_hidden_state
b, s, h = x.size()
start_end_mask = start_end_mask.unsqueeze(-1).expand(b, s, h)
x = x * start_end_mask
x = torch.sum(x, dim=1)
logits = self.linear(x)
loss = loss_fn(logits, labels) if labels is not None else None
return SequenceClassifierOutput(
loss=loss,
logits=logits,
hidden_states=outputs.hidden_states,
attentions=outputs.attentions,
)
class MLUBModelWithMeaningAttention(nn.Module):
def __init__(self, model_name_or_path, num_labels, meaning_input_ids, meaning_attention_mask, inference=False, linear_dropout=0.1, base_model_dropout=None):
super().__init__()
# assuming those tensors have already got device configured
self.meaning_input_ids = meaning_input_ids
self.meaning_attention_mask = meaning_attention_mask
config = AutoConfig.from_pretrained(model_name_or_path)
config.num_labels = num_labels
if base_model_dropout:
print(f"[important] changing `base_model_dropout` to {base_model_dropout}")
config.hidden_dropout_prob = base_model_dropout
config.attention_probs_dropout_prob = base_model_dropout
if inference:
self.base_model = AutoModel.from_config(config=config)
else:
self.base_model = AutoModel.from_pretrained(model_name_or_path, config=config)
self.linear = nn.Sequential(
nn.Linear(config.hidden_size, config.hidden_size),
nn.Dropout(linear_dropout),
# nn.Linear(config.hidden_size, num_labels),
)
self.meaning_linear = nn.Sequential(
nn.Linear(config.hidden_size, config.hidden_size),
nn.Dropout(linear_dropout),
# nn.Linear(config.hidden_size, num_labels),
)
def forward(self, input_ids, attention_mask, start_end_mask, labels=None):
# input text
outputs = self.base_model(
input_ids=input_ids,
attention_mask=attention_mask)
x = outputs.last_hidden_state
b, s, h = x.size()
start_end_mask = start_end_mask.unsqueeze(-1).expand(b, s, h)
x = x * start_end_mask
x = torch.sum(x, dim=1)
x = self.linear(x)
# meaning representation
meaning_outputs = self.base_model(
input_ids=self.meaning_input_ids,
attention_mask=self.meaning_attention_mask)
meaning_x = meaning_outputs.pooler_output
meaning_x = self.meaning_linear(meaning_x)
# attention-like
logits = torch.matmul(x, meaning_x.T)
# loss
loss = loss_fn(logits, labels) if labels is not None else None
return SequenceClassifierOutput(
loss=loss,
logits=logits,
hidden_states=outputs.hidden_states,
attentions=outputs.attentions,
)
if __name__ == "__main__":
model_name_or_path = "bayartsogt/mongolian-roberta-base"
num_labels = 13 # this is 69 in our case
batch_size = 4; max_len = 20
vocab_size = 32000
print("------ MLUBModel ------")
model =MLUBModel(
model_name_or_path=model_name_or_path,
num_labels=num_labels,
inference=True,
linear_dropout=0.1,
base_model_dropout=None
)
# Input: B, S
# Output:
# - loss: 0
# - logits: B, L
input_ids = torch.randint(0, num_labels, (batch_size, max_len))
attention_mask = torch.randint(0, 1, (batch_size, max_len))
start_end_mask = torch.randint(0, 1, (batch_size, max_len))
output = model(
input_ids = input_ids,
attention_mask = attention_mask,
start_end_mask = start_end_mask,
)
print("input_ids.size():", input_ids.size())
print("attention_mask.size():", attention_mask.size())
print("start_end_mask.size():", start_end_mask.size())
print("------")
print("output.logits.size():", output.logits.size())
print("output.loss:", output.loss)
print("-----------------------")
"""
MLUBModelWithMeaningAttention
forward:
input_ids: b, s
attention_mask: b, s
start_end_mask: b, s
meaning_input_ids: l, s
meaning_attention_mask: l, s
"""
print("------ MLUBModelWithMeaningAttention ------")
input_ids = torch.randint(0, vocab_size, (batch_size, max_len))
attention_mask = torch.randint(0, 1, (batch_size, max_len))
start_end_mask = torch.randint(0, 1, (batch_size, max_len))
meaning_input_ids = torch.randint(0, vocab_size, (num_labels, max_len))
meaning_attention_mask = torch.randint(0, 1, (num_labels, max_len))
print(" input_ids:", input_ids.size())
print(" attention_mask:", attention_mask.size())
print(" start_end_mask:", start_end_mask.size())
print(" meaning_input_ids:", meaning_input_ids.size())
print("meaning_attention_mask:", meaning_attention_mask.size())
print("------")
model = MLUBModelWithMeaningAttention(
model_name_or_path=model_name_or_path,
num_labels=num_labels,
inference=True,
linear_dropout=0.1,
base_model_dropout=None,
meaning_input_ids = meaning_input_ids,
meaning_attention_mask = meaning_attention_mask,
)
output = model(
input_ids = input_ids,
attention_mask = attention_mask,
start_end_mask = start_end_mask,
)
print("output.logits.size():", output.logits.size())
print(" output.loss:", output.loss)
print("------")