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 < j)。e_i表示特征i的表示。该层可用于构建DLRM模型。
参数
[batch_size, num_features * num_features],其中一半的元素将为零。否则,输出将仅为交互矩阵的下三角部分。后者可以节省空间,但速度要慢得多。示例
# 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])
点积交互层的正向传播。
参数
[batch_size, feature_dim]的特征张量。列表中的所有张量必须具有相同的形状。返回
表示特征交互的张量。张量的形状为[batch_size, k],其中如果skip_gather为True,则k为num_features * num_features。否则,如果self_interaction为True,则k为num_features * (num_features + 1) / 2;如果为False,则为num_features * (num_features - 1) / 2。