本文整理汇总了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
示例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
示例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
示例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
示例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
示例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
示例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
示例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
示例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
示例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
示例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
示例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
示例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
示例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