-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathesim.py
139 lines (95 loc) · 5.08 KB
/
esim.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
import tensorflow as tf
from util import blocks
class MyModel(object):
def __init__(self, seq_length, emb_dim, hidden_dim, embeddings, emb_train):
## Define hyperparameters
self.embedding_dim = emb_dim
self.dim = hidden_dim
self.sequence_length = seq_length
## Define the placeholders
self.premise_x = tf.placeholder(tf.int32, [None, self.sequence_length])
self.hypothesis_x = tf.placeholder(tf.int32, [None, self.sequence_length])
self.y = tf.placeholder(tf.int32, [None])
self.keep_rate_ph = tf.placeholder(tf.float32, [])
## Define parameters
self.E = tf.Variable(embeddings, trainable=emb_train)
self.W_mlp = tf.Variable(tf.random_normal([self.dim * 8, self.dim], stddev=0.1))
self.b_mlp = tf.Variable(tf.random_normal([self.dim], stddev=0.1))
self.W_cl = tf.Variable(tf.random_normal([self.dim, 3], stddev=0.1))
self.b_cl = tf.Variable(tf.random_normal([3], stddev=0.1))
## Function for embedding lookup and dropout at embedding layer
def emb_drop(x):
emb = tf.nn.embedding_lookup(self.E, x)
emb_drop = tf.nn.dropout(emb, self.keep_rate_ph)
return emb_drop
# Get lengths of unpadded sentences
prem_seq_lengths, mask_prem = blocks.length(self.premise_x)
hyp_seq_lengths, mask_hyp = blocks.length(self.hypothesis_x)
### First biLSTM layer ###
premise_in = emb_drop(self.premise_x)
hypothesis_in = emb_drop(self.hypothesis_x)
premise_outs, c1 = blocks.biLSTM(premise_in, dim=self.dim, seq_len=prem_seq_lengths, name='premise')
hypothesis_outs, c2 = blocks.biLSTM(hypothesis_in, dim=self.dim, seq_len=hyp_seq_lengths, name='hypothesis')
premise_bi = tf.concat(premise_outs, axis=2)
hypothesis_bi = tf.concat(hypothesis_outs, axis=2)
premise_list = tf.unstack(premise_bi, axis=1)
hypothesis_list = tf.unstack(hypothesis_bi, axis=1)
### Attention ###
scores_all = []
premise_attn = []
alphas = []
for i in range(self.sequence_length):
scores_i_list = []
for j in range(self.sequence_length):
score_ij = tf.reduce_sum(tf.multiply(premise_list[i], hypothesis_list[j]), 1, keep_dims=True)
scores_i_list.append(score_ij)
scores_i = tf.stack(scores_i_list, axis=1)
alpha_i = blocks.masked_softmax(scores_i, mask_hyp)
a_tilde_i = tf.reduce_sum(tf.multiply(alpha_i, hypothesis_bi), 1)
premise_attn.append(a_tilde_i)
scores_all.append(scores_i)
alphas.append(alpha_i)
scores_stack = tf.stack(scores_all, axis=2)
scores_list = tf.unstack(scores_stack, axis=1)
hypothesis_attn = []
betas = []
for j in range(self.sequence_length):
scores_j = scores_list[j]
beta_j = blocks.masked_softmax(scores_j, mask_prem)
b_tilde_j = tf.reduce_sum(tf.multiply(beta_j, premise_bi), 1)
hypothesis_attn.append(b_tilde_j)
betas.append(beta_j)
# Make attention-weighted sentence representations into one tensor,
premise_attns = tf.stack(premise_attn, axis=1)
hypothesis_attns = tf.stack(hypothesis_attn, axis=1)
# For making attention plots,
self.alpha_s = tf.stack(alphas, axis=2)
self.beta_s = tf.stack(betas, axis=2)
### Subcomponent Inference ###
prem_diff = tf.subtract(premise_bi, premise_attns)
prem_mul = tf.multiply(premise_bi, premise_attns)
hyp_diff = tf.subtract(hypothesis_bi, hypothesis_attns)
hyp_mul = tf.multiply(hypothesis_bi, hypothesis_attns)
m_a = tf.concat([premise_bi, premise_attns, prem_diff, prem_mul], 2)
m_b = tf.concat([hypothesis_bi, hypothesis_attns, hyp_diff, hyp_mul], 2)
### Inference Composition ###
v1_outs, c3 = blocks.biLSTM(m_a, dim=self.dim, seq_len=prem_seq_lengths, name='v1')
v2_outs, c4 = blocks.biLSTM(m_b, dim=self.dim, seq_len=hyp_seq_lengths, name='v2')
v1_bi = tf.concat(v1_outs, axis=2)
v2_bi = tf.concat(v2_outs, axis=2)
### Pooling Layer ###
v_1_sum = tf.reduce_sum(v1_bi, 1)
v_1_ave = tf.div(v_1_sum, tf.expand_dims(tf.cast(prem_seq_lengths, tf.float32), -1))
v_2_sum = tf.reduce_sum(v2_bi, 1)
v_2_ave = tf.div(v_2_sum, tf.expand_dims(tf.cast(hyp_seq_lengths, tf.float32), -1))
v_1_max = tf.reduce_max(v1_bi, 1)
v_2_max = tf.reduce_max(v2_bi, 1)
v = tf.concat([v_1_ave, v_2_ave, v_1_max, v_2_max], 1)
# MLP layer
h_mlp = tf.nn.tanh(tf.matmul(v, self.W_mlp) + self.b_mlp)
# Dropout applied to classifier
h_drop = tf.nn.dropout(h_mlp, self.keep_rate_ph)
# Get prediction
self.logits = tf.matmul(h_drop, self.W_cl) + self.b_cl
# Define the cost function
self.total_cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=self.y, logits=self.logits))