Keras Tokenizer num_words 似乎不起作用
Keras Tokenizer num_words doesn't seem to work
>>> t = Tokenizer(num_words=3)
>>> l = ["Hello, World! This is so&#$ fantastic!", "There is no other world like this one"]
>>> t.fit_on_texts(l)
>>> t.word_index
{'fantastic': 6, 'like': 10, 'no': 8, 'this': 2, 'is': 3, 'there': 7, 'one': 11, 'other': 9, 'so': 5, 'world': 1, 'hello': 4}
我原以为 t.word_index
只有前 3 个词。我做错了什么?
你所做的没有错。 word_index
的计算方式相同,无论您以后使用多少个最常用的词(如您可能会看到的 here)。因此,当您调用任何转换方法时 - Tokenizer
将仅使用三个最常用的词,同时,它会保留所有词的计数器 - 即使很明显它以后不会使用它。
只需添加 Marcin 的回答 ("it will keep the counter of all words - even when it's obvious that it will not use it later.")。
它对所有单词保持反驳的原因是您可以多次调用 fit_on_texts
。每次它都会更新内部计数器,并且在调用转换时,它会根据更新的计数器使用排名靠前的词。
希望对您有所帮助。
将 num_words 限制为较小的数字(例如 3)对 fit_on_texts 输出没有影响 例如word_index、word_counts、word_docs。它确实对 texts_to_matrix 有影响。生成的矩阵将有 num_words (3) 列。
>>> t = Tokenizer(num_words=3)
>>> l = ["Hello, World! This is so&#$ fantastic!", "There is no other world like this one"]
>>> t.fit_on_texts(l)
>>> print(t.word_index)
{'world': 1, 'this': 2, 'is': 3, 'hello': 4, 'so': 5, 'fantastic': 6, 'there': 7, 'no': 8, 'other': 9, 'like': 10, 'one': 11}
>>> t.texts_to_matrix(l, mode='count')
array([[0., 1., 1.],
[0., 1., 1.]])
只是为了补充一点 farid khafizov 的回答,
num_words及以上序列的单词从texts_to_sequences的结果中删除(第1句中的4个,第2句中的5个和第3句中的6个分别消失)
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
print(tf.__version__) # 2.4.1, in my case
sentences = [
'I love my dog',
'I, love my cat',
'You love my dog!'
]
tokenizer = Tokenizer(num_words=4)
tokenizer.fit_on_texts(sentences)
word_index = tokenizer.word_index
seq = tokenizer.texts_to_sequences(sentences)
print(word_index) # {'love': 1, 'my': 2, 'i': 3, 'dog': 4, 'cat': 5, 'you': 6}
print(seq) # [[3, 1, 2], [3, 1, 2], [1, 2]]
>>> t = Tokenizer(num_words=3)
>>> l = ["Hello, World! This is so&#$ fantastic!", "There is no other world like this one"]
>>> t.fit_on_texts(l)
>>> t.word_index
{'fantastic': 6, 'like': 10, 'no': 8, 'this': 2, 'is': 3, 'there': 7, 'one': 11, 'other': 9, 'so': 5, 'world': 1, 'hello': 4}
我原以为 t.word_index
只有前 3 个词。我做错了什么?
你所做的没有错。 word_index
的计算方式相同,无论您以后使用多少个最常用的词(如您可能会看到的 here)。因此,当您调用任何转换方法时 - Tokenizer
将仅使用三个最常用的词,同时,它会保留所有词的计数器 - 即使很明显它以后不会使用它。
只需添加 Marcin 的回答 ("it will keep the counter of all words - even when it's obvious that it will not use it later.")。
它对所有单词保持反驳的原因是您可以多次调用 fit_on_texts
。每次它都会更新内部计数器,并且在调用转换时,它会根据更新的计数器使用排名靠前的词。
希望对您有所帮助。
将 num_words 限制为较小的数字(例如 3)对 fit_on_texts 输出没有影响 例如word_index、word_counts、word_docs。它确实对 texts_to_matrix 有影响。生成的矩阵将有 num_words (3) 列。
>>> t = Tokenizer(num_words=3)
>>> l = ["Hello, World! This is so&#$ fantastic!", "There is no other world like this one"]
>>> t.fit_on_texts(l)
>>> print(t.word_index)
{'world': 1, 'this': 2, 'is': 3, 'hello': 4, 'so': 5, 'fantastic': 6, 'there': 7, 'no': 8, 'other': 9, 'like': 10, 'one': 11}
>>> t.texts_to_matrix(l, mode='count')
array([[0., 1., 1.],
[0., 1., 1.]])
只是为了补充一点 farid khafizov 的回答, num_words及以上序列的单词从texts_to_sequences的结果中删除(第1句中的4个,第2句中的5个和第3句中的6个分别消失)
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
print(tf.__version__) # 2.4.1, in my case
sentences = [
'I love my dog',
'I, love my cat',
'You love my dog!'
]
tokenizer = Tokenizer(num_words=4)
tokenizer.fit_on_texts(sentences)
word_index = tokenizer.word_index
seq = tokenizer.texts_to_sequences(sentences)
print(word_index) # {'love': 1, 'my': 2, 'i': 3, 'dog': 4, 'cat': 5, 'you': 6}
print(seq) # [[3, 1, 2], [3, 1, 2], [1, 2]]