Keras Tokenizer num_words似乎没有工作

问题描述:

>>> 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个单词。我究竟做错了什么?Keras Tokenizer num_words似乎没有工作

+0

另请参见此问题:https://github.com/fchollet/keras/issues/7836 – petezurich

+1

谢谢!以下内容也有助于https://github.com/fchollet/keras/issues/7551 –

你在做什么没有错。 word_index以相同的方式计算,无论您将在以后使用多少个最频繁的单词(如您可能会看到here)。因此,当你打电话给任何转化方法 - Tokenizer将只使用三个最常见的单词,同时,它将保留所有单词的计数器 - 即使明显它不会在以后使用它。