-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathknow_model_kec.py
152 lines (134 loc) · 6.56 KB
/
know_model_kec.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from model import CausePredictor, mask_logic
from ck_transformer import UtterEncoder2
class CskCauseDag(nn.Module):
def __init__(self,
model_size,
mapping_type,
utter_dim,
conv_encoder,
rnn_dropout,
num_layers,
dropout,
pooler_type,
add_emotion,
emotion_emb,
emotion_dim):
super(CskCauseDag, self).__init__()
self.utter_encoder = UtterEncoder2(model_size, utter_dim, conv_encoder, rnn_dropout)
self.dag = DAGKNN(utter_dim, utter_dim, num_layers, dropout, pooler_type)
self.classifier = CausePredictor(utter_dim, utter_dim)
if model_size == 'base':
csk_dim = 768
else:
csk_dim = 1024
self.csk_mapping = nn.Linear(csk_dim, utter_dim)
self.add_emotion = add_emotion
if add_emotion:
self.emotion_embeddings = nn.Embedding(emotion_emb.shape[0], emotion_emb.shape[1], padding_idx=0, _weight=emotion_emb)
self.emotion_lin = nn.Linear(emotion_emb.shape[1], emotion_dim)
self.emotion_mapping = nn.Linear(emotion_dim + utter_dim, utter_dim)
else:
self.emotion_embeddings = None
self.emotion_lin = None
self.emotion_mapping = None
def forward(self, input_ids, attention_mask, conv_len, mask, s_mask, o_mask,
e_mask, emotion_label, knowledge_text, knowledge_mask, know_adj):
utter_emb = self.utter_encoder(input_ids, attention_mask, conv_len)
# (num_know, seq_len, bert_dim)
knowledge_emb = self.utter_encoder.encoder(knowledge_text, attention_mask=knowledge_mask).last_hidden_state
# (num_know, bert_dim)
knowledge_emb = torch.max(knowledge_emb, dim=1)[0]
knowledge = F.relu(self.csk_mapping(knowledge_emb))
if self.add_emotion:
emo_emb = self.emotion_lin(self.emotion_embeddings(emotion_label))
utter_emb = self.emotion_mapping(torch.cat([utter_emb, emo_emb], dim=-1))
utter_emb = self.dag(utter_emb, knowledge, e_mask, s_mask, o_mask, know_adj)
logits = self.classifier(utter_emb, mask)
return logits
class DAGKNN(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, dropout, pooler_type='all'):
super(DAGKNN, self).__init__()
self.dropout = nn.Dropout(dropout)
self.layers = nn.ModuleList()
self.num_layers = num_layers
self.pooler_type = pooler_type
for i in range(self.num_layers):
self.layers.append(GRNKLayer(input_size, hidden_size))
if self.pooler_type == 'all':
self.emb_lin = nn.Linear((num_layers+1)*hidden_size, hidden_size)
else:
self.emb_lin = nn.Linear(hidden_size, hidden_size)
def forward(self, features, knowledge, adj, s_mask, o_mask, know_adj):
H = [features]
H1 = features
for i in range(self.num_layers):
H1 = self.dropout(self.layers[i](H1, knowledge, adj, s_mask, o_mask, know_adj))
H.append(H1)
if self.pooler_type == 'all':
H = torch.cat(H, dim=2)
utter_emb = self.emb_lin(H)
else:
utter_emb = self.emb_lin(H[-1])
return utter_emb
class GRNKLayer(nn.Module):
def __init__(self, input_size, hidden_size):
super(GRNKLayer, self).__init__()
self.gru_c = nn.GRUCell(input_size, hidden_size)
self.gru_p = nn.GRUCell(input_size, hidden_size)
self.gru_s = nn.GRUCell(input_size, hidden_size)
self.gru_k = nn.GRUCell(input_size, hidden_size)
self.gat = GraphAttentionK(hidden_size)
def forward(self, features, knowledge, adj, s_mask, o_mask, know_adj):
# features: [batch_size, num_utter, utter_dim]
num_utter = features.size()[1]
# the first utterance
# [batch_size, 1, utter_dim]
C = self.gru_c(features[:, 0, :]).unsqueeze(1)
M = torch.zeros_like(C).squeeze(1)
self_know = torch.index_select(knowledge, 0, know_adj[:, 0, 0])
KS = self.gru_s(self_know, features[:, 0, :]).unsqueeze(1)
# [batch_size]
P = self.gru_p(M, features[:, 0, :]).unsqueeze(1)
H1 = C + P + KS
for i in range(1, num_utter):
_, M, K = self.gat(features[:, i, :], H1, H1, knowledge, adj[:, i, :i],
s_mask[:, i, :i], o_mask[:, i, :i], know_adj[:, i, :i])
C = self.gru_c(features[:, i, :], M).unsqueeze(1)
self_know = torch.index_select(knowledge, 0, know_adj[:, i, i])
KS = self.gru_s(self_know, features[:, i, :]).unsqueeze(1)
K_ = self.gru_k(K, features[:, i, :]).unsqueeze(1)
P = self.gru_p(M, features[:, i, :]).unsqueeze(1)
H_temp = C + P + K_ + KS
# [batch_size, i+1, utter_dim]
H1 = torch.cat((H1, H_temp), dim=1)
return H1
class GraphAttentionK(nn.Module):
def __init__(self, hidden_size):
super(GraphAttentionK, self).__init__()
self.hidden_size = hidden_size
self.linear = nn.Linear(hidden_size * 2, 1)
self.Wr0 = nn.Linear(hidden_size, hidden_size, bias=False)
self.Wr1 = nn.Linear(hidden_size, hidden_size, bias=False)
self.Wr2 = nn.Linear(hidden_size, hidden_size, bias=False)
def forward(self, Q, K, V, knowledge, adj, s_mask, o_mask, know_adj):
B = K.size()[0]
N = K.size()[1]
Q = Q.unsqueeze(1).expand(-1, N, -1) # (B, N, D)
know_picked = torch.index_select(knowledge, 0, know_adj.flatten()) # (B*N, D)
know_picked = self.Wr2(know_picked.contiguous().view(B, N, -1)) # (B, N, D)
X = torch.cat((Q, (K + know_picked)), dim=2) # (B, N, 2D)
alpha = self.linear(X).permute(0, 2, 1) # (B, 1, N)
adj = adj.unsqueeze(1) # (B, 1, N)
alpha = mask_logic(alpha, adj) # (B, 1, N)
attn_weight = F.softmax(alpha, dim=2) # (B, 1, N)
V0 = self.Wr0(V) # (B, N, D)
V1 = self.Wr1(V) # (B, N, D)
s_mask = s_mask.unsqueeze(2).float() # (B, N, 1)
o_mask = o_mask.unsqueeze(2).float()
V = V0 * s_mask + V1 * o_mask
know_sum = torch.matmul(attn_weight, know_picked).squeeze(1)
attn_sum = torch.matmul(attn_weight, V).squeeze(1) # (B, D)
return attn_weight, attn_sum, know_sum