簡體   English   中英

沒有為4D 3D張量的張量流中的tf.matmul廣播

[英]no broadcasting for tf.matmul in tensorflow for 4D 3D tensors

首先我在這里找到另一個問題TensorFlow中沒有廣播tf.matmul
但這個問題並沒有解決我的問題。

我的問題是一批矩陣乘以另一批向量。

x=tf.placeholder(tf.float32,shape=[10,1000,3,4])
y=tf.placeholder(tf.float32,shape=[1000,4])

x是一批矩陣。有10 * 1000個矩陣。每個矩陣都是有形的[3,4]
y是一批向量。有1000個向量。每個向量都是形狀[4]
x的暗淡1和y的暗淡0是相同的。 (這是1000)
如果tf.matmul支持廣播,我可以寫

y=tf.reshape(y,[1,1000,4,1])
result=tf.matmul(x,y)
result=tf.reshape(result,[10,1000,3])

但是tf.matmul不支持廣播
如果我使用上面引用的問題的方法

x=tf.reshape(x,[10*1000*3,4])
y=tf.transpose(y,perm=[1,0]) #[4,1000]
result=tf.matmul(x,y)
result=tf.reshape(result,[10,1000,3,1000])

結果是形狀[10,1000,3,1000],而不是[10,1000,3]。
我不知道如何刪除多余的1000
如何獲得與支持廣播的tf.matmul相同的結果?

我自己解決了。

x=tf.transpose(x,perm=[1,0,2,3]) #[1000,10,3,4]
x=tf.reshape(x,[1000,30,4])
y=tf.reshape(y,[1000,4,1])
result=tf.matmul(x,y) #[1000,30,1]
result=tf.reshape(result,[1000,10,3])
result=tf.transpose(result,perm=[1,0,2]) #[10,1000,3]

如圖所示在這里 ,你可以用一個函數來解決:

def broadcast_matmul(A, B):
  "Compute A @ B, broadcasting over the first `N-2` ranks"
  with tf.variable_scope("broadcast_matmul"):
    return tf.reduce_sum(A[..., tf.newaxis] * B[..., tf.newaxis, :, :],
                         axis=-2)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM