我試圖在張量流中取兩個向量的內積,為此我使用點積:
x = tf.constant([1, 2, 3], dtype=tf.int32)
y = tf.constant([4, 5, 6], dtype=tf.int32)
# desired result
tf.tensordot(x, y, axes=1)
# Output: 32
現在我正在處理都有 shape 的批量張量(32, 3)。我仍然想要相同的操作,產生一個 shape 的輸出向量(32, )。到目前為止,我唯一成功的嘗試是:
tf.linalg.diag_part(tf.tensordot(x, y, axes=[[1], [1]]))
# Output: <tf.Tensor: shape=(32,)>
# where each entry is the inner product of the vectors of length 3
但是,我根據需要計算了 32 個內積。
如何更有效地解決我的問題?
uj5u.com熱心網友回復:
想想這個操作在一天結束時是什么:元素乘法和軸 1 上的總和。所以你可以這樣做
tf.reduce_sum(x * y, axis=1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/434145.html
