-
Notifications
You must be signed in to change notification settings - Fork 48
/
bgwa.py
289 lines (220 loc) · 11.7 KB
/
bgwa.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from base import Model
from helper import *
import tensorflow as tf
"""
Abbreviations used in variable names:
Type: Entity type side informatoin
ProbY, RelAlias: Relation alias side information
Recommendation: View this file with tab size 8.
"""
class BGWA(Model):
def getBatches(self, data, shuffle = True):
"""
Generates batches of multiple bags
Parameters
----------
data: Data to be used for creating batches. Dataset as list of bags where each bag is a dictionary
shuffle: Decides whether to shuffle the data or not.
Returns
-------
Generator for creating batches.
"""
if shuffle: random.shuffle(data)
for chunk in getChunks(data, self.p.batch_size): # chunk = batch
batch = ddict(list)
num = 0
for i, bag in enumerate(chunk):
batch['X'] += bag['X']
batch['Pos1'] += bag['Pos1']
batch['Pos2'] += bag['Pos2']
batch['Y'].append(bag['Y'])
old_num = num
num += len(bag['X'])
batch['sent_num'].append([old_num, num, i])
batch = dict(batch)
yield batch
def add_placeholders(self):
"""
Defines the placeholder required for the model
"""
self.input_x = tf.placeholder(tf.int32, shape=[None, None], name='input_data') # Tokens ids of sentences
self.input_y = tf.placeholder(tf.int32, shape=[None, None], name='input_labels') # Actual relation of the bag
self.input_pos1 = tf.placeholder(tf.int32, shape=[None, None], name='input_pos1') # Position ids wrt entity 1
self.input_pos2 = tf.placeholder(tf.int32, shape=[None, None], name='input_pos2') # Position ids wrt entity 2
self.part_pos = tf.placeholder(tf.int32, shape=[None, 2], name='input_part_pos') # Positions where sentence needs to be partitioned
self.x_len = tf.placeholder(tf.int32, shape=[None], name='input_len') # Number of words in sentences in a batch
self.sent_num = tf.placeholder(tf.int32, shape=[None, 3], name='sent_num') # Stores which sentences belong to which bag | [start_index, end_index, bag_number]
self.seq_len = tf.placeholder(tf.int32, shape=(), name='seq_len') # Max number of tokens in sentences in a batch
self.total_bags = tf.placeholder(tf.int32, shape=(), name='total_bags') # Total number of bags in a batch
self.total_sents = tf.placeholder(tf.int32, shape=(), name='total_sents') # Total number of sentences in a batch
self.dropout = tf.placeholder_with_default(self.p.dropout, shape=(), name='dropout') # Dropout used in GCN Layer
self.rec_dropout = tf.placeholder_with_default(self.p.rec_dropout, shape=(), name='rec_dropout') # Dropout used in Bi-LSTM
def pad_dynamic(self, X, pos1, pos2):
"""
Pads each batch during runtime.
Parameters
----------
X: For each sentence in the bag, list of words
pos1: For each sentence in the bag, list position of words with respect to subject
pos2: For each sentence in the bag, list position of words with respect to object
Returns
-------
x_pad Padded words
x_len Number of sentences in each sentence,
pos1_pad Padded position 1
pos2_pad Padded position 2
seq_len Maximum sentence length in the batch
"""
seq_len, max_et = 0, 0
x_len = np.zeros((len(X)), np.int32)
for i, x in enumerate(X):
seq_len = max(seq_len, len(x))
x_len[i] = len(x)
x_pad, _ = self.padData(X, seq_len)
pos1_pad, _ = self.padData(pos1, seq_len)
pos2_pad, _ = self.padData(pos2, seq_len)
return x_pad, x_len, pos1_pad, pos2_pad, seq_len
def create_feed_dict(self, batch, wLabels=True, split='train'):
X, Y, pos1, pos2, sent_num = batch['X'], batch['Y'], batch['Pos1'], batch['Pos2'], batch['sent_num']
total_sents = len(batch['X'])
total_bags = len(batch['Y'])
x_pad, x_len, pos1_pad, pos2_pad, seq_len = self.pad_dynamic(X, pos1, pos2)
y_hot = self.getOneHot(Y, self.num_class)
feed_dict = {}
feed_dict[self.input_x] = np.array(x_pad)
feed_dict[self.input_pos1] = np.array(pos1_pad)
feed_dict[self.input_pos2] = np.array(pos2_pad)
feed_dict[self.x_len] = np.array(x_len)
feed_dict[self.seq_len] = seq_len
feed_dict[self.total_sents] = total_sents
feed_dict[self.total_bags] = total_bags
feed_dict[self.sent_num] = sent_num
if wLabels: feed_dict[self.input_y] = y_hot
if split != 'train':
feed_dict[self.dropout] = 1.0
feed_dict[self.rec_dropout] = 1.0
else:
feed_dict[self.dropout] = self.p.dropout
feed_dict[self.rec_dropout] = self.p.rec_dropout
return feed_dict
def add_model(self):
"""
Creates the Computational Graph
Parameters
----------
Returns
-------
nn_out: Logits for each bag in the batch
accuracy: accuracy for the entire batch
"""
in_wrds, in_pos1, in_pos2 = self.input_x, self.input_pos1, self.input_pos2
with tf.variable_scope('Embeddings', reuse=tf.AUTO_REUSE) as scope:
embed_init = getEmbeddings(self.wrd_list, self.p.embed_dim, self.p.embed_loc)
_wrd_embeddings = tf.get_variable('embeddings', initializer=embed_init, trainable=True, regularizer=self.regularizer)
wrd_pad = tf.zeros([1, self.p.embed_dim])
wrd_embeddings = tf.concat([wrd_pad, _wrd_embeddings], axis=0)
pos1_embeddings = tf.get_variable('pos1_embeddings', [self.max_pos, self.p.pos_dim], initializer=tf.contrib.layers.xavier_initializer(), trainable=True, regularizer=self.regularizer)
pos2_embeddings = tf.get_variable('pos2_embeddings', [self.max_pos, self.p.pos_dim], initializer=tf.contrib.layers.xavier_initializer(), trainable=True, regularizer=self.regularizer)
wrd_embed = tf.nn.embedding_lookup(wrd_embeddings, in_wrds)
pos1_embed = tf.nn.embedding_lookup(pos1_embeddings, in_pos1)
pos2_embed = tf.nn.embedding_lookup(pos2_embeddings, in_pos2)
embeds = tf.concat([wrd_embed, pos1_embed, pos2_embed], axis=2)
with tf.variable_scope('Bi-LSTM') as scope:
fw_cell = tf.contrib.rnn.DropoutWrapper(tf.nn.rnn_cell.GRUCell(self.p.lstm_dim, reuse=tf.AUTO_REUSE, name='FW_GRU'), output_keep_prob=self.rec_dropout)
bk_cell = tf.contrib.rnn.DropoutWrapper(tf.nn.rnn_cell.GRUCell(self.p.lstm_dim, reuse=tf.AUTO_REUSE, name='BW_GRU'), output_keep_prob=self.rec_dropout)
val, state = tf.nn.bidirectional_dynamic_rnn(fw_cell, bk_cell, embeds, sequence_length=self.x_len, dtype=tf.float32)
lstm_out = tf.concat((val[0], val[1]), axis=2)
lstm_out_dim = self.p.lstm_dim*2
with tf.variable_scope('Word_attention', reuse=tf.AUTO_REUSE) as scope:
wrd_query = tf.get_variable('wrd_query', [lstm_out_dim, 1], initializer=tf.contrib.layers.xavier_initializer())
sent_reps = tf.reshape(
tf.matmul(
tf.reshape(
tf.nn.softmax(
tf.reshape(
tf.matmul(
tf.reshape(tf.tanh(lstm_out),[self.total_sents*self.seq_len, lstm_out_dim]),
wrd_query),
[self.total_sents, self.seq_len]
)),
[self.total_sents, 1, self.seq_len]),
lstm_out),
[self.total_sents, lstm_out_dim]
)
with tf.variable_scope('Sentence_attention', reuse=tf.AUTO_REUSE) as scope:
sent_atten_q = tf.get_variable('sent_atten_q', [lstm_out_dim, 1], initializer=tf.contrib.layers.xavier_initializer())
def getSentAtten(num):
num_sents = num[1] - num[0]
bag_sents = sent_reps[num[0]: num[1]]
sent_atten_wts = tf.nn.softmax(tf.reshape(tf.matmul(tf.tanh(bag_sents), sent_atten_q), [num_sents]) )
bag_rep_ = tf.reshape(
tf.matmul(
tf.reshape(sent_atten_wts, [1, num_sents]),
bag_sents),
[lstm_out_dim]
)
return bag_rep_
bag_rep = tf.map_fn(getSentAtten, self.sent_num, dtype=tf.float32)
with tf.variable_scope('FC1', reuse=tf.AUTO_REUSE) as scope:
w_rel = tf.get_variable('w_rel', [lstm_out_dim, self.num_class], initializer=tf.contrib.layers.xavier_initializer(), regularizer=self.regularizer)
b_rel = tf.get_variable('b_rel', initializer=np.zeros([self.num_class]).astype(np.float32), regularizer=self.regularizer)
nn_out = tf.nn.xw_plus_b(bag_rep, w_rel, b_rel)
with tf.name_scope('Accuracy') as scope:
prob = tf.nn.softmax(nn_out)
y_pred = tf.argmax(prob, axis=1)
y_actual = tf.argmax(self.input_y, axis=1)
accuracy = tf.reduce_mean(tf.cast(tf.equal(y_pred, y_actual), tf.float32))
''' Debugging command:
Put the below command anywhere and get the values of the tensors (Use TF like PyTorch!)
res = debug_nn([de_out], self.create_feed_dict( next(self.getBatches(self.data['train'])) ) ); pdb.set_trace()
'''
return nn_out, accuracy
def __init__(self, params):
"""
Constructor for the main function. Loads data and creates computation graph.
Parameters
----------
params: Hyperparameters of the model
Returns
-------
"""
super(BGWA, self).__init__(params)
if __name__== "__main__":
parser = argparse.ArgumentParser(description='Improving Distantly-Supervised Neural Relation Extraction using Side Information')
parser.add_argument('-data', dest="dataset", required=True, help='Dataset to use')
parser.add_argument('-gpu', dest="gpu", default='0', help='GPU to use')
parser.add_argument('-nGate', dest="wGate", action='store_false', help='Include edgewise-gating in GCN')
parser.add_argument('-lstm_dim', dest="lstm_dim", default=192, type=int, help='Hidden state dimension of Bi-LSTM')
parser.add_argument('-pos_dim', dest="pos_dim", default=16, type=int, help='Dimension of positional embeddings')
parser.add_argument('-drop', dest="dropout", default=0.8, type=float, help='Dropout for full connected layer')
parser.add_argument('-rdrop', dest="rec_dropout", default=0.8, type=float, help='Recurrent dropout for LSTM')
parser.add_argument('-lr', dest="lr", default=0.001, type=float, help='Learning rate')
parser.add_argument('-l2', dest="l2", default=0.001, type=float, help='L2 regularization')
parser.add_argument('-epoch', dest="max_epochs", default=2, type=int, help='Max epochs')
parser.add_argument('-batch', dest="batch_size", default=32, type=int, help='Batch size')
parser.add_argument('-chunk', dest="chunk_size", default=1000, type=int, help='Chunk size')
parser.add_argument('-restore', dest="restore", action='store_true', help='Restore from the previous best saved model')
parser.add_argument('-only_eval',dest="only_eval", action='store_true', help='Only Evaluate the pretrained model (skip training)')
parser.add_argument('-opt', dest="opt", default='adam', help='Optimizer to use for training')
parser.add_argument('-eps', dest="eps", default=0.00000001, type=float, help='Value of epsilon')
parser.add_argument('-name', dest="name", default='test_'+str(uuid.uuid4()), help='Name of the run')
parser.add_argument('-seed', dest="seed", default=1234, type=int, help='Seed for randomization')
parser.add_argument('-logdir', dest="log_dir", default='./log/', help='Log directory')
parser.add_argument('-config', dest="config_dir", default='./config/', help='Config directory')
parser.add_argument('-embed_loc',dest="embed_loc", default='./glove/glove.6B.50d.txt', help='Log directory')
parser.add_argument('-embed_dim',dest="embed_dim", default=50, type=int, help='Dimension of embedding')
args = parser.parse_args()
if not args.restore: args.name = args.name
# Set GPU to use
set_gpu(args.gpu)
# Set seed
tf.set_random_seed(args.seed)
random.seed(args.seed)
np.random.seed(args.seed)
# Create model computational graph
model = BGWA(args)
config = tf.ConfigProto()
config.gpu_options.allow_growth=True
with tf.Session(config=config) as sess:
sess.run(tf.global_variables_initializer())
model.fit(sess)