def attentive_matching(input_sentence, att_matrix, weights):
"""
Parameters
----------
input_sentence: Tensor
Tensor of shape (batch_size, num_sentence_words, rnn_hidden_dim)
att_matrix: Tensor
Tensor of shape (batch_size, num_sentence_words, rnn_hidden_dim)
"""
def single_instance(inputs):
# Shapes: (num_sentence_words, rnn_hidden_dim)
sentence_a_single = inputs[0]
sentence_b_single_att = inputs[1]
# Shapes: (num_sentence_words, multiperspective_dims, rnn_hidden_dim)
expanded_sentence_a_single = multi_perspective_expand_for_2D(
sentence_a_single, weights)
expanded_sentence_b_single_att = multi_perspective_expand_for_2D(
sentence_b_single_att, weights)
# Shape: (num_sentence_words, multiperspective_dims)
return cosine_distance(expanded_sentence_a_single,
expanded_sentence_b_single_att)
elems = (input_sentence, att_matrix)
# Shape: (batch_size, num_sentence_words, multiperspective_dims)
return tf.map_fn(single_instance, elems, dtype="float")
评论列表
文章目录