StringLookup 类tf_keras.layers.StringLookup(
max_tokens=None,
num_oov_indices=1,
mask_token=None,
oov_token="[UNK]",
vocabulary=None,
idf_weights=None,
encoding="utf-8",
invert=False,
output_mode="int",
sparse=False,
pad_to_max_tokens=False,
**kwargs
)
一个预处理层,用于将字符串特征映射到整数索引。
此层通过基于表的词汇查找将一组任意字符串转换为整数输出。此层不会对输入字符串进行分割或转换。有关可以分割和标记自然语言的层,请参阅 tf.keras.layers.TextVectorization 层。
此层的词汇表必须在构造时提供,或通过 adapt() 学习。在 adapt() 期间,该层将分析数据集,确定单个字符串标记的频率,并从中创建词汇表。如果词汇表的大小受到限制,则将使用频率最高的标记来创建词汇表,而所有其他标记将被视为词汇表外 (OOV)。
该层有两种可能的输出模式。当 output_mode 为 "int" 时,输入字符串将被转换为其在词汇表中的索引(一个整数)。当 output_mode 为 "multi_hot"、"count" 或 "tf_idf" 时,输入字符串将被编码为数组,其中每个维度对应于词汇表中的一个元素。
词汇表可以选择包含一个掩码标记和一个 OOV 标记(根据 num_oov_indices 的设置,OOV 标记可以选择占用词汇表中的多个索引)。这些标记在词汇表中的位置是固定的。当 output_mode 为 "int" 时,词汇表将以掩码标记(如果设置了)开始,然后是 OOV 索引,最后是词汇表的其余部分。当 output_mode 为 "multi_hot"、"count" 或 "tf_idf" 时,词汇表将以 OOV 索引开始,并且掩码标记的实例将被丢弃。
有关预处理层的概述和完整列表,请参阅预处理 指南。
参数
pad_to_max_tokens=True 时才应指定此项。如果为 None,则词汇表大小没有上限。请注意,此大小包括 OOV 和掩码标记。默认为 None。1。output_mode 为 "int" 时,该标记包含在词汇表中并映射到索引 0。在其他输出模式下,该标记不会出现在词汇表中,并且输入中的掩码标记实例将被丢弃。如果设置为 None,则不会添加掩码项。默认为 None。invert 为 True 时使用。为 OOV 索引返回的标记。默认为 "[UNK]"。adapt() 该层。output_mode 为 "tf_idf" 时有效。一个元组、列表、一维 numpy 数组或一维张量,其长度与词汇表相同,包含浮点逆文档频率权重,这些权重将乘以每个样本的术语计数,以获得最终的 tf_idf 权重。如果设置了 vocabulary 参数,并且 output_mode 为 "tf_idf",则必须提供此参数。output_mode 为 "int" 时有效。如果为 True,此层将把索引映射到词汇表项,而不是将词汇表项映射到索引。默认为 False。"int"、"one_hot"、"multi_hot"、"count" 或 "tf_idf",配置层如下:"int":返回输入标记的原始整数索引。"one_hot":将输入中的每个单独元素编码到一个与词汇表大小相同的数组中,该数组在元素索引处包含一个 1。如果最后一个维度的大小为 1,则在该维度上进行编码。如果最后一个维度不为 1,则会附加一个新维度用于编码输出。"multi_hot":将输入中的每个样本编码为与词汇表大小相同的单个数组,其中包含一个 1,表示样本中存在的每个词汇表项。将最后一个维度视为样本维度,如果输入形状为 (..., sample_length),则输出形状为 (..., num_tokens)。"count":与 "multi_hot" 相同,但整数数组包含该索引处标记在样本中出现的次数。"tf_idf":与 "multi_hot" 相同,但应用 TF-IDF 算法来查找每个令牌槽中的值。对于 "int" 输出,支持任何形状的输入和输出。对于所有其他输出模式,目前仅支持高达秩 2 的输出。默认为 "int"。output_mode 为 "multi_hot"、"count" 或 "tf_idf" 时适用。如果为 True,即使词汇表中唯一标记的数量少于 max_tokens,输出的特征轴也会填充到 max_tokens,从而得到形状为 [batch_size, max_tokens] 的张量,而与词汇表大小无关。默认为 False。output_mode 为 "multi_hot"、"count" 或 "tf_idf" 时适用。如果为 True,则返回 SparseTensor 而不是密集 Tensor。默认为 False。"utf-8"。示例
使用已知词汇表创建查找层
此示例使用预先存在的词汇表创建查找层。
>>> vocab = ["a", "b", "c", "d"]
>>> data = tf.constant([["a", "c", "d"], ["d", "z", "b"]])
>>> layer = tf.keras.layers.StringLookup(vocabulary=vocab)
>>> layer(data)
<tf.Tensor: shape=(2, 3), dtype=int64, numpy=
array([[1, 3, 4],
[4, 0, 2]])>
使用自适应词汇表创建查找层
此示例创建一个查找层,并通过分析数据集生成词汇表。
>>> data = tf.constant([["a", "c", "d"], ["d", "z", "b"]])
>>> layer = tf.keras.layers.StringLookup()
>>> layer.adapt(data)
>>> layer.get_vocabulary()
['[UNK]', 'd', 'z', 'c', 'b', 'a']
请注意,OOV 标记 "[UNK]" 已添加到词汇表中。其余标记按频率排序(出现 2 次的 "d" 排在第一位),然后按反向排序。
>>> data = tf.constant([["a", "c", "d"], ["d", "z", "b"]])
>>> layer = tf.keras.layers.StringLookup()
>>> layer.adapt(data)
>>> layer(data)
<tf.Tensor: shape=(2, 3), dtype=int64, numpy=
array([[5, 3, 1],
[1, 2, 4]])>
多 OOV 索引查找
此示例演示了如何使用具有多个 OOV 索引的查找层。当一个层用一个以上的 OOV 索引创建时,任何 OOV 值都会被哈希到 OOV 桶的数量中,从而以确定的方式将 OOV 值分布在集合中。
>>> vocab = ["a", "b", "c", "d"]
>>> data = tf.constant([["a", "c", "d"], ["m", "z", "b"]])
>>> layer = tf.keras.layers.StringLookup(vocabulary=vocab,
... num_oov_indices=2)
>>> layer(data)
<tf.Tensor: shape=(2, 3), dtype=int64, numpy=
array([[2, 4, 5],
[0, 1, 3]])>
请注意,OOV 值 'm' 的输出为 0,而 OOV 值 'z' 的输出为 1。词汇表内的术语的输出索引比前面的示例增加了 1(a 映射到 2 等),以便为额外的 OOV 值腾出空间。
One-hot 输出
将层配置为 output_mode='one_hot'。请注意,one_hot 编码中的前 num_oov_indices 维表示 OOV 值。
>>> vocab = ["a", "b", "c", "d"]
>>> data = tf.constant(["a", "b", "c", "d", "z"])
>>> layer = tf.keras.layers.StringLookup(
... vocabulary=vocab, output_mode='one_hot')
>>> layer(data)
<tf.Tensor: shape=(5, 5), dtype=float32, numpy=
array([[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0.]], dtype=float32)>
多热输出
将层配置为 output_mode='multi_hot'。请注意,multi_hot 编码中的前 num_oov_indices 维表示 OOV 值。
>>> vocab = ["a", "b", "c", "d"]
>>> data = tf.constant([["a", "c", "d", "d"], ["d", "z", "b", "z"]])
>>> layer = tf.keras.layers.StringLookup(
... vocabulary=vocab, output_mode='multi_hot')
>>> layer(data)
<tf.Tensor: shape=(2, 5), dtype=float32, numpy=
array([[0., 1., 0., 1., 1.],
[1., 0., 1., 0., 1.]], dtype=float32)>
标记计数输出
将层配置为 output_mode='count'。与 multi_hot 输出一样,输出中的前 num_oov_indices 维表示 OOV 值。
>>> vocab = ["a", "b", "c", "d"]
>>> data = tf.constant([["a", "c", "d", "d"], ["d", "z", "b", "z"]])
>>> layer = tf.keras.layers.StringLookup(
... vocabulary=vocab, output_mode='count')
>>> layer(data)
<tf.Tensor: shape=(2, 5), dtype=float32, numpy=
array([[0., 1., 0., 1., 2.],
[2., 0., 1., 0., 1.]], dtype=float32)>
TF-IDF 输出
将层配置为 output_mode="tf_idf"。与 multi_hot 输出一样,输出中的前 num_oov_indices 维表示 OOV 值。
每个标记 bin 将输出 token_count * idf_weight,其中 idf 权重是每个标记的逆文档频率权重。这些应与词汇表一起提供。请注意,OOV 值的 idf_weight 将默认为传入的所有 idf 权重的平均值。
>>> vocab = ["a", "b", "c", "d"]
>>> idf_weights = [0.25, 0.75, 0.6, 0.4]
>>> data = tf.constant([["a", "c", "d", "d"], ["d", "z", "b", "z"]])
>>> layer = tf.keras.layers.StringLookup(output_mode="tf_idf")
>>> layer.set_vocabulary(vocab, idf_weights=idf_weights)
>>> layer(data)
<tf.Tensor: shape=(2, 5), dtype=float32, numpy=
array([[0. , 0.25, 0. , 0.6 , 0.8 ],
[1.0 , 0. , 0.75, 0. , 0.4 ]], dtype=float32)>
要为 oov 值指定 idf 权重,您需要传递整个词汇表,包括开头的 oov 标记。
>>> vocab = ["[UNK]", "a", "b", "c", "d"]
>>> idf_weights = [0.9, 0.25, 0.75, 0.6, 0.4]
>>> data = tf.constant([["a", "c", "d", "d"], ["d", "z", "b", "z"]])
>>> layer = tf.keras.layers.StringLookup(output_mode="tf_idf")
>>> layer.set_vocabulary(vocab, idf_weights=idf_weights)
>>> layer(data)
<tf.Tensor: shape=(2, 5), dtype=float32, numpy=
array([[0. , 0.25, 0. , 0.6 , 0.8 ],
[1.8 , 0. , 0.75, 0. , 0.4 ]], dtype=float32)>
在 "tf_idf" 模式下适应层时,每个输入样本将被视为一个文档,并且每个标记的 IDF 权重将计算为 log(1 + num_documents / (1 + token_document_count))。
逆向查找
此示例演示了如何使用此层将索引映射到字符串。(您也可以使用 adapt() 和 inverse=True,但为简单起见,我们在本示例中传递词汇表。)
>>> vocab = ["a", "b", "c", "d"]
>>> data = tf.constant([[1, 3, 4], [4, 0, 2]])
>>> layer = tf.keras.layers.StringLookup(vocabulary=vocab, invert=True)
>>> layer(data)
<tf.Tensor: shape=(2, 3), dtype=string, numpy=
array([[b'a', b'c', b'd'],
[b'd', b'[UNK]', b'b']], dtype=object)>
请注意,默认情况下,第一个索引对应于 oov 标记。
前向和逆向查找对
此示例演示如何使用标准查找层的词汇表创建逆向查找层。
>>> vocab = ["a", "b", "c", "d"]
>>> data = tf.constant([["a", "c", "d"], ["d", "z", "b"]])
>>> layer = tf.keras.layers.StringLookup(vocabulary=vocab)
>>> i_layer = tf.keras.layers.StringLookup(vocabulary=vocab, invert=True)
>>> int_data = layer(data)
>>> i_layer(int_data)
<tf.Tensor: shape=(2, 3), dtype=string, numpy=
array([[b'a', b'c', b'd'],
[b'd', b'[UNK]', b'b']], dtype=object)>
在此示例中,输入值 "z" 导致输出为 "[UNK]",因为 1000 不在词汇表中 - 它被表示为 OOV,并且在反向层中所有 OOV 值都返回为 "[UNK]"。另外,请注意,为了使反向工作,您必须在调用 get_vocabulary() 之前,通过直接或通过 adapt() 设置正向层的词汇表。