コード例 #1
0
ファイル: mm_dnn.py プロジェクト: jiahenghuang/ElegantNlp
    def predict(self, left_slots, right_slots):
        """
        predict graph of this net
        """
        left = left_slots[self.left_name]
        right = right_slots[self.right_name]
        left_emb = self.emb_layer.ops(left) # (N, len, D)
        right_emb = self.emb_layer.ops(right) # (N, len, D)
        ## left
        bi_left_outputs, _ = tf.nn.bidirectional_dynamic_rnn(self.fw_cell, self.bw_cell, left_emb,
                                                             sequence_length=utility.seq_length(left), dtype=tf.float32)
        left_seq_encoder = tf.concat(bi_left_outputs, -1)
        ## right
        bi_right_outputs, _ = tf.nn.bidirectional_dynamic_rnn(self.fw_cell, self.bw_cell, right_emb,
                                                              sequence_length=utility.seq_length(right), dtype=tf.float32)
        right_seq_encoder = tf.concat(bi_right_outputs, -1)

        cross = tf.matmul(left_seq_encoder, tf.transpose(right_seq_encoder, [0, 2, 1])) # (N, len, len)
        if self.match_mask:
            cross_mask = utility.get_cross_mask(left, right)
        else:
            cross_mask = None
        conv_pool = self.cnn_layer.ops(cross, mask=cross_mask)
        pool_relu = self.relu_layer.ops(conv_pool)
        relu_hid1 = self.fc1_layer.ops(pool_relu)
        hid1_tanh = self.tanh_layer.ops(relu_hid1)
        pred = self.fc2_layer.ops(hid1_tanh)
        return pred
コード例 #2
0
ファイル: lstm.py プロジェクト: Inistlwq/text_similarity
 def predict(self, left_slots, right_slots):
     """
     predict graph of this net
     """
     left = left_slots[self.left_name]
     right = right_slots[self.right_name]
     left_emb = self.emb_layer.ops(left)  # (N, len, D)
     right_emb = self.emb_layer.ops(right)  # (N, len, D)
     ## left
     left_length = seq_length(left)  #得到实际的长度
     left_encoder = self.rnn.ops(left_emb, left_length)
     left_rep = self.extract.ops(left_encoder, left_length)  #获得最后的隐藏层
     right_length = seq_length(right)
     right_encoder = self.rnn.ops(right_emb, right_length)
     right_rep = self.extract.ops(right_encoder, right_length)
     if self.task_mode == "pointwise":
         rep_concat = tf.concat([left_rep, right_rep], -1)
         hidden1 = self.fc1_layer.ops(rep_concat)
         hidden1_relu = self.relu_layer.ops(hidden1)
         pred = self.fc2_layer.ops(hidden1_relu)
     elif self.task_mode == "pairwise":
         left_hidden1 = self.fc1_layer.ops(left_rep)
         right_hidden1 = self.fc1_layer.ops(right_rep)
         pred = self.cos_layer.ops(left_hidden1, right_hidden1)
     return pred
コード例 #3
0
 def predict(self, left_slots, right_slots):
     """
     predict graph of this net
     """
     left = left_slots[self.left_name]
     right = right_slots[self.right_name]
     left_emb = self.emb_layer.ops(left)  # (N, len, D)
     right_emb = self.emb_layer.ops(right)  # (N, len, D)
     ## left
     left_length = seq_length(left)
     left_encoder = self.rnn.ops(left_emb, left_length)
     left_rep = self.extract.ops(left_encoder, left_length)
     right_length = seq_length(right)
     right_encoder = self.rnn.ops(right_emb, right_length)
     right_rep = self.extract.ops(right_encoder, right_length)
     rep_concat = tf.concat([left_rep, right_rep], -1)
     hidden1 = self.fc1_layer.ops(rep_concat)
     pred = self.fc2_layer.ops(hidden1)
     return pred
コード例 #4
0
    def predict(self, left_slots, right_slots):
        """
        predict graph of this net
        """
        left = left_slots[self.left_name]
        right = right_slots[self.right_name]
        left_emb = self.emb_layer.ops(left)  # (N, len, D)
        right_emb = self.emb_layer.ops(right)  # (N, len, D)
        ## left
        bi_left_outputs, _ = tf.nn.bidirectional_dynamic_rnn(
            self.fw_cell,
            self.bw_cell,
            left_emb,
            sequence_length=seq_length(left),
            dtype=tf.float32)
        left_seq_encoder = tf.concat(bi_left_outputs, -1)
        ## right
        bi_right_outputs, _ = tf.nn.bidirectional_dynamic_rnn(
            self.fw_cell,
            self.bw_cell,
            right_emb,
            sequence_length=seq_length(right),
            dtype=tf.float32)
        right_seq_encoder = tf.concat(bi_right_outputs, -1)

        #进行交互运算
        cross = tf.matmul(left_seq_encoder,
                          tf.transpose(right_seq_encoder,
                                       [0, 2, 1]))  # (N, len, len)
        #if self.match_mask:
        #    cross_mask = get_cross_mask(left, right)
        #    cross = tf.multiply(cross, tf.cast(cross_mask, tf.float32))
        #    #paddings = tf.ones_like(cross)*(-2**32+1)
        #    #cross = tf.where(tf.equal(cross_mask, 0), paddings, cross)
        cross_reshape = tf.reshape(cross, [-1, self.seq_len1 * self.seq_len2])
        #返回values,indices
        k_max_match = tf.nn.top_k(cross_reshape, k=self.k_max_num,
                                  sorted=True)[0]
        #k_max_match = tf.Print(k_max_match, [k_max_match], "k_max_match")
        pred = self.fc2_layer.ops(k_max_match)
        return pred