深度学习模型是不是在原始文本上做任何处理的,为了使用深度学习模型处理文本数据,文本数据必须编码为数字,这样才可以顺利的使用机器学习和深度学习模型。Keras深度学习库提供了一些基本工具来帮助您预处理文本数据。本章中,您将学习如何使用Keras准备文本数据。完成本教程后,您将了解:

  • 关于可用于快速准备文本数据的便捷方法。
  • Tokenizer API,适用于训练数据,用于编码训练,验证和测试文档。
  • Tokenizer API提供的4种不同文档编码方案的范围。

4.1 教程概述

本教程分为以下几部分:

  1. text_to_word_sequence()分词
  2. 如何转变成one_hot编码。
  3. 如何是用hash_trick 进行哈希编码。
  4. 使用分词APITokenizer API

 

4.2 使用text_to_word_sequence 分词

使用文本第一步是将其拆分为单词。单词称为标记token,将文本拆分为标记的过程称为标记化tokenizationKeras提供了函数text_to_word_sequence(),您可以使用该函数将文本拆分为单词列表。默认情况下,此函数自动执行以下三项操作:

  • 空格拆分单词。
  • 过滤掉标点符号。
  • 将文本转换为小写(lower = True)。

您可以通过将参数传递给函数来更改这些默认值下面是使用text_to_word_sequence()函数将文档(在本例中为简单字符串)拆分为单词列表的示例。

from keras.preprocessing.text import text_to_word_sequence
# define the document
text = 'The quick brown fox jumped over the lazy dog.'
# tokenize the document
result = text_to_word_sequence(text)
print(result)

代码清单4.1:使用Tokenizer拆分单词的示例

运行该示例将创建一个包含文档中所有单词的数组。打印单词列表。

['the', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']

代码清单4.2:使用Tokenizer拆分单词的示例输出

文本预处理的第一步,在使用文本之前需要进一步的预处理。

 

4.3 使用one_hot

将文档表示为整数值序列是很流行的,其中文档中的每个单词都表示为唯一的整数。Keras提供了one_hot()函数,使用它可以对文本文档进行标记化和整数编码。该名称表明它将文档转换成one_hot形式的编码,但事实并非如此相反,one_hot()数是下一节中描述的hash_trick()函数的包装器返回文档的整数编码版本。散列函数的使用意味着可能存在冲突,并且不是所有单词都将被分配唯一的整数值。与上一节中的text_to_word_sequence()函数一样,one_hot()函数将使文本小写,过滤掉标点符号,并根据空格分割单词。

除文本外,还必须指定词汇量(总词数)。这个数字应该是你编码文档的词汇数,当然如果你希望未来能更好的处理其他文档,这个词汇量会要求更大。词汇表的大小定义了散列单词的散列空间。默认情况下,使用散列函数,尽管我们将在下一节中看到,可以在直接调用hashing_trick()数时指定备用散列函数。

我们用上一节介绍的text_to_word_sequence()函数将文档拆分为单词,然后使用集合操作获取文档的所有独立词。该集的大小可用于估计一个文档的词汇量的大小。例如:

from keras.preprocessing.text import text_to_word_sequence
# define the document
text = 'The quick brown fox jumped over the lazy dog.'
# estimate the size of the vocabulary
words = set(text_to_word_sequence(text))
vocab_size = len(words)
print(vocab_size)

代码清单4.3:准备词汇表的示例

我们可以将它与one_hot()数放在一起使用,并对文档中的单词进行编码。下面列出了完整的示例。词汇量增加三分之一,以最大限度地减少散列词时的冲突。

from keras.preprocessing.text import one_hot
from keras.preprocessing.text import text_to_word_sequence
# define the document
text = 'The quick brown fox jumped over the lazy dog.'
# estimate the size of the vocabulary
words = set(text_to_word_sequence(text))
vocab_size = len(words)
print(vocab_size)
# integer encode the document
result = one_hot(text, round(vocab_size*1.3))
print(result)

代码清单4.4one_hot()编码的示例

首先运行该示例将词汇表的大小打印为8.然后将编码的文档打印为整数编码的单词数组。

意:鉴于神经网络的随机性,您的具体结果可能会有所不同。考虑运行几次示例。

8

[2, 5, 6, 9, 4, 9, 2, 6, 8]

代码清单4.5:使用Tokenizer进行one_hot编码的示例

4.4 使用hashing_trick()进行哈希编码

整数和计数基本编码的限制是它们必须保持单词的词汇表及其到整数的映射。此方法的替代方法是使用单向散列函数将单词转换为整数。这避免了跟踪词汇表的需要,词汇表更快并且需要更少的内存。

Keras提供hashing_trick()数,该函数对文档token后进行编码,就像one_hot()函数一样。灵活,允许您指定你希望的散列(默认)或其他散列函数,例如内置的md5()函数或您自己的定义的函数。下面是使用md5散列函数对文档进行整数编码的示例。

from keras.preprocessing.text import hashing_trick
from keras.preprocessing.text import text_to_word_sequence
# define the document
text = 'The quick brown fox jumped over the lazy dog.'
# estimate the size of the vocabulary
words = set(text_to_word_sequence(text))
vocab_size = len(words)
print(vocab_size)
# integer encode the document
result = hashing_trick(text, round(vocab_size*1.3), hash_function='md5')
print(result)

代码清单4.6:哈希编码的例子

运行该示例将打印词汇表的大小和文档整数编码。我们可以看到,使用不同的散列函数会导致单词整数编码不一样

8

[6, 4, 1, 2, 7, 5, 6, 2, 6]

代码清单4.7:使用Tokenizer进行哈希编码的示例输出

4.5. Tokenizer API

到目前为止,我们已了解了使用Keras准备文本的快捷方法。但这个不是Keras文本预处理的全部,Keras提供了很多大量的效率更好的文本处理API,用于单个或多个文本文档的文的预处理,许多成功的商业项目就是大量使用KerasAPI进行自己的文本预处理Keras提供了Tokenizer类,用于为深度学习文本文档的预处理首先引入并实例化Tokenizer接着就可以使用这个Tokenizer对文本处理或者对文本进行编码。例如:

from keras.preprocessing.text import Tokenizer
# define 5 documents
docs = ['Well done!',
'Good work',
'Great effort',
'nice work',
'Excellent!']
# create the tokenizer
t = Tokenizer()
# fit the tokenizer on the documents
t.fit_on_texts(docs)

代码清单4.8:适配Tokenizer的示例

t.fit_on_texts()Tokenizer提供了4个属性,您可以使用这些属性查询有关文档的内容:

  • word_counts字典,保存每个词在所有文档中出现的次数
  • word docs字典,保存每个词出现文档数量。
  • word_index字典,保存所有Word对应的ID编号,从1开始
  • document_count:处理的文档数。

例如:

# summarize what was learned

print (t.word_counts)

print (t.document_count)

print (t.word_index)

print (t.word_docs)

代码清单4.9:汇总fit Tokenizer的输出。

一旦Tokenizer练数据集上fit之后,它就可用于编码训练或测试数据集中的文档Tokenizer上的text_to_matrix()数用于为每个输入文档的向量化,文档向量的长度是词汇表的总大小。此函数提供了一套标准的词袋模型文本编码方案,可以通过函数的mode设定其编码方案。可用的模式包括:

  • binary文档中是否有该词。这是默认值。
  • count:文档中每个单词的计数。
  • tfidf:文档中每个单词的评分TF-IDF
  • freq:每个单词的频率,作为每个文档中单词的比例。

我们可以将所有这些与一个有效的例子放在一起。

from keras.preprocessing.text import Tokenizer
# define 5 documents
docs = ['Well done!',
'Good work',
'Great effort',
'nice work',
'Excellent!']
# create the tokenizer
t = Tokenizer()
# fit the tokenizer on the documents
t.fit_on_texts(docs)
# summarize what was learned
print(t.word_counts)
print(t.document_count)
print(t.word_index)
print(t.word_docs)
# integer encode documents
encoded_docs = t.texts_to_matrix(docs, mode='count')
print(encoded_docs)

代码清单4.10:使用Tokenizer进行拟合和编码的示例

运行该示例使Tokenizer5个小文档fit打印适合token的详细信息。然后使用字数对5个文档进行编码。每个文档被编码为9元素向量,每个字具有一个位置,并且每个字位置具有所选择的编码方案值。这里使用简单的字数计数模式(你可以试着改变mode参数:freq,tfidf,binary)

OrderedDict([('well', 1), ('done', 1), ('good', 1), ('work', 2), ('great', 1), ('effort', 1), ('nice', 1), ('excellent', 1)])

5

{'work': 1, 'well': 2, 'done': 3, 'good': 4, 'great': 5, 'effort': 6, 'nice': 7, 'excellent': 8}

defaultdict(<class 'int'>, {'well': 1, 'done': 1, 'work': 2, 'good': 1, 'great': 1, 'effort': 1, 'nice': 1, 'excellent': 1})

[[0. 0. 1. 1. 0. 0. 0. 0. 0.]

 [0. 1. 0. 0. 1. 0. 0. 0. 0.]

 [0. 0. 0. 0. 0. 1. 1. 0. 0.]

 [0. 1. 0. 0. 0. 0. 0. 1. 0.]

 [0. 0. 0. 0. 0. 0. 0. 0. 1.]]

代码清单4.11:使用Tokenizer进行fit和编码的示例输出

Tokenizer将是我们在本书中使用单词嵌入文本预处理的关键方法。

区经验公司. 回复之后发表社区发布网上.


0 条 查看最新 评论

没有评论
暂时无法发表评论