text stringlengths 1 93.6k |
|---|
initializer: the author of the original paper used gaussian initialization however I found xavier converge faster
|
Returns:
|
params: A collection of parameters used throughout the layers
|
'''
|
with tf.variable_scope("attention_weights"):
|
params = {"W_u_Q":tf.get_variable("W_u_Q",dtype = tf.float32, shape = (2 * attn_size, attn_size), initializer = initializer()),
|
#"W_ru_Q":tf.get_variable("W_ru_Q",dtype = tf.float32, shape = (2 * attn_size, 2 * attn_size), initializer = initializer()),
|
"W_u_P":tf.get_variable("W_u_P",dtype = tf.float32, shape = (2 * attn_size, attn_size), initializer = initializer()),
|
"W_v_P":tf.get_variable("W_v_P",dtype = tf.float32, shape = (attn_size, attn_size), initializer = initializer()),
|
"W_v_P_2":tf.get_variable("W_v_P_2",dtype = tf.float32, shape = (2 * attn_size, attn_size), initializer = initializer()),
|
"W_g":tf.get_variable("W_g",dtype = tf.float32, shape = (4 * attn_size, 4 * attn_size), initializer = initializer()),
|
"W_h_P":tf.get_variable("W_h_P",dtype = tf.float32, shape = (2 * attn_size, attn_size), initializer = initializer()),
|
"W_v_Phat":tf.get_variable("W_v_Phat",dtype = tf.float32, shape = (2 * attn_size, attn_size), initializer = initializer()),
|
"W_h_a":tf.get_variable("W_h_a",dtype = tf.float32, shape = (2 * attn_size, attn_size), initializer = initializer()),
|
"W_v_Q":tf.get_variable("W_v_Q",dtype = tf.float32, shape = (attn_size, attn_size), initializer = initializer()),
|
"v":tf.get_variable("v",dtype = tf.float32, shape = (attn_size), initializer =initializer())}
|
return params
|
def encoding(word, char, word_embeddings, char_embeddings, scope = "embedding"):
|
with tf.variable_scope(scope):
|
word_encoding = tf.nn.embedding_lookup(word_embeddings, word)
|
char_encoding = tf.nn.embedding_lookup(char_embeddings, char)
|
return word_encoding, char_encoding
|
def apply_dropout(inputs, size = None, is_training = True):
|
'''
|
Implementation of Zoneout from https://arxiv.org/pdf/1606.01305.pdf
|
'''
|
if Params.dropout is None and Params.zoneout is None:
|
return inputs
|
if Params.zoneout is not None:
|
return ZoneoutWrapper(inputs, state_zoneout_prob= Params.zoneout, is_training = is_training)
|
elif is_training:
|
return tf.contrib.rnn.DropoutWrapper(inputs,
|
output_keep_prob = 1 - Params.dropout,
|
# variational_recurrent = True,
|
# input_size = size,
|
dtype = tf.float32)
|
else:
|
return inputs
|
def bidirectional_GRU(inputs, inputs_len, cell = None, cell_fn = tf.contrib.rnn.GRUCell, units = Params.attn_size, layers = 1, scope = "Bidirectional_GRU", output = 0, is_training = True, reuse = None):
|
'''
|
Bidirectional recurrent neural network with GRU cells.
|
Args:
|
inputs: rnn input of shape (batch_size, timestep, dim)
|
inputs_len: rnn input_len of shape (batch_size, )
|
cell: rnn cell of type RNN_Cell.
|
output: if 0, output returns rnn output for every timestep,
|
if 1, output returns concatenated state of backward and
|
forward rnn.
|
'''
|
with tf.variable_scope(scope, reuse = reuse):
|
if cell is not None:
|
(cell_fw, cell_bw) = cell
|
else:
|
shapes = inputs.get_shape().as_list()
|
if len(shapes) > 3:
|
inputs = tf.reshape(inputs,(shapes[0]*shapes[1],shapes[2],-1))
|
inputs_len = tf.reshape(inputs_len,(shapes[0]*shapes[1],))
|
# if no cells are provided, use standard GRU cell implementation
|
if layers > 1:
|
cell_fw = MultiRNNCell([apply_dropout(cell_fn(units), size = inputs.shape[-1] if i == 0 else units, is_training = is_training) for i in range(layers)])
|
cell_bw = MultiRNNCell([apply_dropout(cell_fn(units), size = inputs.shape[-1] if i == 0 else units, is_training = is_training) for i in range(layers)])
|
else:
|
cell_fw, cell_bw = [apply_dropout(cell_fn(units), size = inputs.shape[-1], is_training = is_training) for _ in range(2)]
|
outputs, states = tf.nn.bidirectional_dynamic_rnn(cell_fw, cell_bw, inputs,
|
sequence_length = inputs_len,
|
dtype=tf.float32)
|
if output == 0:
|
return tf.concat(outputs, 2)
|
elif output == 1:
|
return tf.reshape(tf.concat(states,1),(Params.batch_size, shapes[1], 2*units))
|
def pointer_net(passage, passage_len, question, question_len, cell, params, scope = "pointer_network"):
|
'''
|
Answer pointer network as proposed in https://arxiv.org/pdf/1506.03134.pdf.
|
Args:
|
passage: RNN passage output from the bidirectional readout layer (batch_size, timestep, dim)
|
passage_len: variable lengths for passage length
|
question: RNN question output of shape (batch_size, timestep, dim) for question pooling
|
question_len: Variable lengths for question length
|
cell: rnn cell of type RNN_Cell.
|
params: Appropriate weight matrices for attention pooling computation
|
Returns:
|
softmax logits for the answer pointer of the beginning and the end of the answer span
|
'''
|
with tf.variable_scope(scope):
|
weights_q, weights_p = params
|
shapes = passage.get_shape().as_list()
|
initial_state = question_pooling(question, units = Params.attn_size, weights = weights_q, memory_len = question_len, scope = "question_pooling")
|
inputs = [passage, initial_state]
|
p1_logits = attention(inputs, Params.attn_size, weights_p, memory_len = passage_len, scope = "attention")
|
scores = tf.expand_dims(p1_logits, -1)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.