KerasRS / API 文档 / 特征交互层 / DotInteraction 层

DotInteraction 层

[源代码]

DotInteraction

keras_rs.layers.DotInteraction(
    self_interaction: bool = False, skip_gather: bool = False, **kwargs: Any
)

DLRM 中存在的点积交互层。

此层计算每对特征之间不同的点积(“特征交互”)。如果 self_interaction 为 True,我们计算形式为 dot(e_i, e_j) 的点积,其中 i <= j;否则,我们计算形式为 dot(e_i, e_j) 的点积,其中 i < je_i 表示特征 i 的表示。该层可用于构建 DLRM 模型。

参数

  • self_interaction: bool。指示特征是否应该“自交互”。如果为 True,则也考虑交互矩阵的对角线元素。
  • skip_gather: bool。如果设置为 True,则交互矩阵的上三角部分设置为 0。输出的形状将是 [batch_size, num_features * num_features],其中一半的元素将为零。否则,输出将仅是交互矩阵的下三角部分。后者节省空间但速度慢得多。
  • **kwargs: 传递给基类的参数。

示例

# 1. Simple forward pass
batch_size = 2
embedding_dim = 32
feature1 = np.random.randn(batch_size, embedding_dim)
feature2 = np.random.randn(batch_size, embedding_dim)
feature3 = np.random.randn(batch_size, embedding_dim)
feature_interactions = keras_rs.layers.DotInteraction()(
    [feature1, feature2, feature3]
)

# 2. After embedding layer in a model
vocabulary_size = 32
embedding_dim = 6

# Create a simple model containing the layer.
feature_input_1 = keras.Input(shape=(), name='indices_1', dtype="int32")
feature_input_2 = keras.Input(shape=(), name='indices_2', dtype="int32")
feature_input_3 = keras.Input(shape=(), name='indices_3', dtype="int32")
x1 = keras.layers.Embedding(
    input_dim=vocabulary_size,
    output_dim=embedding_dim
)(feature_input_1)
x2 = keras.layers.Embedding(
    input_dim=vocabulary_size,
    output_dim=embedding_dim
)(feature_input_2)
x3 = keras.layers.Embedding(
    input_dim=vocabulary_size,
    output_dim=embedding_dim
)(feature_input_3)
feature_interactions = keras_rs.layers.DotInteraction()([x1, x2, x3])
output = keras.layers.Dense(units=10)(x2)
model = keras.Model(
    [feature_input_1, feature_input_2, feature_input_3], output
)

# Call the model on the inputs.
batch_size = 2
f1 = np.random.randint(0, vocabulary_size, size=(batch_size,))
f2 = np.random.randint(0, vocabulary_size, size=(batch_size,))
f3 = np.random.randint(0, vocabulary_size, size=(batch_size,))
outputs = model([f1, f2, f3])

参考文献


[源代码]

call 方法

DotInteraction.call(inputs: list[typing.Any])

点积交互层的前向传播。

参数

  • inputs: list。列表中的每个元素表示形状为 [batch_size, feature_dim] 的特征张量。列表中所有张量必须具有相同的形状。

返回

表示特征交互的张量。张量的形状为 [batch_size, k],其中如果 skip_gatherTrue,则 knum_features * num_features。否则,如果 self_interactionTrue,则 knum_features * (num_features + 1) / 2;如果为 False,则为 num_features * (num_features - 1) / 2