更正轴以使用点积评估列表学习排序模型的最终输出
Correct axes to use dot product to evaluate the final output of a listwise learning to rank model
当每个条目都有值列表时,我无法找到传递给 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(axes=??)([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.Dot
和 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]],
])
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)
当每个条目都有值列表时,我无法找到传递给 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(axes=??)([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.Dot
和 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]],
])
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)