特定の次元を持つ2つの行列の行列乗算がnumpyで機能しますが、テンソルフローでは機能しません。行列の乗算テンソルと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)))
これは期待どおりに動作して印刷:私は上記の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)
を取得するのと同じ形状のプレースホルダや変数を乗算しようとすると、tensorflowにしかし
np shapes: (10, 20, 30) x (30, 40) = (10, 20, 40)
結果は
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は何ですか?第2のエントリーを10,20,30に放送し、10マットマットで20,30を(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