當每個條目都有值串列時,我無法找到正確的配置傳遞給 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]
]
我在檔案中找到的所有示例都比我的用例少一維。此呼叫的正確軸值是多少?
uj5u.com熱心網友回復:
你應該能夠用tf.keras.layers.Multiply()and解決這個問題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.Dotand 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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/426833.html
標籤:Python 张量流 喀拉斯 numpy-ndarray
