def multi_perspective_match(feature_dim, repres1, repres2, is_training=True, dropout_rate=0.2, options=None, scope_name='mp-match', reuse=False): ''' :param repres1: [batch_size, len, feature_dim] :param repres2: [batch_size, len, feature_dim] :return: ''' input_shape = tf.shape(repres1) batch_size = input_shape[0] seq_length = input_shape[1] matching_result = [] with tf.variable_scope(scope_name, reuse=reuse): match_dim = 0 if options.with_cosine: cosine_value = layer_utils.cosine_distance(repres1, repres2, cosine_norm=False) cosine_value = tf.reshape(cosine_value, [batch_size, seq_length, 1]) matching_result.append(cosine_value) match_dim += 1 if options.with_mp_cosine: mp_cosine_params = tf.get_variable("mp_cosine", shape=[options.cosine_MP_dim, feature_dim], dtype=tf.float32) mp_cosine_params = tf.expand_dims(mp_cosine_params, axis=0) mp_cosine_params = tf.expand_dims(mp_cosine_params, axis=0) repres1_flat = tf.expand_dims(repres1, axis=2) repres2_flat = tf.expand_dims(repres2, axis=2) mp_cosine_matching = layer_utils.cosine_distance(tf.multiply(repres1_flat, mp_cosine_params), repres2_flat, cosine_norm=False) matching_result.append(mp_cosine_matching) match_dim += options.cosine_MP_dim matching_result = tf.concat(axis=2, values=matching_result) return (matching_result, match_dim)
def comU1(x, y): result = [ layer_utils.cosine_distance(x, y), layer_utils.euclidean_distance(x, y), layer_utils.l1_distance(x, y) ] # , compute_euclidean_distance(x, y), compute_l1_distance(x, y) return tf.stack(result, axis=1)
def singel_instance(x): p = x[0] q = x[1] # p: [pasasge_len, dim], q: [question_len, dim] p = multi_perspective_expand_for_2D(p, decompose_params) # [pasasge_len, decompose_dim, dim] q = multi_perspective_expand_for_2D(q, decompose_params) # [question_len, decompose_dim, dim] p = tf.expand_dims(p, 1) # [pasasge_len, 1, decompose_dim, dim] q = tf.expand_dims(q, 0) # [1, question_len, decompose_dim, dim] return layer_utils.cosine_distance(p, q) # [passage_len, question_len, decompose]
def cal_relevancy_matrix(self, in_question_repres, in_passage_repres): in_question_repres_tmp = tf.expand_dims( in_question_repres, 1) # [batch_size, 1, question_len, dim] in_passage_repres_tmp = tf.expand_dims( in_passage_repres, 2) # [batch_size, passage_len, 1, dim] relevancy_matrix = layer_utils.cosine_distance( in_question_repres_tmp, in_passage_repres_tmp) # [batch_size, passage_len, question_len] return relevancy_matrix
def multi_perspective_match(feature_dim, repres1, repres2, is_training=True, dropout_rate=0.2, options=None, scope_name='mp-match', reuse=False): ''' :param repres1: [batch_size, len, feature_dim] :param repres2: [batch_size, len, feature_dim] :return: ''' repres1 = layer_utils.dropout_layer(repres1, dropout_rate, is_training=is_training) repres2 = layer_utils.dropout_layer(repres2, dropout_rate, is_training=is_training) input_shape = tf.shape(repres1) batch_size = input_shape[0] seq_length = input_shape[1] matching_result = [] cosine_norm = True with tf.variable_scope(scope_name, reuse=reuse): match_dim = 0 if options.with_cosine: cosine_value = layer_utils.cosine_distance(repres1, repres2, cosine_norm=cosine_norm) cosine_value = tf.reshape(cosine_value, [batch_size, seq_length, 1]) matching_result.append(cosine_value) match_dim += 1 concat_rep = tf.concat(axis=2, values=[repres1, repres2]) if options.with_nn_match: nn_match_W = tf.get_variable( "nn_match_W", [2 * feature_dim, options.nn_match_dim], dtype=tf.float32) nn_match_b = tf.get_variable("nn_match_b", [options.nn_match_dim], dtype=tf.float32) cur_rep = tf.reshape(concat_rep, [batch_size * seq_length, 2 * feature_dim]) cur_match_result = tf.tanh( tf.matmul(cur_rep, nn_match_W) + nn_match_b) cur_match_result = tf.reshape( cur_match_result, [batch_size, seq_length, options.nn_match_dim]) matching_result.append(cur_match_result) match_dim += options.nn_match_dim if options.with_mp_cosine: if options.mp_cosine_proj_dim > 0: mp_cosine_projection = tf.get_variable( "mp_cosine_projection", [feature_dim, options.mp_cosine_proj_dim], dtype=tf.float32) mp_cosine_params = tf.get_variable( "mp_cosine", shape=[ 1, options.cosine_MP_dim, options.mp_cosine_proj_dim ], dtype=tf.float32) repres1_flat = tf.reshape( repres1, [batch_size * seq_length, feature_dim]) repres2_flat = tf.reshape( repres2, [batch_size * seq_length, feature_dim]) repres1_flat = tf.tanh( tf.matmul(repres1_flat, mp_cosine_projection)) repres2_flat = tf.tanh( tf.matmul(repres2_flat, mp_cosine_projection)) repres1_flat = tf.expand_dims(repres1_flat, axis=1) repres2_flat = tf.expand_dims(repres2_flat, axis=1) mp_cosine_matching = layer_utils.cosine_distance( tf.multiply(repres1_flat, mp_cosine_params), repres2_flat, cosine_norm=cosine_norm) mp_cosine_matching = tf.reshape( mp_cosine_matching, [batch_size, seq_length, options.cosine_MP_dim]) else: mp_cosine_params = tf.get_variable( "mp_cosine", shape=[1, 1, options.cosine_MP_dim, feature_dim], dtype=tf.float32) repres1_flat = tf.expand_dims(repres1, axis=2) repres2_flat = tf.expand_dims(repres2, axis=2) mp_cosine_matching = layer_utils.cosine_distance( tf.multiply(repres1_flat, mp_cosine_params), repres2_flat, cosine_norm=cosine_norm) matching_result.append(mp_cosine_matching) match_dim += options.cosine_MP_dim if options.with_match_lstm: (_, _, match_lstm_result) = layer_utils.my_lstm_layer( concat_rep, options.match_lstm_dim, scope_name="match_lstm", reuse=False, is_training=is_training, dropout_rate=dropout_rate) matching_result.append(match_lstm_result) match_dim += 2 * options.match_lstm_dim matching_result = tf.concat(axis=2, values=matching_result) return (matching_result, match_dim)