Keras tokenizer.fit_on_texts 在做什么?
What is Keras tokenizer.fit_on_texts doing?
如何使用 Keras Tokenizer 方法 fit_on_texts
?
它与 fit_on_sequences
有何不同?
fit_on_texts
与 texts_to_matrix
结合使用可生成文本的单热编码,请参阅 https://www.tensorflow.org/text/guide/word_embeddings
fit_on_texts
使用示例fit_on_texts
from keras.preprocessing.text import Tokenizer
text='check check fail'
tokenizer = Tokenizer()
tokenizer.fit_on_texts([text])
tokenizer.word_index
会产生{'check': 1, 'fail': 2}
请注意,我们使用 [text]
作为参数,因为输入必须是一个列表,其中列表的每个元素都被视为一个标记。输入也可以是文本生成器或字符串列表。
将文本生成器作为输入传递是内存高效的,这里有一个示例:(1) 定义一个文本生成器返回可迭代的文本集合
def text_generator(texts_generator):
for texts in texts_generator:
for text in texts:
yield text
(2) 将其作为输入传递给 fit_on_texts
tokenizer.fit_on_text(text_generator)
fit_on_texts
在调用 texts_to_matrix
之前使用,它会为原始文本集生成单热编码。
num_words 参数
将 num_words
参数传递给分词器将指定我们在表示中考虑的(最频繁的)词的数量。举个例子,首先 num_words = 1
我们只对最常见的词进行编码,love
sentences = [
'i love my dog',
'I, love my cat',
'You love my dog!'
]
tokenizer = Tokenizer(num_words = 1+1)
tokenizer.fit_on_texts(sentences)
tokenizer.texts_to_sequences(sentences) # [[1], [1], [1]]
其次,num_words = 100
,我们对 100 个最常见的词进行编码
tokenizer = Tokenizer(num_words = 100+1)
tokenizer.fit_on_texts(sentences)
tokenizer.texts_to_sequences(sentences) # [[3, 1, 2, 4], [3, 1, 2, 5], [6, 1, 2, 4]]
fit_on_sequences
Fit_on_sequences
适用于“序列”,即整数词索引列表。在调用 sequence_to_matrix
之前使用
from tensorflow.keras.preprocessing.text import Tokenizer
test_seq = [[1,2,3,4,5,6]]
tok = Tokenizer(num_words=10)
tok.fit_on_sequences(test_seq)
tok.sequences_to_matrix(test_seq)
制作中
array([[0., 1., 1., 1., 1., 1., 1., 0., 0., 0.]])
如何使用 Keras Tokenizer 方法 fit_on_texts
?
它与 fit_on_sequences
有何不同?
fit_on_texts
与 texts_to_matrix
结合使用可生成文本的单热编码,请参阅 https://www.tensorflow.org/text/guide/word_embeddings
fit_on_texts
使用示例fit_on_texts
from keras.preprocessing.text import Tokenizer
text='check check fail'
tokenizer = Tokenizer()
tokenizer.fit_on_texts([text])
tokenizer.word_index
会产生{'check': 1, 'fail': 2}
请注意,我们使用 [text]
作为参数,因为输入必须是一个列表,其中列表的每个元素都被视为一个标记。输入也可以是文本生成器或字符串列表。
将文本生成器作为输入传递是内存高效的,这里有一个示例:(1) 定义一个文本生成器返回可迭代的文本集合
def text_generator(texts_generator):
for texts in texts_generator:
for text in texts:
yield text
(2) 将其作为输入传递给 fit_on_texts
tokenizer.fit_on_text(text_generator)
fit_on_texts
在调用 texts_to_matrix
之前使用,它会为原始文本集生成单热编码。
num_words 参数
将 num_words
参数传递给分词器将指定我们在表示中考虑的(最频繁的)词的数量。举个例子,首先 num_words = 1
我们只对最常见的词进行编码,love
sentences = [
'i love my dog',
'I, love my cat',
'You love my dog!'
]
tokenizer = Tokenizer(num_words = 1+1)
tokenizer.fit_on_texts(sentences)
tokenizer.texts_to_sequences(sentences) # [[1], [1], [1]]
其次,num_words = 100
,我们对 100 个最常见的词进行编码
tokenizer = Tokenizer(num_words = 100+1)
tokenizer.fit_on_texts(sentences)
tokenizer.texts_to_sequences(sentences) # [[3, 1, 2, 4], [3, 1, 2, 5], [6, 1, 2, 4]]
fit_on_sequences
Fit_on_sequences
适用于“序列”,即整数词索引列表。在调用 sequence_to_matrix
from tensorflow.keras.preprocessing.text import Tokenizer
test_seq = [[1,2,3,4,5,6]]
tok = Tokenizer(num_words=10)
tok.fit_on_sequences(test_seq)
tok.sequences_to_matrix(test_seq)
制作中
array([[0., 1., 1., 1., 1., 1., 1., 0., 0., 0.]])