forked from Guangzidetiaoyue/CDT_ABSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcn.py
127 lines (105 loc) · 4.99 KB
/
gcn.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
import torch
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from tree import Tree, head_to_tree, tree_to_adj
class GCNClassifier(nn.Module):
def __init__(self, args, emb_matrix=None):
super().__init__()
in_dim = args.hidden_dim
self.args = args
self.gcn_model = GCNAbsaModel(args, emb_matrix=emb_matrix)
self.classifier = nn.Linear(in_dim, args.num_class)
def forward(self, inputs):
outputs = self.gcn_model(inputs)
logits = self.classifier(outputs)
return logits, outputs
class GCNAbsaModel(nn.Module):
def __init__(self, args, emb_matrix=None):
super().__init__()
self.args = args
self.emb_matrix = emb_matrix
# create embedding layers
self.emb = nn.Embedding(args.tok_size, args.emb_dim, padding_idx=0)
if emb_matrix is not None:
self.emb.weight = nn.Parameter(emb_matrix.cuda(), requires_grad=False)
self.pos_emb = nn.Embedding(args.pos_size, args.pos_dim, padding_idx=0) if args.pos_dim > 0 else None # POS emb
self.post_emb = nn.Embedding(args.post_size, args.post_dim, padding_idx=0) if args.post_dim > 0 else None # position emb
embeddings = (self.emb, self.pos_emb, self.post_emb)
# gcn layer
self.gcn = GCN(args, embeddings, args.hidden_dim, args.num_layers)
def forward(self, inputs):
tok, asp, pos, head, deprel, post, mask, l = inputs # unpack inputs
maxlen = max(l.data)
def inputs_to_tree_reps(head, words, l):
trees = [head_to_tree(head[i], words[i], l[i]) for i in range(len(l))]
adj = [tree_to_adj(maxlen, tree, directed=self.args.direct, self_loop=self.args.loop).reshape(1, maxlen, maxlen) for tree in trees]
adj = np.concatenate(adj, axis=0)
adj = torch.from_numpy(adj)
return adj.cuda()
adj = inputs_to_tree_reps(head.data, tok.data, l.data)
h = self.gcn(adj, inputs)
# avg pooling asp feature
asp_wn = mask.sum(dim=1).unsqueeze(-1) # aspect words num
mask = mask.unsqueeze(-1).repeat(1,1,self.args.hidden_dim) # mask for h
outputs = (h*mask).sum(dim=1) / asp_wn # mask h
return outputs
class GCN(nn.Module):
def __init__(self, args, embeddings, mem_dim, num_layers):
super(GCN, self).__init__()
self.args = args
self.layers = num_layers
self.mem_dim = mem_dim
self.in_dim = args.emb_dim+args.post_dim+args.pos_dim
self.emb, self.pos_emb, self.post_emb = embeddings
# rnn layer
input_size = self.in_dim
self.rnn = nn.LSTM(input_size, args.rnn_hidden, args.rnn_layers, batch_first=True, \
dropout=args.rnn_dropout, bidirectional=args.bidirect)
if args.bidirect:
self.in_dim = args.rnn_hidden * 2
else:
self.in_dim = args.rnn_hidden
# drop out
self.rnn_drop = nn.Dropout(args.rnn_dropout)
self.in_drop = nn.Dropout(args.input_dropout)
self.gcn_drop = nn.Dropout(args.gcn_dropout)
# gcn layer
self.W = nn.ModuleList()
for layer in range(self.layers):
input_dim = self.in_dim if layer == 0 else self.mem_dim
self.W.append(nn.Linear(input_dim, self.mem_dim))
def encode_with_rnn(self, rnn_inputs, seq_lens, batch_size):
h0, c0 = rnn_zero_state(batch_size, self.args.rnn_hidden, self.args.rnn_layers, self.args.bidirect)
rnn_inputs = nn.utils.rnn.pack_padded_sequence(rnn_inputs, seq_lens, batch_first=True)
rnn_outputs, (ht, ct) = self.rnn(rnn_inputs, (h0, c0))
rnn_outputs, _ = nn.utils.rnn.pad_packed_sequence(rnn_outputs, batch_first=True)
return rnn_outputs
def forward(self, adj, inputs):
tok, asp, pos, head, deprel, post, mask, l = inputs # unpack inputs
# embedding
word_embs = self.emb(tok)
embs = [word_embs]
if self.args.pos_dim > 0:
embs += [self.pos_emb(pos)]
if self.args.post_dim > 0:
embs += [self.post_emb(post)]
embs = torch.cat(embs, dim=2)
embs = self.in_drop(embs)
# rnn layer
gcn_inputs = self.rnn_drop(self.encode_with_rnn(embs, l, tok.size()[0]))
# gcn layer
denom = adj.sum(2).unsqueeze(2) + 1 # norm
for l in range(self.layers):
Ax = adj.bmm(gcn_inputs)
AxW = self.W[l](Ax)
AxW = AxW / denom
gAxW = F.relu(AxW)
gcn_inputs = self.gcn_drop(gAxW) if l < self.layers - 1 else gAxW
return gcn_inputs
def rnn_zero_state(batch_size, hidden_dim, num_layers, bidirectional=True):
total_layers = num_layers * 2 if bidirectional else num_layers
state_shape = (total_layers, batch_size, hidden_dim)
h0 = c0 = Variable(torch.zeros(*state_shape), requires_grad=False)
return h0.cuda(), c0.cuda()