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 大小相同的数组,其中输入对应的 bin 索引处为 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])>
交叉并进行独热编码两个标量特征。
>>> 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)>