- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Keras 提供了几个辅助函数来处理文本:
texts_to_sequences
和 texts_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/
我正在努力创建一个文本分类代码,但我在使用分词器对文档进行编码时遇到了问题。 1)我首先在我的文档上安装了一个标记器,如下所示: vocabulary_size = 20000 tokenizer =
我正在使用: from keras.preprocessing.text import Tokenizer max_words = 10000 text = 'Decreased glucose-6-
Keras 提供了几个辅助函数来处理文本: texts_to_sequences 和 texts_to_matrix 似乎大多数人都使用 texts_to_sequences,但我不清楚为什么选择一个
我已申请 np.hstack 上 tensorflow.keras.preprocessing.text.Tokenizer.texts_to_sequences 对于培训标签和验证(测试)标签。 令
Numpy 数组 tensorflow.keras.preprocessing.text.Tokenizer.texts_to_sequences 为训练标签提供奇怪的输出,如下所示: (traini
我是一名优秀的程序员,十分优秀!