校正轴以使用点积来评估列表学习排序模型的最终输出



当条目都有值列表时,我找不到正确的配置来传递给tf.keras.layers.Dot以生成成对的点积,比如从列表学习到排序模型。例如,假设:

repeated_query_vector = [
[[1, 2], [1, 2]],
[[3, 4], [3, 4]]
]
document_vectors = [
[[5, 6], [7, 8]],
[[9, 10], [11, 12]],
]

调用tf.keras.layers.Dot(axs=??(([repeated_query_vector,document_vectors](我希望输出像:

[
[1*5 + 2*6, 1*7 + 2*8]
[3*9 + 4*10, 3*11 + 4*12]
]

我在文档中找到的所有示例都比我的用例少了一个维度。这个调用的轴的正确值是多少?

您应该能够使用tf.keras.layers.Multiply()tf.reshape:解决此问题

import tensorflow as tf
repeated_query_vector = tf.constant([
[[1, 2], [1, 2]],
[[3, 4], [3, 4]]
])
document_vectors = tf.constant([
[[5, 6], [7, 8]],
[[9, 10], [11, 12]],
])
multiply_layer = tf.keras.layers.Multiply()
result = multiply_layer([repeated_query_vector, document_vectors])
shape = tf.shape(result)
result = tf.reduce_sum(tf.reshape(result, (shape[0], shape[1] * shape[2])), axis=1, keepdims=True)
tf.Tensor(
[[ 40]
[148]], shape=(2, 1), dtype=int32)

或者使用tf.keras.layers.Dottf.reshape:

import tensorflow as tf
repeated_query_vector = tf.constant([
[[1, 2], [1, 2]],
[[3, 4], [3, 4]]
])
document_vectors = tf.constant([
[[5, 6], [7, 8]],
[[9, 10], [11, 12]],
])
dot_layer = tf.keras.layers.Dot(axes=1)
result = dot_layer([tf.reshape(repeated_query_vector, (repeated_query_vector.shape[0], repeated_query_vector.shape[1] * repeated_query_vector.shape[2])),
tf.reshape(document_vectors, (document_vectors.shape[0], document_vectors.shape[1] * document_vectors.shape[2]))])
tf.Tensor(
[[ 40]
[148]], shape=(2, 1), dtype=int32)

最新更新