-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsparsebert.py
159 lines (133 loc) · 5.97 KB
/
sparsebert.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
# -*- coding: utf-8 -*-
import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers.models.bert.modeling_bert import BertPreTrainedModel, BertModel
from modules.modeling_sparsebert import SparseBertModel
import collections
def soft_cross_entropy(input, target, reduction="mean"):
s_likelihood = F.log_softmax(input, dim=-1)
t_probability = F.softmax(target, dim=-1)
cross_entropy = -torch.sum(t_probability * s_likelihood, dim=-1)
if reduction == "mean":
cross_entropy = cross_entropy.mean()
else:
pass
return cross_entropy
class SparseBertCls(BertPreTrainedModel):
Output = collections.namedtuple(
"Output",
(
"hidden_states",
"logit",
"prediction",
"label",
)
)
def __init__(self, config):
super().__init__(config)
self.bert = SparseBertModel(config)
self.cls = nn.Sequential(
nn.Linear(config.hidden_size, config.hidden_size),
nn.Tanh(),
nn.Dropout(0.1),
nn.Linear(config.hidden_size, config.num_labels),
)
self.init_weights()
self.layer_num = config.num_hidden_layers
self.layer_skip = int(self.layer_num / 6)
def forward(self, inputs, head_mask=None, neuron_mask=None):
text_indices, text_mask, text_segments, label = inputs
if neuron_mask is None:
all_hidden_states = \
self.bert(text_indices, attention_mask=text_mask, token_type_ids=text_segments, output_hidden_states=True)[2]
else:
all_hidden_states = \
self.bert(text_indices, attention_mask=text_mask, token_type_ids=text_segments, head_mask=head_mask, neuron_mask=neuron_mask, output_hidden_states=True)[2]
hidden_states = [all_hidden_states[i] for i in list(range(0, self.layer_num + 1, self.layer_skip))] # 0, 6, 12
# batch_size x num_selected_layers x seq_length x hidden_size
hidden_states = torch.stack(hidden_states, dim=1)
logit = self.cls(all_hidden_states[-1][:, 0])
hidden_size = hidden_states.shape[-1]
mask = text_mask.unsqueeze(1).unsqueeze(-1).expand_as(hidden_states)
hidden_states = torch.masked_select(hidden_states, mask)
hidden_states = hidden_states.reshape(-1, hidden_size)
if logit.shape[-1] == 1:
prediction = logit.squeeze(-1)
else:
prediction = logit.argmax(-1)
return SparseBertCls.Output(
hidden_states=hidden_states,
logit=logit,
prediction=prediction,
label=label,
)
@staticmethod
def loss_fn(t_output, s_output, temperature=1.0):
loss = F.mse_loss(s_output.hidden_states, t_output.hidden_states.detach(), reduction="mean")
if s_output.logit.shape[-1] == 1:
loss += F.mse_loss(s_output.logit.squeeze(-1), t_output.logit.squeeze(-1).detach(), reduction="mean")
else:
loss += (temperature ** 2) * soft_cross_entropy(s_output.logit / temperature, t_output.logit.detach() / temperature, reduction="mean")
#if s_output.logit.shape[-1] == 1:
# loss += F.mse_loss(s_output.logit.squeeze(-1), s_output.label, reduction="mean")
#else:
# loss += F.cross_entropy(s_output.logit, s_output.label, reduction="mean")
loss = loss / 2.0
return loss
class SparseBertNer(BertPreTrainedModel):
Output = collections.namedtuple(
"Output",
(
"hidden_states",
"logits",
"predictions",
"labels",
)
)
def __init__(self, config):
super().__init__(config)
self.bert = SparseBertModel(config)
self.cls = nn.Sequential(
nn.Dropout(0.1),
nn.Linear(config.hidden_size, config.num_labels),
)
self.init_weights()
self.layer_num = config.num_hidden_layers
self.layer_skip = int(self.layer_num / 6)
def forward(self, inputs, head_mask=None, neuron_mask=None):
text_indices, text_mask, text_segments, labels, label_mask = inputs
if neuron_mask is None:
all_hidden_states = \
self.bert(text_indices, attention_mask=text_mask, token_type_ids=text_segments, output_hidden_states=True)[2]
else:
all_hidden_states = \
self.bert(text_indices, attention_mask=text_mask, token_type_ids=text_segments, head_mask=head_mask, neuron_mask=neuron_mask, output_hidden_states=True)[2]
hidden_states = [all_hidden_states[i] for i in list(range(0, self.layer_num + 1, self.layer_skip))] # 0, 6, 12
# batch_size x num_selected_layers x seq_length x hidden_size
hidden_states = torch.stack(hidden_states, dim=1)
logits = self.cls(all_hidden_states[-1])
logit_size = logits.shape[-1]
mask = label_mask.unsqueeze(-1).expand_as(logits)
logits = torch.masked_select(logits, mask)
logits = logits.reshape(-1, logit_size)
mask = label_mask
labels = torch.masked_select(labels, mask)
hidden_size = hidden_states.shape[-1]
mask = text_mask.unsqueeze(1).unsqueeze(-1).expand_as(hidden_states)
hidden_states = torch.masked_select(hidden_states, mask)
hidden_states = hidden_states.reshape(-1, hidden_size)
predictions = logits.argmax(-1)
return SparseBertNer.Output(
hidden_states=hidden_states,
logits=logits,
predictions=predictions,
labels=labels,
)
@staticmethod
def loss_fn(t_output, s_output, temperature=1.0):
loss = F.mse_loss(s_output.hidden_states, t_output.hidden_states.detach(), reduction="mean")
loss += (temperature ** 2) * soft_cross_entropy(s_output.logits / temperature, t_output.logits.detach() / temperature, reduction="mean")
#loss += F.cross_entropy(s_output.logits, s_output.labels, reduction="mean")
loss = loss / 2.0
return loss