tensorflow - 如何在Tensorflow Transform中计算TF-IDF(使用tft.tfidf函数)

标签 tensorflow machine-learning nlp tensorflow2.0 tf-idf

在浏览 tensorflow 变换中的文档时,我遇到了执行 TD-IDF 的函数。

tft.tfidf(
    x, vocab_size, smooth=True, name=None
)

由于文档没有明确提供如何执行 TD-IDF 的示例,我尝试使用 example_string

example_strings=[["I", "like", "pie", "pie", "pie"], ["yum", "yum", "pie"]]

词汇大小为 1000。(只是随机数)但下面的代码给了我一个属性错误。

tft.tfidf(example_strings, vocab_size=1000)

属性错误:“列表”对象没有属性“索引”

请帮助我解决这个问题,因为我对 Tensorflow 变换操作很幼稚。

最佳答案

如果您想使用 TFT (here an example) 计算 tfidf,您可以这样做

example_strings = ["I like pie pie pie", "yum yum pie"]
VOCAB_SIZE = 100

tf.compat.v1.disable_eager_execution()

tokens = tf.compat.v1.string_split(example_strings)
indices = tft.compute_and_apply_vocabulary(tokens, top_k=VOCAB_SIZE)
bow_indices, weight = tft.tfidf(indices, VOCAB_SIZE + 1)

另外,您也可以使用TF Tokenizer :

tk = tf.keras.preprocessing.text.Tokenizer(num_words=VOCAB_SIZE)
tk.fit_on_texts(example_strings)

tk.sequences_to_matrix(tk.texts_to_sequences(example_strings), mode='tfidf')

关于tensorflow - 如何在Tensorflow Transform中计算TF-IDF(使用tft.tfidf函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61907589/

相关文章:

python - 在 keras fit_generator() 中未调用 on_epoch_end()

python - Keras VGG16 相同模型不同方法给出不同结果

tensorflow - KERAS:如何使用与权重所需形状相同的张量显式设置 Conv2D 层的权重?

machine-learning - 用于图像识别、图像大小的 TensorFlow

image-processing - AdaBoost算法的训练集

python - 直接从打包的 tar.gz 文件加载 spacy 模型

python - 来自函数的 Tensorflow 占位符

javascript - 泛化 XPath

python-3.x - Sklearn - NLTK 问题预测

perl - 从 Perl 文本 block 中提取一个人的全名?