char2num={v:k for k,v in enumerate(chars)}
num2char={k:v for k,v in enumerate(chars)}
#输入'abc','de'对应在chars中的索引‘012’,‘34’
inputs=['hello','tom']
inputs=[[char2num[item1] for item1 in item0] for item0 in inputs]
inputs
#[[7, 4, 11, 11, 14], [19, 14, 12]]
defdecode_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 item in decoded_indexes:
result.append([sparse_tensor[1][index] for index in item])
return result
decoded_indexes=decode_sparse_tensor(sparse_tensor)
decoded_indexes
#[[7, 4, 11, 11, 14], [19, 14, 12]]
outputs=[[num2char[index] for index in item] for item in decoded_indexes]
outputs
#[['h', 'e', 'l', 'l', 'o'], ['t', 'o', 'm']]
import numpy as npimport stringchars=string.ascii_lowercasechars'abcdefghijklmnopqrstuvwxyz'char2num={v:k for k,v in enumerate(chars)}num2char={k:v for k,v in enumerate(chars)}#输入'abc','de'对应在chars