TextVectorization
类keras.layers.TextVectorization(
max_tokens=None,
standardize="lower_and_strip_punctuation",
split="whitespace",
ngrams=None,
output_mode="int",
output_sequence_length=None,
pad_to_max_tokens=False,
vocabulary=None,
idf_weights=None,
sparse=False,
ragged=False,
encoding="utf-8",
name=None,
**kwargs
)
将文本特征映射到整数序列的预处理层。
此层包含用于在 Keras 模型中管理文本的基本选项。它将一批字符串(一个示例 = 一个字符串)转换为令牌索引列表(一个示例 = 一个整数令牌索引的一维张量)或密集表示(一个示例 = 一个浮点值的一维张量,表示有关示例令牌的数据)。此层旨在处理自然语言输入。要处理简单的字符串输入(分类字符串或预令牌化字符串),请参见 keras_core.layers.StringLookup
。
层的词汇表必须在构建时提供或通过 adapt()
学习。当此层进行自适应时,它将分析数据集,确定单个字符串值的频率,并从中创建词汇表。此词汇表可以具有无限大小或受上限限制,具体取决于此层的配置选项;如果输入中的唯一值多于最大词汇量,则最频繁的术语将用于创建词汇表。
每个示例的处理包含以下步骤:
关于传递可调用对象以自定义此层的拆分和标准化的一些说明:
keras.saving.register_keras_serializable
)。standardize
时,可调用对象接收的数据将与传递给此层的数据完全相同。可调用对象应返回与输入形状相同的张量。split
时,可调用对象接收的数据将压缩第一个维度——不是 [["string to split"], ["another string to split"]]
,而是 ["string to split", "another string to split"]
。可调用对象应返回一个 dtype 为 string
的 tf.Tensor
,其第一个维度包含拆分的令牌——在此示例中,我们应该看到类似 [["string", "to", "split"], ["another", "string", "to", "split"]]
的内容。注意:此层在内部使用 TensorFlow。除了 TensorFlow 之外,它不能作为任何后端模型的编译计算图的一部分使用。但是,在急切执行时,它可以与任何后端一起使用。它也可以始终作为任何后端(模型本身之外)的输入预处理管道的一部分使用,这是我们推荐使用此层的方式。
注意:此层可以在 tf.data
管道中使用(与您使用的后端无关)。
参数
pad_to_max_tokens=True
时指定。请注意,此词汇表包含 1 个 OOV 令牌,因此有效令牌数是 (max_tokens - 1 - (1 if output_mode == "int" else 0))
。None
:不进行标准化。"lower_and_strip_punctuation"
:文本将小写并去除所有标点符号。"lower"
:文本将小写。"strip_punctuation"
:将去除所有标点符号。None
:不拆分。"whitespace"
:按空格拆分。"character"
:按每个 Unicode 字符拆分。None
、整数或整数元组;传递整数将创建最大为该整数的 N 元组,传递整数元组将创建元组中指定值的 N 元组。传递 None
表示不创建 N 元组。"int"
、"multi_hot"
、"count"
或 "tf_idf"
,配置层如下:"int"
:输出整数索引,每个拆分的字符串令牌一个整数索引。当 output_mode == "int"
时,0 保留用于遮罩位置;这会将词汇量减少到 max_tokens - 2
而不是 max_tokens - 1
。"multi_hot"
:每个批次输出一个整数数组,大小为 vocab_size 或 max_tokens,其中所有元素中如果令牌映射到该索引在批次项中至少存在一次,则为 1。"count"
:类似于 "multi_hot"
,但整数数组包含该索引处的令牌在批次项中出现的次数。"tf_idf"
:类似于 "multi_hot"
,但应用 TF-IDF 算法来查找每个令牌槽中的值。对于 "int"
输出,支持任何形状的输入和输出。对于所有其他输出模式,目前仅支持秩 1 输入(拆分后秩 2 输出)。output_sequence_length
值,从而无论拆分步骤产生多少令牌,都将生成形状为 (batch_size, output_sequence_length)
的张量。默认为 None
。如果 ragged
为 True
,则 output_sequence_length
仍然可以截断输出。"multi_hot"
、"count"
和 "tf_idf"
模式下有效。如果为 True
,即使词汇表中唯一令牌的数量小于 max_tokens
,输出的特征轴也将填充到 max_tokens
,从而无论词汇量大小如何,都将生成形状为 (batch_size, max_tokens)
的张量。默认为 False
。adapt()
。output_mode
为 "tf_idf"
时有效。与词汇表长度相同的元组、列表、一维 NumPy 数组或一维张量,包含浮点逆文档频率权重,这些权重将乘以每个样本的术语计数,以获得最终的 tf_idf
权重。如果设置了 vocabulary
参数,并且 output_mode
为 "tf_idf"
,则必须提供此参数。"int"
输出模式。仅支持 TensorFlow 后端。如果为 True
,则返回 RaggedTensor
而不是密集 Tensor
,其中每个序列在字符串拆分后可能具有不同的长度。默认为 False
。"multi_hot"
、"count"
和 "tf_idf"
输出模式。仅支持 TensorFlow 后端。如果为 True
,则返回 SparseTensor
而不是密集 Tensor
。默认为 False
。"utf-8"
。示例
此示例实例化一个 TextVectorization
层,该层将文本小写,按空格拆分,去除标点符号,并输出整数词汇索引。
>>> max_tokens = 5000 # Maximum vocab size.
>>> max_len = 4 # Sequence length to pad the outputs to.
>>> # Create the layer.
>>> vectorize_layer = TextVectorization(
... max_tokens=max_tokens,
... output_mode='int',
... output_sequence_length=max_len)
>>> # Now that the vocab layer has been created, call `adapt` on the
>>> # list of strings to create the vocabulary.
>>> vectorize_layer.adapt(["foo bar", "bar baz", "baz bada boom"])
>>> # Now, the layer can map strings to integers -- you can use an
>>> # embedding layer to map these integers to learned embeddings.
>>> input_data = [["foo qux bar"], ["qux baz"]]
>>> vectorize_layer(input_data)
array([[4, 1, 3, 0],
[1, 2, 0, 0]])
此示例通过将词汇表术语列表传递给层的 __init__()
方法来实例化 TextVectorization
层。
>>> vocab_data = ["earth", "wind", "and", "fire"]
>>> max_len = 4 # Sequence length to pad the outputs to.
>>> # Create the layer, passing the vocab directly. You can also pass the
>>> # vocabulary arg a path to a file containing one vocabulary word per
>>> # line.
>>> vectorize_layer = keras.layers.TextVectorization(
... max_tokens=max_tokens,
... output_mode='int',
... output_sequence_length=max_len,
... vocabulary=vocab_data)
>>> # Because we've passed the vocabulary directly, we don't need to adapt
>>> # the layer - the vocabulary is already set. The vocabulary contains the
>>> # padding token ('') and OOV token ('[UNK]')
>>> # as well as the passed tokens.
>>> vectorize_layer.get_vocabulary()
['', '[UNK]', 'earth', 'wind', 'and', 'fire']