예제 #1
0
def mean_match(mid, pat, mid_mask, pat_mask, keep_prob, is_train):
    pat_d = dropout(pat, keep_prob=keep_prob, is_train=is_train)
    mid_v = mean(mid, mask=mid_mask)
    pat_v = mean(pat, mask=pat_mask)
    pat_v_d = mean(pat_d, mask=pat_mask)
    sur_sim = cosine(mid_v, pat_v, weighted=False)
    pat_sim = cosine(pat_v, pat_v_d, weighted=False)
    return sur_sim, pat_sim
예제 #2
0
def att_match(mid, pat, mid_mask, pat_mask, hidden, keep_prob, is_train):
    mid_d = dropout(mid, keep_prob=keep_prob, is_train=is_train)
    pat_d = dropout(pat, keep_prob=keep_prob, is_train=is_train)
    mid_a = attention(mid_d, hidden, mask=mid_mask)
    pat_a = attention(pat_d, hidden, mask=pat_mask)
    mid_v = tf.reduce_sum(tf.expand_dims(mid_a, axis=2) * mid, axis=1)
    pat_v = tf.reduce_sum(tf.expand_dims(pat_a, axis=2) * pat, axis=1)
    pat_v_d = tf.reduce_sum(tf.expand_dims(pat_a, axis=2) * pat_d, axis=1)
    sur_sim = cosine(mid_v, pat_v_d)
    pat_sim = cosine(pat_v, pat_v_d)
    return sur_sim, pat_sim
예제 #3
0
def lstm_match(mid, pat, mid_mask, pat_mask, mid_len, pat_len, hidden,
               keep_prob, is_train):

    rnn = Cudnn_RNN(num_layers=1, num_units=hidden // 2)
    mid, _ = rnn(mid, seq_len=mid_len, concat_layers=False)
    pat, _ = rnn(pat, seq_len=pat_len, concat_layers=False)

    mid_d = dropout(mid, keep_prob=keep_prob, is_train=is_train)
    pat_d = dropout(pat, keep_prob=keep_prob, is_train=is_train)
    mid_a = attention(mid_d, hidden, mask=mid_mask)
    pat_a = attention(pat_d, hidden, mask=pat_mask)

    mid_v = tf.reduce_sum(tf.expand_dims(mid_a, axis=2) * mid, axis=1)
    pat_v = tf.reduce_sum(tf.expand_dims(pat_a, axis=2) * pat, axis=1)
    pat_v_d = tf.reduce_sum(tf.expand_dims(pat_a, axis=2) * pat_d, axis=1)
    sur_sim = cosine(mid_v, pat_v_d)
    pat_sim = cosine(pat_v, pat_v_d)
    return sur_sim, pat_sim
예제 #4
0
 def model(self, scope='CBOW'):
     config = self.config
     with tf.variable_scope(scope, reuse=tf.AUTO_REUSE):
         with tf.variable_scope('Init'):
             wordmat = tf.concat([
                 tf.zeros([1, config.embedding_dim], tf.float32,
                          name='pad'),
                 tf.get_variable(
                     'unk',
                     shape=[1, config.embedding_dim],
                     dtype=tf.float32,
                     initializer=tf.contrib.layers.xavier_initializer()),
                 tf.get_variable('wordmat',
                                 initializer=tf.constant(self.wordmat,
                                                         dtype=tf.float32))
             ],
                                 axis=0)
         with tf.variable_scope('placeholder'):
             self.input1 = input1 = tf.placeholder(tf.int32, [None, 20],
                                                   'input1')
             self.input2 = input2 = tf.placeholder(tf.int32,
                                                   [None, None, 20],
                                                   'input2')
             len1 = tf.reduce_sum(tf.cast(tf.cast(input1, tf.bool),
                                          tf.float32),
                                  axis=1,
                                  keep_dims=True)
             len2 = tf.reduce_sum(tf.cast(tf.cast(input2, tf.bool),
                                          tf.float32),
                                  axis=2,
                                  keep_dims=True)
             numtag1 = tf.reduce_sum(
                 tf.cast(tf.cast(tf.squeeze(len1), tf.bool), tf.float32))
             tag_mask = tf.tile(
                 tf.expand_dims(tf.cast(tf.cast(tf.squeeze(len2), tf.bool),
                                        tf.float32),
                                axis=1), [1, tf.shape(self.input1)[0], 1])
         with tf.variable_scope('Emb'):
             emb1 = tf.nn.embedding_lookup(wordmat, input1)
             self.emb1 = emb1 = tf.div(tf.reduce_sum(emb1, axis=1),
                                       len1 + 1e-8,
                                       name='emb1')
             emb1 = tf.tile(tf.expand_dims(emb1, axis=0),
                            [tf.shape(self.input2)[0], 1, 1])
             emb2 = tf.nn.embedding_lookup(wordmat, input2)
             self.emb2 = emb2 = tf.div(tf.reduce_sum(emb2, axis=2),
                                       len2 + 1e-8,
                                       name='emb2')
         with tf.variable_scope('Sim'):
             self.sim = cosine(emb1, emb2) - 2 * (1 - tag_mask)
             self.mean_sim = tf.div(tf.reduce_sum(tf.reduce_max(self.sim,
                                                                axis=2),
                                                  axis=1),
                                    numtag1,
                                    name='mean_sim')
예제 #5
0
def FIND_module(sent,pats,word_mat,config,is_train,rnn,scope='Find_module'):#sents_emb [batch,maxlength_sent] pats [num_pats,maxlength_pat]   [batch,maxlength_sent,dim]
    with tf.variable_scope(scope,reuse=tf.AUTO_REUSE):
        keep_prob = config.keep_prob
        d = config.hidden
        batch_size = tf.shape(sent)[0]
        maxlength_sent = tf.shape(sent)[1]
        dim = tf.shape(word_mat)[1]
        num_pats = tf.shape(pats)[0]

        sent_mask = tf.cast(sent, tf.bool)

        pat_mask = tf.cast(pats, tf.bool)
        pat_len = tf.reduce_sum(tf.cast(pat_mask, tf.int32), axis=1)

        with tf.variable_scope('embedding'):
            sent_emb = tf.nn.embedding_lookup(word_mat, sent)
            sent_emb_d = dropout(sent_emb, keep_prob=config.word_keep_prob, is_train=is_train, mode="embedding")
            pat_emb = tf.nn.embedding_lookup(word_mat, pats)
            pat_emb_d = dropout(pat_emb, keep_prob=config.word_keep_prob, is_train=is_train,mode='embedding')

        with tf.variable_scope('stack'):
            pad = tf.zeros([batch_size,1,dim],tf.float32)

            sent_emb_pad = tf.concat([pad,sent_emb,pad],axis=1)
            sent_emb_stack_2 = tf.reshape(sent_emb_pad,[batch_size,maxlength_sent+2,1,dim])
            sent_emb_stack_2 = tf.concat([sent_emb_stack_2[:,0:-1,:],sent_emb_stack_2[:,1:,:]],axis=2)
            sent_emb_stack_2 = tf.reshape(sent_emb_stack_2,[batch_size*(maxlength_sent+1),2,dim])

            sent_emb_pad2 = tf.concat([pad,pad,sent_emb,pad,pad],axis=1)
            sent_emb_stack_3 = tf.reshape(sent_emb_pad2,[batch_size,maxlength_sent+4,1,dim])
            sent_emb_stack_3 = tf.concat([sent_emb_stack_3[:, 0:-2, :], sent_emb_stack_3[:, 1:-1, :], sent_emb_stack_3[:, 2:, :]], axis=2)
            sent_emb_stack_3 = tf.reshape(sent_emb_stack_3,[batch_size*(maxlength_sent+2),3,dim])

            sent_emb_stack_1 = tf.reshape(sent_emb,[batch_size*maxlength_sent,1,dim])

        with tf.variable_scope('stack_d'):
            pad = tf.zeros([batch_size,1,dim],tf.float32)

            sent_emb_pad_d = tf.concat([pad,sent_emb_d,pad],axis=1)
            sent_emb_stack_2_d = tf.reshape(sent_emb_pad_d,[batch_size,maxlength_sent+2,1,dim])
            sent_emb_stack_2_d = tf.concat([sent_emb_stack_2_d[:,0:-1,:],sent_emb_stack_2_d[:,1:,:]],axis=2)
            sent_emb_stack_2_d = tf.reshape(sent_emb_stack_2_d,[batch_size*(maxlength_sent+1),2,dim])

            sent_emb_pad2_d = tf.concat([pad,pad,sent_emb_d,pad,pad],axis=1)
            sent_emb_stack_3_d = tf.reshape(sent_emb_pad2_d,[batch_size,maxlength_sent+4,1,dim])
            sent_emb_stack_3_d = tf.concat([sent_emb_stack_3_d[:, 0:-2, :], sent_emb_stack_3_d[:, 1:-1, :], sent_emb_stack_3_d[:, 2:, :]], axis=2)
            sent_emb_stack_3_d = tf.reshape(sent_emb_stack_3_d,[batch_size*(maxlength_sent+2),3,dim])

            sent_emb_stack_1_d = tf.reshape(sent_emb_d,[batch_size*maxlength_sent,1,dim])

        with tf.variable_scope("encoder"):
            with tf.variable_scope('encode_pat'):
                pat, _ = rnn(pat_emb, seq_len=pat_len, concat_layers=False)       #[numpats,d]
                pat_d = dropout(pat, keep_prob=config.keep_prob, is_train=is_train)
            with tf.variable_scope('encode_sent'):
                cont_stack_3, _ = rnn(sent_emb_stack_3,seq_len=3 * tf.ones([batch_size * (maxlength_sent + 2)], tf.int32),concat_layers=False)
                cont_stack_2, _ = rnn(sent_emb_stack_2, seq_len=2*tf.ones([batch_size*(maxlength_sent+1)],tf.int32), concat_layers=False)  #[batch_size*(maxlength_sent+1),d]
                cont_stack_1, _ = rnn(sent_emb_stack_1, seq_len=tf.ones([batch_size*maxlength_sent],tf.int32), concat_layers=False)  #[batch_size*maxlength_sent,d]
                cont_stack_3_d = dropout(cont_stack_3, keep_prob=keep_prob, is_train=is_train)
                cont_stack_2_d = dropout(cont_stack_2, keep_prob=keep_prob, is_train=is_train)
                cont_stack_1_d = dropout(cont_stack_1, keep_prob=keep_prob, is_train=is_train)

        with tf.variable_scope('attention'):
            pat_d_a = attention(pat_d,config.att_hidden, mask=pat_mask)
            cont_stack_2_d_a = attention(cont_stack_2_d,config.att_hidden)
            cont_stack_3_d_a = attention(cont_stack_3_d,config.att_hidden)

            cont_stack_3_att = tf.reduce_sum(tf.expand_dims(cont_stack_3_d_a, axis=2) * cont_stack_3, axis=1)
            cont_stack_2_att = tf.reduce_sum(tf.expand_dims(cont_stack_2_d_a, axis=2) * cont_stack_2, axis=1)
            pat_d_att = tf.reduce_sum(tf.expand_dims(pat_d_a, axis=2) * pat_d, axis=1)
            pat_att = tf.reduce_sum(tf.expand_dims(pat_d_a, axis=2) * pat, axis=1)
            cont_stack_1_att = tf.squeeze(cont_stack_1)
        with tf.variable_scope('emb_attention'):
            pat_emb_d_a = attention(pat_emb_d, config.att_hidden, mask=pat_mask)
            pat_emb_d_att = tf.reduce_sum(tf.expand_dims(pat_emb_d_a, axis=2) * pat_emb_d, axis=1)
            pat_emb_att = tf.reduce_sum(tf.expand_dims(pat_emb_d_a, axis=2) * pat_emb, axis=1)

            sent_emb_stack_3_d_a = attention(sent_emb_stack_3_d, config.att_hidden)
            sent_emb_stack_3_att = tf.reduce_sum(tf.expand_dims(sent_emb_stack_3_d_a, axis=2) * sent_emb_stack_3, axis=1)

            sent_emb_stack_2_d_a = attention(sent_emb_stack_2_d, config.att_hidden)
            sent_emb_stack_2_att = tf.reduce_sum(tf.expand_dims(sent_emb_stack_2_d_a, axis=2) * sent_emb_stack_2, axis=1)

            sent_emb_stack_1_att = tf.squeeze(sent_emb_stack_1)

        with tf.variable_scope('Score'):
            scores_stack_2 = cosine(cont_stack_2_att,pat_d_att,weighted=False)
            scores_stack_1 = cosine(cont_stack_1_att,pat_d_att,weighted=False)
            scores_stack_3 = cosine(cont_stack_3_att, pat_d_att, weighted=False)

            scores_stack_3 = tf.reshape(scores_stack_3, [batch_size, 1, maxlength_sent + 2, num_pats])
            scores_stack_2 = tf.reshape(scores_stack_2,[batch_size,1,maxlength_sent+1,num_pats])
            scores_stack_1 = tf.reshape(scores_stack_1,[batch_size,1,maxlength_sent,num_pats])
            scores_sim = cosine(pat_att, pat_d_att, weighted=False)

        with tf.variable_scope('emb_Score'):
            scores_stack_3_emb = cosine(sent_emb_stack_3_att,pat_emb_d_att)
            scores_stack_2_emb = cosine(sent_emb_stack_2_att,pat_emb_d_att)
            scores_stack_1_emb = cosine(sent_emb_stack_1_att,pat_emb_d_att)

            scores_stack_3_emb = tf.reshape(scores_stack_3_emb, [batch_size, 1, maxlength_sent + 2, num_pats])
            scores_stack_2_emb = tf.reshape(scores_stack_2_emb,[batch_size,1,maxlength_sent+1,num_pats])
            scores_stack_1_emb = tf.reshape(scores_stack_1_emb,[batch_size,1,maxlength_sent,num_pats])

            phi = 0
            scores_stack_3 = phi * scores_stack_3_emb + (1 - phi) * scores_stack_3
            scores_stack_2 = phi*scores_stack_2_emb+(1-phi)*scores_stack_2
            scores_stack_1 = phi*scores_stack_1_emb+(1-phi)*scores_stack_1

            scores = tf.concat([scores_stack_3[:,:,0:-2,:],scores_stack_3[:,:,1:-1,:],scores_stack_3[:,:,2:,:],scores_stack_2[:,:,0:-1,:],scores_stack_2[:,:,1:,:],scores_stack_1],axis=1)
            scores = tf.reshape(scores,[batch_size,6,maxlength_sent,num_pats])
            scores = tf.transpose(scores,[0,3,1,2])
            scores = tf.reshape(scores,[batch_size*num_pats,6,maxlength_sent])

            scores_sim_emb = cosine(pat_emb_att, pat_emb_d_att)
            scores_sim = phi*scores_sim_emb+(1-phi)*scores_sim

        with tf.variable_scope('SeqLabel'):
            seq = tf.layers.dense(tf.transpose(scores,[0,2,1]),1)
            seq = tf.squeeze(seq)
            seq = tf.reshape(seq,[batch_size,num_pats,maxlength_sent])
            #seq = tf.reshape(tf.reduce_max(scores,axis=1),[batch_size,num_pats,maxlength_sent])
            seq = tf.transpose(seq,[0,2,1])
            seq = seq*tf.tile(tf.cast(tf.reshape(sent_mask,[batch_size,maxlength_sent,1]),tf.float32),[1,1,num_pats])

        return seq,scores_sim