gpt4 book ai didi

keras 和 nlp - 何时使用 .texts_to_matrix 而不是 .texts_to_sequences?

转载 作者:行者123 更新时间:2023-12-04 15:25:09 25 4
gpt4 key购买 nike

Keras 提供了几个辅助函数来处理文本:

texts_to_sequencestexts_to_matrix

似乎大多数人都使用 texts_to_sequences,但我不清楚为什么选择一个而不是另一个以及在什么条件下您可能想要使用 texts_to_matrix

最佳答案

texts_to_matrix 很容易理解。它将文本转换为矩阵,其中列引用单词和单元格,包含出现次数或存在次数。这样的设计将有助于机器学习算法(逻辑回归、决策树等)的直接应用

texts_to_sequence 将创建列表,这些列表是表示单词的整数集合。某些函数(如 Keras 嵌入)需要这种格式进行预处理。

考虑下面的例子。

txt = ['Python is great and useful', 'Python is easy to learn', 'Python is easy to implement']
txt = pd.Series(txt)

tok = Tokenizer(num_words=10)
tok.fit_on_texts(txt)
mat_texts = tok.texts_to_matrix(txt, mode='count')
mat_texts

输出:数组([[0., 1., 1., 0., 0., 1., 1., 1., 0., 0.],[0., 1., 1., 1., 1., 0., 0., 0., 1., 0.],[0., 1., 1., 1., 1., 0., 0., 0., 0., 1.]])

tok.get_config()['word_index']

输出:'{"python": 1, "is": 2, "easy": 3, "to": 4, "great": 5, "and": 6, "useful": 7, "learn": 8, “实现”:9}'

mat_texts_seq = tok.texts_to_sequences(txt)
mat_texts_seq

输出:-[[1, 2, 5, 6, 7], [1, 2, 3, 4, 8], [1, 2, 3, 4, 9]]

关于keras 和 nlp - 何时使用 .texts_to_matrix 而不是 .texts_to_sequences?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62419638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com