【504】NLP实战系列(一)—— one-hot encoding

参考:Text Preprocessing —— Tokenizer

参考:Preprocessing » 文本预处理

  对于 Embedding 层使用的输入,就是整数矩阵,并不是真正的 one-hot 向量,需要利用 Tokenizer 来实现。

1. Tokenizer 

1.1 语法

keras.preprocessing.text.Tokenizer(num_words=None, 
                                   filters='!"#$%&()*+,-./:;<=>?@[]^_`{|}~ ', 
                                   lower=True, 
                                   split=' ', 
                                   char_level=False, 
                                   oov_token=None, 
                                   document_count=0)

  文本标记实用类。该类允许使用两种方法向量化一个文本语料库: 将每个文本转化为一个整数序列(每个整数都是词典中标记的索引); 或者将其转化为一个向量,其中每个标记的系数可以是二进制值、词频、TF-IDF权重等。

1.2 参数说明

  • num_words: 需要保留的最大词数,基于词频。只有最常出现的 num_words 词会被保留。
  • filters: 一个字符串,其中每个元素是一个将从文本中过滤掉的字符。默认值是所有标点符号,加上制表符和换行符,减去 ' 字符。
  • lower: 布尔值。是否将文本转换为小写。
  • split: 字符串。按该字符串切割文本。
  • char_level: 如果为 True,则每个字符都将被视为标记。
  • oov_token: 如果给出,它将被添加到 word_index 中,并用于在 text_to_sequence 调用期间替换词汇表外的单词。

  默认情况下,删除所有标点符号,将文本转换为空格分隔的单词序列(单词可能包含 ' 字符)。 这些序列然后被分割成标记列表。然后它们将被索引或向量化。

  0 是不会被分配给任何单词的保留索引。

1.3 类方法

  • fit_on_texts(texts)

    • texts:要用以训练的文本列表
  • texts_to_sequences(texts)

    • texts:待转为序列的文本列表

    • 返回值:序列的列表,列表中每个序列对应于一段输入文本

    • [[1, 2, 3, 4, 1, 5], [1, 6, 7, 8, 9]]
  • texts_to_sequences_generator(texts)

    • 本函数是texts_to_sequences的生成器函数版

    • texts:待转为序列的文本列表

    • 返回值:每次调用返回对应于一段输入文本的序列

  • texts_to_matrix(texts, mode):

    • texts:待向量化的文本列表

    • mode:‘binary’,‘count’,‘tfidf’,‘freq’之一,默认为‘binary’

    • 返回值:形如(len(texts), nb_words)的numpy array

    • [[0. 1. 1. ... 0. 0. 0.]
       [0. 1. 0. ... 0. 0. 0.]]
  • fit_on_sequences(sequences):

    • sequences:要用以训练的序列列表
  • sequences_to_matrix(sequences):

    • sequences:待向量化的序列列表

    • mode:‘binary’,‘count’,‘tfidf’,‘freq’之一,默认为‘binary’

    • 返回值:形如(len(sequences), nb_words)的numpy array

1.4 属性

  • word_counts:字典,将单词(字符串)映射为它们在训练期间出现的次数。仅在调用fit_on_texts之后设置。
    • OrderedDict([('the', 3), ('cat', 1), ('sat', 1), ('on', 1), ('mat', 1), ('dog', 1), ('ate', 1), ('my', 1), ('homework', 1)])
    • len(word_counts) 来计算单词的个数
  • word_docs: 字典,将单词(字符串)映射为它们在训练期间所出现的文档或文本的数量。仅在调用fit_on_texts之后设置。
  • word_index: 字典,将单词(字符串)映射为它们的排名或者索引。仅在调用fit_on_texts之后设置。
  • document_count: 整数。分词器被训练的文档(文本或者序列)数量。仅在调用fit_on_texts或fit_on_sequences之后设置。

1.5 举例

from keras.preprocessing.text import Tokenizer

samples = ['The cat sat on the mat.', 'The dog ate my homework.']

# We create a tokenizer, configured to only take
# into account the top-1000 most common words
tokenizer = Tokenizer(num_words=1000)
# This builds the word index
tokenizer.fit_on_texts(samples)

print("word_counts: 
", tokenizer.word_counts)
print("
total words: 
", len(tokenizer.word_counts))

# This turns strings into lists of integer indices.
sequences = tokenizer.texts_to_sequences(samples)

print("
sequences:
", sequences)

# You could also directly get the one-hot binary representations.
# Note that other vectorization modes than one-hot encoding are supported!
one_hot_results = tokenizer.texts_to_matrix(samples, mode='binary')

print("
one_hot_results:
", one_hot_results)

# This is how you can recover the word index that was computed
word_index = tokenizer.word_index

print("
word_index:
", word_index)
print('
Found %s unique tokens.' % len(word_index))

  outputs:

word_counts: 
 OrderedDict([('the', 3), ('cat', 1), ('sat', 1), ('on', 1), ('mat', 1), ('dog', 1), ('ate', 1), ('my', 1), ('homework', 1)])

total words: 
 9

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

one_hot_results:
 [[0. 1. 1. ... 0. 0. 0.]
 [0. 1. 0. ... 0. 0. 0.]]

word_index:
 {'the': 1, 'cat': 2, 'sat': 3, 'on': 4, 'mat': 5, 'dog': 6, 'ate': 7, 'my': 8, 'homework': 9}

Found 9 unique tokens.
原文地址:https://www.cnblogs.com/alex-bn-lee/p/14193254.html