Keras Tokenizer方法究竟做了什么?

Jac*_*ing 27 python nlp keras

有时,情况要求我们执行以下操作:

from keras.preprocessing.text import Tokenizer
tokenizer = Tokenizer(num_words=my_max)
Run Code Online (Sandbox Code Playgroud)

然后,我们总是念诵这句口头禅:

tokenizer.fit_on_texts(text) 
sequences = tokenizer.texts_to_sequences(text)
Run Code Online (Sandbox Code Playgroud)

虽然我(或多或少)了解总效应是什么,但无论我做了多少研究(显然包括文档),我都无法弄清楚每个人分别做了什么.我不认为我没见过另一个.

那么每个人做什么?在任何情况下你都会使用其中任何一个吗?如果没有,为什么不将它们简单地组合成如下:

sequences = tokenizer.fit_on_texts_to_sequences(text)
Run Code Online (Sandbox Code Playgroud)

抱歉,如果我遗漏了一些明显的东西,但我对此很陌生.

nur*_*ric 50

源代码:

  1. fit_on_texts 根据文本列表更新内部词汇表.此方法基于词频创建词汇索引.所以,如果你给它类似的东西,"猫坐在垫子上." 它将创建一个字典,word_index["the"] = 1; word_index["cat"] = 2它是word - > index dictionary,因此每个单词都会获得一个唯一的整数值.所以较低的整数意味着更频繁的单词(通常前几个是标点符号,因为它们看起来很多).
  2. texts_to_sequences 将文本中的每个文本转换为整数序列.所以它基本上取文本中的每个单词并用word_index字典中相应的整数值替换它.没有更多,没有更少,当然没有魔法涉及.

为什么不把它们结合起来?因为你几乎总是适合一次多次转换为序列.您将适应您的训练语料库并word_index在训练/评估/测试/预测时使用完全相同的字典将实际文本转换为序列以将它们提供给网络.因此将这些方法分开是有意义的.

  • 所以这一切都在源代码中!我想我看起来不够努力......现在我明白了:适合 - 一次,序列 - 多次!像往常一样生活和学习。谢谢。 (3认同)
  • @nuric 感谢您的回复。但是,有一些注意事项。根据官方文档,“`0` 是不会分配给任何单词的保留索引。” 因此,在您的示例中,第一个单词索引将是 1 https://keras.io/preprocessing/text/ 另一点是默认过滤器会删除大部分标点符号,因此只有当您从筛选。 (2认同)

Jos*_*Jos 17

通过示例在上述答案中添加更多内容,有助于更好地理解:

范例1

t  = Tokenizer()
fit_text = "The earth is an awesome place live"
t.fit_on_texts(fit_text)
test_text = "The earth is an great place live"
sequences = t.texts_to_sequences(test_text)

print("sequences : ",sequences,'\n')

print("word_index : ",t.word_index)
#[] specifies : 1. space b/w the words in the test_text    2. letters that have not occured in fit_text

Output :

       sequences :  [[3], [4], [1], [], [1], [2], [8], [3], [4], [], [5], [6], [], [2], [9], [], [], [8], [1], [2], [3], [], [13], [7], [2], [14], [1], [], [7], [5], [15], [1]] 

       word_index :  {'e': 1, 'a': 2, 't': 3, 'h': 4, 'i': 5, 's': 6, 'l': 7, 'r': 8, 'n': 9, 'w': 10, 'o': 11, 'm': 12, 'p': 13, 'c': 14, 'v': 15}
Run Code Online (Sandbox Code Playgroud)

范例2

t  = Tokenizer()
fit_text = ["The earth is an awesome place live"]
t.fit_on_texts(fit_text)

#fit_on_texts fits on sentences when list of sentences is passed to fit_on_texts() function. 
#ie - fit_on_texts( [ sent1, sent2, sent3,....sentN ] )

#Similarly, list of sentences/single sentence in a list must be passed into texts_to_sequences.
test_text1 = "The earth is an great place live"
test_text2 = "The is my program"
sequences = t.texts_to_sequences([test_text1, test_text2])

print('sequences : ',sequences,'\n')

print('word_index : ',t.word_index)
#texts_to_sequences() returns list of list. ie - [ [] ]

Output:

        sequences :  [[1, 2, 3, 4, 6, 7], [1, 3]] 

        word_index :  {'the': 1, 'earth': 2, 'is': 3, 'an': 4, 'awesome': 5, 'place': 6, 'live': 7}
Run Code Online (Sandbox Code Playgroud)

  • 例子永远不会说谎!感谢您的精彩回答。 (4认同)

小智 5

让我们看看这行代码的作用。

tokenizer.fit_on_texts(文本)

例如,考虑句子“地球是一个很棒的生活场所”

tokenizer.fit_on_texts("The earth is an awesome place live") 适合[[1,2,3,4,5,6,7]]其中3->“ is”,6->“ place”,依此类推。

sequences = tokenizer.texts_to_sequences("The earth is an great place live")
Run Code Online (Sandbox Code Playgroud)

返回[[1,2,3,4,6,7]]。

你看这里发生了什么。单词“ great”最初不适合使用,因此无法识别单词“ great”。意思是,可以在火车数据上单独使用fit_on_text,然后可以使用拟合的词汇索引来表示一组全新的单词序列。这是两个不同的过程。因此,这两行代码。