首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tensorflow令牌器还原fit_on_sequences

Tensorflow令牌器还原fit_on_sequences
EN

Stack Overflow用户
提问于 2021-09-29 07:20:05
回答 1查看 75关注 0票数 1

Tensorflow标记器将文本标记化并编码为机器可读的向量。首先,我们对一些大量的文本调用fit_on_texts来构建一个字典,然后我们对我们的输入文本调用fit_on_sequences来构建相应的编码向量。

What does Keras Tokenizer method exactly do?

然而,似乎没有一种内置的方法来进行反向操作,即根据字典从数值向量中检索文本。

在Python中,可以实现如下内容

代码语言:javascript
复制
 # 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反向操作

EN

回答 1

Stack Overflow用户

发布于 2021-11-16 17:41:22

有一种内置方法可用于从数值向量中检索文本。

例如,检查下面的代码:

代码语言:javascript
复制
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)

输出:

代码语言:javascript
复制
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内置函数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69372004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档