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)>