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

问题描述 投票:0回答:1

在进行tensorflow转换的文档时,我遇到了执行TD-IDF的函数。

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

由于文档尚不清楚,我提供了如何使用example_string尝试执行TD-IDF的示例

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

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

tft.tfidf(example_strings, vocab_size=1000)

AttributeError:'list'对象没有属性'indices'

由于我不熟悉Tensorflow转换操作,请帮助我解决这个问题。

tensorflow2.0 tf-idf tensorflow-transform
1个回答
0
投票

如果您想用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')
© www.soinside.com 2019 - 2024. All rights reserved.