Tensorflow的一些常用函数说明
一、tf.nn.ctc_beam_search_decoder
tf.nn.ctc_beam_search_decoder(
inputs,
sequence_length,
beam_width=100,
top_paths=1,
merge_repeated=True
)
定义在:tensorflow/python/ops/ctc_ops.py.
对输入中给出的logits执行波束(beam)搜索解码.
注意:这ctc_greedy_decoder是带有top_paths=1和beam_width=1的ctc_beam_search_decoder的特殊情况(但解码器在这种特殊情况下更快).
如果merge_repeated是True,则合并输出波束中的重复类.这意味着如果波束中的连续条目相同,则仅发出第一个条目.也就是说,当顶部路径为时A B B B B,返回值为:
A B:如果merge_repeated = True.
A B B B B:如果merge_repeated = False.
参数:
inputs:3-D float类型的 Tensor,大小为[max_time x batch_size x num_classes],是logits.
sequence_length:1-D int32向量,包含序列长度,具有大小[batch_size].
beam_width:int标量大于等于0(波束搜索波束宽度).
top_paths:int标量大于等于0,小于等于beam_width(控制输出大小).
merge_repeated:Boolean,默认值:True.
返回:
元组(decoded, log_probabilities),其中:top_paths长度的列表,其中decoded[j]是SparseTensor,它包含已解码的输出:
decoded[j].indices: Indices matrix (total_decoded_outputs[j] x 2),行存储:[batch, time].
decoded[j].values: Values vector, size (total_decoded_outputs[j]),向量存储波束 j 的解码类.
decoded[j].dense_shape: Shape vector, size (2),形状值为[batch_size, max_decoded_length[j]]
log_probability:一个浮点矩阵(batch_size x top_paths),包含序列对数概率.
举例:输出 decoded[0]会得到类似下面的这个结果,有indices和values,就类似一个稀疏矩阵,需要再解析得出我们需要的结果
SparseTensorValue(indices=array([[0, 0],
[0, 1],
[0, 2],
[0, 3],
[0, 4]]), values=array([17, 33, 30, 26, 30]), dense_shape=array([1, 5]))
decoded[0].indices #如下
[[0 0]
[0 1]
[0 2]
[0 3]
[0 4]]
decoded[0].values #如下
[17 33 30 26 30]
解析函数如下:
DIGITS ="0123456789"
LETTERS ="ABCDEFGHJKLMNPQRSTUVWXYZ"
CHARS =list(DIGITS + LETTERS)
def decode_sparse_tensor(sparse_tensor):
decoded_indexes = list()
current_i = 0
current_seq = []
for offset, i_and_index in enumerate(sparse_tensor[0]):
i = i_and_index[0]
if i != current_i:
decoded_indexes.append(current_seq)
current_i = i
current_seq = list()
current_seq.append(offset)
decoded_indexes.append(current_seq)
result = []
for index in decoded_indexes:
result.append(decode_a_seq(index, sparse_tensor))
return result
def decode_a_seq(indexes, spars_tensor):
str_decoded = []
for m in indexes:
str_decoded.append(spars_tensor[1][m])
return str_decoded
友情链接
TensorFlow函数教程:tf.nn.ctc_beam_search_decoder
tensorflow LSTM+CTC实现端到端的不定长数字串识别