當前位置: 首頁>>代碼示例>>Python>>正文


Python crf.viterbi_decode方法代碼示例

本文整理匯總了Python中tensorflow.contrib.crf.viterbi_decode方法的典型用法代碼示例。如果您正苦於以下問題:Python crf.viterbi_decode方法的具體用法?Python crf.viterbi_decode怎麽用?Python crf.viterbi_decode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在tensorflow.contrib.crf的用法示例。


在下文中一共展示了crf.viterbi_decode方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test

# 需要導入模塊: from tensorflow.contrib import crf [as 別名]
# 或者: from tensorflow.contrib.crf import viterbi_decode [as 別名]
def test(self, sentence):
        feed_dict = dict()
        saver = tensorflow.train.Saver()
        with tensorflow.Session() as sess:
            cp = tensorflow.train.get_checkpoint_state(checkpoint_dir=self.model_path)
            saver.restore(sess, save_path=cp.model_checkpoint_path)
            label = [0.0] * len(sentence)
            feed_dict[word_ids] = sentence
            feed_dict[seq_length] = [len(sentence)]
            feed_dict[labels] = [label]
            feed_dict[lr_pl] = self.lr
            feed_dict[dropout_pl] = self.dropout_keep_prob
            tag, transition = sess.run(fetches=[logic, transition_params], feed_dict=feed_dict)
            tags = []
            for t in tag[0]:
                [vite], _ = viterbi_decode(t[:len(sentence)], transition_params)
                tags.append(vite)
            print(tags)
        return tags 
開發者ID:jtyoui,項目名稱:Jtyoui,代碼行數:21,代碼來源:LSTM_CRF.py

示例2: decode

# 需要導入模塊: from tensorflow.contrib import crf [as 別名]
# 或者: from tensorflow.contrib.crf import viterbi_decode [as 別名]
def decode(self, logits, lengths, matrix):
        """
        :param logits: [batch_size, num_steps, num_tags]float32, logits
        :param lengths: [batch_size]int32, real length of each sequence
        :param matrix: transaction matrix for inference
        :return:
        """
        # inference final labels usa viterbi Algorithm
        paths = []
        small = -1000.0
        start = np.asarray([[small] * self.num_tags + [0]])
        for score, length in zip(logits, lengths):
            score = score[:length]
            pad = small * np.ones([length, 1])
            logits = np.concatenate([score, pad], axis=1)
            logits = np.concatenate([start, logits], axis=0)
            path, _ = viterbi_decode(logits, matrix)

            paths.append(path[1:])
        return paths 
開發者ID:koala-ai,項目名稱:tensorflow_nlp,代碼行數:22,代碼來源:model.py

示例3: predict_one_batch

# 需要導入模塊: from tensorflow.contrib import crf [as 別名]
# 或者: from tensorflow.contrib.crf import viterbi_decode [as 別名]
def predict_one_batch(self, sess, seqs):
        """

        :param sess:
        :param seqs:
        :return: label_list
                 seq_len_list
        """
        feed_dict, seq_len_list = self.get_feed_dict(seqs, dropout=1.0)

        if self.CRF:
            logits, transition_params = sess.run([self.logits, self.transition_params],
                                                 feed_dict=feed_dict)
            label_list = []
            for logit, seq_len in zip(logits, seq_len_list):
                viterbi_seq, _ = viterbi_decode(logit[:seq_len], transition_params)
                label_list.append(viterbi_seq)
            return label_list, seq_len_list

        else:
            label_list = sess.run(self.labels_softmax_, feed_dict=feed_dict)
            return label_list, seq_len_list 
開發者ID:baiyyang,項目名稱:medical-entity-recognition,代碼行數:24,代碼來源:model.py

示例4: decode

# 需要導入模塊: from tensorflow.contrib import crf [as 別名]
# 或者: from tensorflow.contrib.crf import viterbi_decode [as 別名]
def decode(self, logits, lengths, matrix):
        """
        :param logits: [batch_size, num_steps, num_tags]float32, logits
        :param lengths: [batch_size]int32, real length of each sequence
        :param matrix: transaction matrix for inference
        :return:
        """
        # inference final labels usa viterbi Algorithm
        paths = []
        small = -1000.0
        start = np.asarray([[small]*self.num_tags +[0]])
        for score, length in zip(logits, lengths):
            score = score[:length]
            pad = small * np.ones([length, 1])
            logits = np.concatenate([score, pad], axis=1)
            logits = np.concatenate([start, logits], axis=0)
            path, _ = viterbi_decode(logits, matrix)

            paths.append(path[1:])
        return paths 
開發者ID:nlpdz,項目名稱:Medical-Named-Entity-Rec-Based-on-Dilated-CNN,代碼行數:22,代碼來源:model.py

示例5: decode

# 需要導入模塊: from tensorflow.contrib import crf [as 別名]
# 或者: from tensorflow.contrib.crf import viterbi_decode [as 別名]
def decode(self, logits, lengths, matrix):
        """
        :param logits: [batch_size, num_steps, num_tags]float32, logits
        :param lengths: [batch_size]int32, real length of each sequence
        :param matrix: transaction matrix for inference
        :return:
        """
        # inference final labels usa viterbi Algorithm
        paths = []
        small = -1000.0
        start = np.asarray([[small]*self.num_tags + [0]])
        for score, length in zip(logits, lengths):
            score = score[:length]
            pad = small * np.ones([length, 1])
            logits = np.concatenate([score, pad], axis=1)
            logits = np.concatenate([start, logits], axis=0)
            path, _ = viterbi_decode(logits, matrix)

            paths.append(path[1:])
        return paths 
開發者ID:GaoQ1,項目名稱:rasa_nlu_gq,代碼行數:22,代碼來源:model.py

示例6: inference

# 需要導入模塊: from tensorflow.contrib import crf [as 別名]
# 或者: from tensorflow.contrib.crf import viterbi_decode [as 別名]
def inference(self, scores, sequence_lengths=None):
        """
        Inference label sequence given scores.
        If transitions is given, then perform veterbi search, else perform greedy search.

        Args:
            scores: A numpy array with shape (batch, max_length, num_tags).
            sequence_lengths: A numpy array with shape (batch,).

        Returns:
            A numpy array with shape (batch, max_length).
        """

        if not self.parameters['use_crf']:
            return np.argmax(scores, 2)
        else:
            with tf.variable_scope(self.scope, reuse=True):
                transitions = tf.get_variable('transitions').eval(session=self.sess)
            paths = np.zeros(scores.shape[:2], dtype=INT_TYPE)
            for i in xrange(scores.shape[0]):
                tag_score, length = scores[i], sequence_lengths[i]
                if length == 0:
                    continue
                path, _ = crf.viterbi_decode(tag_score[:length], transitions)
                paths[i, :length] = path
            return paths 
開發者ID:chqiwang,項目名稱:convseg,代碼行數:28,代碼來源:tagger.py

示例7: decode

# 需要導入模塊: from tensorflow.contrib import crf [as 別名]
# 或者: from tensorflow.contrib.crf import viterbi_decode [as 別名]
def decode(self, logits, lengths, matrix):
        # inference final labels usa viterbi Algorithm
        paths = []
        small = -1000.0
        start = np.asarray([[small] * self.num_tags + [0]])
        for score, length in zip(logits, lengths):
            score = score[:length]
            pad = small * np.ones([length, 1])
            logits = np.concatenate([score, pad], axis=1)
            logits = np.concatenate([start, logits], axis=0)
            path, _ = viterbi_decode(logits, matrix)

            paths.append(path[1:])
        return paths 
開發者ID:koala-ai,項目名稱:tensorflow_nlp,代碼行數:16,代碼來源:model.py

示例8: viterbi_decode

# 需要導入模塊: from tensorflow.contrib import crf [as 別名]
# 或者: from tensorflow.contrib.crf import viterbi_decode [as 別名]
def viterbi_decode(logits, trans_params, seq_len):
        viterbi_sequences = []
        for logit, lens in zip(logits, seq_len):
            logit = logit[:lens]  # keep only the valid steps
            viterbi_seq, viterbi_score = viterbi_decode(logit, trans_params)
            viterbi_sequences += [viterbi_seq]
        return viterbi_sequences 
開發者ID:IsaacChanghau,項目名稱:neural_sequence_labeling,代碼行數:9,代碼來源:base_model.py

示例9: _predict_op

# 需要導入模塊: from tensorflow.contrib import crf [as 別名]
# 或者: from tensorflow.contrib.crf import viterbi_decode [as 別名]
def _predict_op(self, data):
        feed_dict = self._get_feed_dict(data)
        if self.cfg["use_crf"]:
            logits, trans_params, seq_len = self.sess.run([self.logits, self.trans_params, self.seq_len],
                                                          feed_dict=feed_dict)
            return self.viterbi_decode(logits, trans_params, seq_len)
        else:
            pred_logits = tf.cast(tf.argmax(self.logits, axis=-1), tf.int32)
            logits = self.sess.run(pred_logits, feed_dict=feed_dict)
            return logits 
開發者ID:IsaacChanghau,項目名稱:neural_sequence_labeling,代碼行數:12,代碼來源:base_model.py

示例10: decode

# 需要導入模塊: from tensorflow.contrib import crf [as 別名]
# 或者: from tensorflow.contrib.crf import viterbi_decode [as 別名]
def decode(self, scores, lengths, trans):
        paths = []
        for score, length in zip(scores, lengths):
            path, _ = viterbi_decode(score, trans)
            paths.append(path)
        return paths 
開發者ID:sladesha,項目名稱:deep_learning,代碼行數:8,代碼來源:model.py

示例11: vdecode

# 需要導入模塊: from tensorflow.contrib import crf [as 別名]
# 或者: from tensorflow.contrib.crf import viterbi_decode [as 別名]
def vdecode(logits, trans, sequence_lengths, tag_num):
    viterbi_sequences = []
    small = -1000.0
    start = np.asarray([[small] * tag_num + [0]])
    for logit, length in zip(logits, sequence_lengths):
        score = logit[:length]
        pad = small * np.ones([length, 1])
        score = np.concatenate([score, pad], axis=1)
        score = np.concatenate([start, score], axis=0)
        viterbi_seq, viterbi_score = viterbi_decode(score, trans)
        viterbi_sequences.append(viterbi_seq[1:])

    return viterbi_sequences 
開發者ID:rockyzhengwu,項目名稱:FoolNLTK,代碼行數:15,代碼來源:decode.py

示例12: decode

# 需要導入模塊: from tensorflow.contrib import crf [as 別名]
# 或者: from tensorflow.contrib.crf import viterbi_decode [as 別名]
def decode(logits, trans, sequence_lengths, tag_num):
    viterbi_sequences = []
    small = -1000.0
    start = np.asarray([[small] * tag_num + [0]])
    for logit, length in zip(logits, sequence_lengths):
        score = logit[:length]
        pad = small * np.ones([length, 1])
        logits = np.concatenate([score, pad], axis=1)
        logits = np.concatenate([start, logits], axis=0)
        viterbi_seq, viterbi_score = viterbi_decode(logits, trans)
        viterbi_sequences += [viterbi_seq]
    return viterbi_sequences 
開發者ID:rockyzhengwu,項目名稱:FoolNLTK,代碼行數:14,代碼來源:export_model.py

示例13: decode

# 需要導入模塊: from tensorflow.contrib import crf [as 別名]
# 或者: from tensorflow.contrib.crf import viterbi_decode [as 別名]
def decode(logits, trans, sequence_lengths, tag_num):
    viterbi_sequences = []
    small = -1000.0
    start = np.asarray([[small] * tag_num + [0]])
    for logit, length in zip(logits, sequence_lengths):
        score = logit[:length]
        pad = small * np.ones([length, 1])
        score = np.concatenate([score, pad], axis=1)
        score = np.concatenate([start, score], axis=0)
        viterbi_seq, viterbi_score = viterbi_decode(score, trans)
        viterbi_sequences.append(viterbi_seq[1:])
    return viterbi_sequences 
開發者ID:rockyzhengwu,項目名稱:FoolNLTK,代碼行數:14,代碼來源:predictor.py

示例14: decode

# 需要導入模塊: from tensorflow.contrib import crf [as 別名]
# 或者: from tensorflow.contrib.crf import viterbi_decode [as 別名]
def decode(logits, trans, sequence_lengths, tag_num):
    viterbi_sequences = []
    small = -1000.0
    start = np.asarray([[small] * tag_num + [0]])
    for logit, length in zip(logits, sequence_lengths):
        score = logit[:length]
        pad = small * np.ones([length, 1])
        logits = np.concatenate([score, pad], axis=1)
        logits = np.concatenate([start, logits], axis=0)
        viterbi_seq, viterbi_score = viterbi_decode(logits, trans)
        viterbi_sequences .append(viterbi_seq[1:])
    return viterbi_sequences 
開發者ID:rockyzhengwu,項目名稱:FoolNLTK,代碼行數:14,代碼來源:model.py


注:本文中的tensorflow.contrib.crf.viterbi_decode方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。