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])>
交叉和独热编码两个标量特征。
>>> 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)>