HashedCrossing 类tf_keras.layers.HashedCrossing(num_bins, output_mode="int", sparse=False, **kwargs)
一个使用“哈希技巧”对特征进行交叉的预处理层。
该层使用“哈希技巧”对类别特征进行交叉。从概念上讲,这种转换可以视为:hash(concatenate(features)) % num_bins。
该层目前仅对标量输入和批量标量输入执行交叉。有效的输入形状为 (batch_size, 1)、(batch_size,) 和 ()。
有关预处理层的概述和完整列表,请参阅预处理指南。
参数
"int" 或 "one_hot",配置如下:"int": 直接返回整数桶索引。"one_hot": 将输入中的每个元素编码成一个与 num_bins 大小相同的数组,在输入对应的桶索引处包含一个 1。默认为 "int"。"one_hot" 模式。如果为 True,则返回一个 SparseTensor 而非密集 Tensor。默认为 False。示例
交叉两个标量特征。
>>> layer = tf.keras.layers.HashedCrossing(
... num_bins=5)
>>> feat1 = tf.constant(['A', 'B', 'A', 'B', 'A'])
>>> feat2 = tf.constant([101, 101, 101, 102, 102])
>>> layer((feat1, feat2))
<tf.Tensor: shape=(5,), dtype=int64, numpy=array([1, 4, 1, 1, 3])>
交叉并进行独热编码(one-hot)两个标量特征。
>>> layer = tf.keras.layers.HashedCrossing(
... num_bins=5, output_mode='one_hot')
>>> feat1 = tf.constant(['A', 'B', 'A', 'B', 'A'])
>>> feat2 = tf.constant([101, 101, 101, 102, 102])
>>> layer((feat1, feat2))
<tf.Tensor: shape=(5, 5), dtype=float32, numpy=
array([[0., 1., 0., 0., 0.],
[0., 0., 0., 0., 1.],
[0., 1., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 0., 1., 0.]], dtype=float32)>