当前位置: 首页>>代码示例>>Python>>正文


Python crf.crf_decode方法代码示例

本文整理汇总了Python中tensorflow.contrib.crf.crf_decode方法的典型用法代码示例。如果您正苦于以下问题:Python crf.crf_decode方法的具体用法?Python crf.crf_decode怎么用?Python crf.crf_decode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tensorflow.contrib.crf的用法示例。


在下文中一共展示了crf.crf_decode方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: decode

# 需要导入模块: from tensorflow.contrib import crf [as 别名]
# 或者: from tensorflow.contrib.crf import crf_decode [as 别名]
def decode(self, unary, lengths):
        """Do Viterbi decode on a batch.

        :param unary: torch.FloatTensor: [T, B, N] or [B, T, N]
        :param lengths: torch.LongTensor: [B]

        :return: List[torch.LongTensor]: [B] the paths
        :return: torch.FloatTensor: [B] the path score
        """
        bsz = tf.shape(unary)[0]
        lsz = self.num_tags
        np_gos = np.full((1, 1, lsz), -1e4, dtype=np.float32)
        np_gos[:, :, Offsets.GO] = 0
        gos = tf.constant(np_gos)

        start = tf.tile(gos, [bsz, 1, 1])
        start = tf.nn.log_softmax(start, axis=-1)

        probv = tf.concat([start, unary], axis=1)

        viterbi, path_scores = crf_decode(probv, self.transitions, lengths + 1)
        return tf.identity(viterbi[:, 1:], name="best"), path_scores 
开发者ID:dpressel,项目名称:mead-baseline,代码行数:24,代码来源:layers.py

示例2: call

# 需要导入模块: from tensorflow.contrib import crf [as 别名]
# 或者: from tensorflow.contrib.crf import crf_decode [as 别名]
def call(self, inputs, training=False, mask=None):

        unary, lengths = inputs

        if self.inv_mask is not None:
            bsz = tf.shape(unary)[0]
            lsz = self.num_tags
            np_gos = np.full((1, 1, lsz), -1e4, dtype=np.float32)
            np_gos[:, :, Offsets.GO] = 0
            gos = tf.constant(np_gos)
            start = tf.tile(gos, [bsz, 1, 1])
            probv = tf.concat([start, unary], axis=1)
            viterbi, path_scores = crf_decode(probv, self.transitions, lengths + 1)
            return tf.identity(viterbi[:, 1:], name="best"), path_scores
        else:
            return tf.argmax(unary, 2, name="best"), None 
开发者ID:dpressel,项目名称:mead-baseline,代码行数:18,代码来源:layers.py

示例3: add_blstm_crf_layer

# 需要导入模块: from tensorflow.contrib import crf [as 别名]
# 或者: from tensorflow.contrib.crf import crf_decode [as 别名]
def add_blstm_crf_layer(self, crf_only):
        """
        blstm-crf网络
        :return: 
        """
        if self.is_training:
            # lstm input dropout rate i set 0.9 will get best score
            self.embedded_chars = tf.nn.dropout(self.embedded_chars, self.dropout_rate)

        if crf_only:
            logits = self.project_crf_layer(self.embedded_chars)
        else:
            #blstm
            lstm_output = self.blstm_layer(self.embedded_chars)
            #project
            logits = self.project_bilstm_layer(lstm_output)
        #crf
        loss, trans = self.crf_layer(logits)
        # CRF decode, pred_ids 是一条最大概率的标注路径
        pred_ids, _ = crf.crf_decode(potentials=logits, transition_params=trans, sequence_length=self.lengths)
        return ((loss, logits, trans, pred_ids)) 
开发者ID:WenRichard,项目名称:KBQA-BERT,代码行数:23,代码来源:lstm_crf_layer.py

示例4: viterbi_decode

# 需要导入模块: from tensorflow.contrib import crf [as 别名]
# 或者: from tensorflow.contrib.crf import crf_decode [as 别名]
def viterbi_decode(self, potentials, sequence_length):
        """Decode the highest scoring sequence of tags in TensorFlow.

        This is a function for tensor.

        Args:
            potentials: A [batch_size, max_seq_len, num_tags] tensor, matrix of unary potentials.
            sequence_length: A [batch_size] tensor, containing sequence lengths.

        Returns:
            decode_tags: A [batch_size, max_seq_len] tensor, with dtype tf.int32.
                         Contains the highest scoring tag indicies.
        """
        decode_tags, best_score = crf_decode(potentials, self.transition_params, sequence_length)

        return decode_tags 
开发者ID:Hironsan,项目名称:keras-crf-layer,代码行数:18,代码来源:crf.py

示例5: __init__

# 需要导入模块: from tensorflow.contrib import crf [as 别名]
# 或者: from tensorflow.contrib.crf import crf_decode [as 别名]
def __init__(self,num_classes,max_docs,input_size,rnn_units=300,
                 dropout_keep=0.9,lr=0.0001,bidirectional=True):
        
        self.max_docs = max_docs
        self.dropout_keep = dropout_keep
        self.dropout = tf.placeholder(tf.float32)
        self.rnn_units = rnn_units

        self.doc_input = tf.placeholder(tf.float32, shape=[None,max_docs,input_size])
        self.num_docs = tf.placeholder(tf.int32, shape=[None])
        max_len = tf.reduce_max(self.num_docs)
        doc_input_reduced = self.doc_input[:,:max_len,:]
        doc_input_reduced = tf.nn.dropout(doc_input_reduced,self.dropout)

        self.labels = tf.placeholder(tf.int32,shape=[None,max_docs])
        labels_reduced = self.labels[:,:max_len]

        with tf.variable_scope('rnn',initializer=tf.contrib.layers.xavier_initializer()):

            if bidirectional:
                [outputs_fw,outputs_bw],_ = tf.nn.bidirectional_dynamic_rnn(
                            GRUCell(self.rnn_units/2),GRUCell(self.rnn_units/2),
                            doc_input_reduced,sequence_length=self.num_docs,dtype=tf.float32)
                outputs = tf.concat((outputs_fw,outputs_bw),2)

            else:
                outputs,_ = tf.nn.dynamic_rnn(GRUCell(self.rnn_units),
                            doc_input_reduced,sequence_length=self.num_docs,dtype=tf.float32)

        outputs = tf.nn.dropout(outputs,self.dropout)
        
        #conditional random field
        weights = tf.get_variable("weights",[outputs.shape[2],num_classes],initializer=tf.contrib.layers.xavier_initializer())
        matricized_docs = tf.reshape(outputs,[-1,outputs.shape[2]])
        matricized_unary = tf.matmul(matricized_docs,weights)
        unary_scores = tf.reshape(matricized_unary,[-1,max_len,num_classes])
        
        log_likelihood, transition_params = crf_log_likelihood(unary_scores,labels_reduced,self.num_docs)
        preds,viterbi_score = crf_decode(unary_scores,transition_params,self.num_docs)

        self.doc_idx = tf.placeholder(tf.int32, shape=[None,2])
        self.prediction = tf.gather_nd(preds,self.doc_idx)

        #loss, accuracy, and training functions
        self.loss = tf.reduce_mean(-log_likelihood)
        self.optimizer = tf.train.AdamOptimizer(lr,0.9,0.99).minimize(self.loss)

        #init op
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        self.saver = tf.train.Saver()
        self.sess = tf.Session(config=config)
        self.sess.run(tf.global_variables_initializer()) 
开发者ID:iamshang1,项目名称:Projects,代码行数:55,代码来源:tf_rnn_crf.py

示例6: crf_decode

# 需要导入模块: from tensorflow.contrib import crf [as 别名]
# 或者: from tensorflow.contrib.crf import crf_decode [as 别名]
def crf_decode(potentials, transition_params, sequence_length):
        """Decode the highest scoring sequence of tags in TensorFlow.
        This is a function for tensor.
        Args:
        potentials: A [batch_size, max_seq_len, num_tags] tensor, matrix of
                  unary potentials.
        transition_params: A [num_tags, num_tags] tensor, matrix of
                  binary potentials.
        sequence_length: A [batch_size] tensor, containing sequence lengths.
        Returns:
        decode_tags: A [batch_size, max_seq_len] tensor, with dtype tf.int32.
                    Contains the highest scoring tag indicies.
        best_score: A [batch_size] tensor, containing the score of decode_tags.
        """
        # For simplicity, in shape comments, denote:
        # 'batch_size' by 'B', 'max_seq_len' by 'T' , 'num_tags' by 'O' (output).
        num_tags = potentials.get_shape()[2].value

        # Computes forward decoding. Get last score and backpointers.
        crf_fwd_cell = CrfDecodeForwardRnnCell(transition_params)
        initial_state = array_ops.slice(potentials, [0, 0, 0], [-1, 1, -1])
        initial_state = array_ops.squeeze(initial_state, axis=[1])  # [B, O]
        inputs = array_ops.slice(potentials, [0, 1, 0], [-1, -1, -1])  # [B, T-1, O]
        backpointers, last_score = rnn.dynamic_rnn(
            crf_fwd_cell,
            inputs=inputs,
            sequence_length=sequence_length - 1,
            initial_state=initial_state,
            time_major=False,
            dtype=dtypes.int32)  # [B, T - 1, O], [B, O]
        backpointers = gen_array_ops.reverse_sequence(backpointers, sequence_length - 1, seq_dim=1)  # [B, T-1, O]

        # Computes backward decoding. Extract tag indices from backpointers.
        crf_bwd_cell = CrfDecodeBackwardRnnCell(num_tags)
        initial_state = math_ops.cast(math_ops.argmax(last_score, axis=1), dtype=dtypes.int32)  # [B]
        initial_state = array_ops.expand_dims(initial_state, axis=-1)  # [B, 1]
        decode_tags, _ = rnn.dynamic_rnn(
            crf_bwd_cell,
            inputs=backpointers,
            sequence_length=sequence_length - 1,
            initial_state=initial_state,
            time_major=False,
            dtype=dtypes.int32)  # [B, T - 1, 1]
        decode_tags = array_ops.squeeze(decode_tags, axis=[2])  # [B, T - 1]
        decode_tags = array_ops.concat([initial_state, decode_tags], axis=1)  # [B, T]
        decode_tags = gen_array_ops.reverse_sequence(decode_tags, sequence_length, seq_dim=1)  # [B, T]

        best_score = math_ops.reduce_max(last_score, axis=1)  # [B]
        return decode_tags, best_score 
开发者ID:Hironsan,项目名称:tensorflow-nlp-examples,代码行数:51,代码来源:crf.py


注:本文中的tensorflow.contrib.crf.crf_decode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。