Tensorflow标记器将文本标记化并编码为机器可读的向量。首先,我们对一些大量的文本调用fit_on_texts来构建一个字典,然后我们对我们的输入文本调用fit_on_sequences来构建相应的编码向量。
What does Keras Tokenizer method exactly do?
然而,似乎没有一种内置的方法来进行反向操作,即根据字典从数值向量中检索文本。
在Python中,可以实现如下内容
# map predicted word index to word
out_word=''
for word, index in tokenizer.word_index.items():
if index==yhat:
out_word=word
break有没有一种很好的方法来从数字中检索文本,换句话说,有没有内置的fit_to_sequences反向操作
发布于 2021-11-16 17:41:22
有一种内置方法可用于从数值向量中检索文本。
例如,检查下面的代码:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.preprocessing.text import Tokenizer
Sentences=["Life is Beautiful"]
tokenizer= Tokenizer(num_words= 30)
tokenizer.fit_on_texts(Sentences)
word_index=tokenizer.word_index
print("Word Index: ", word_index)
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
print("Reversed Word Index:", reverse_word_index)
seq = tokenizer.texts_to_sequences(Sentences)
print("Texts to Numbers:",seq)
seq_to_wrd=tokenizer.sequences_to_texts(seq)
print("Numbers to Texts:",seq_to_wrd)输出:
Word Index: {'life': 1, 'is': 2, 'beautiful': 3}
Reversed Word Index: {1: 'life', 2: 'is', 3: 'beautiful'}
Texts to Numbers: [[1, 2, 3]]
Numbers to Texts: ['life is beautiful']检查此link以查找更多Tokenizer内置函数。
https://stackoverflow.com/questions/69372004
复制相似问题