我有兩個矩陣與某些維度的矩陣乘法numpy工作,但在tensorflow中不起作用的情況。矩陣乘法tensorflow與numpy的區別
x = np.ndarray(shape=(10,20,30), dtype = float)
y = np.ndarray(shape=(30,40), dtype = float)
z = np.matmul(x,y)
print("np shapes: %s x %s = %s" % (np.shape(x), np.shape(y), np.shape(z)))
可正常工作和打印:
np shapes: (10, 20, 30) x (30, 40) = (10, 20, 40)
但是在tensorflow當我嘗試乘同一形狀的佔位符和變量如上numpy的陣列我得到一個錯誤
x = tf.placeholder(tf.float32, shape=(10,20,30))
y = tf.Variable(tf.truncated_normal([30,40], name='w'))
print("tf shapes: %s x %s" % (x.get_shape(), y.get_shape()))
tf.matmul(x,y)
結果於
tf shapes: (10, 20, 30) x (30, 40)
InvalidArgumentError:
Shape must be rank 2 but is rank 3 for 'MatMul_12'
(op: 'MatMul') with input shapes: [10,20,30], [30,40].
爲什麼此操作失敗?
numpy matmul在這裏做什麼?廣播第二次進入10,20,30和20,30到10乘以(30,40)?似乎TF matmul缺少廣播,可能值得提交功能請求。你可以通過執行'y = tf.Variable(tf.truncated_normal([30,40],name ='w')+ tf.zeros((10,30,40)))''觸發廣播。相關問題(可能由於錯誤而關閉) - https://github.com/tensorflow/tensorflow/issues/216 –
matmul在這裏與'np.einsum('ijk,kl-> ijl',x,y) ' – Kuba