在Tensorflow中将3D张量与矩阵相乘的有效方法?

时间:2018-02-22 11:46:21

标签: tensorflow matrix-multiplication tensor

我有2个3D形状张量A(batch_size,dim1,dim2)和B大小(batch_size,dim2,dim3)。我想执行以下计算:

C = np.zeros((batch_size, dim1, dim3))
for i in range(batch_size):
    c[i] = A[i].dot(B[i])

我尝试了C = tf.matmul(A,B),但收到了错误:

  

tensorflow.python.framework.errors_impl.InvalidArgumentError:Shape       必须是等级2,但对于'MatMul'(op:'MatMul')的输入是3级       形状:[10,100,100],[10,100]

在TensorFlow中有没有有效的方法来实现它? 谢谢!

1 个答案:

答案 0 :(得分:0)

让我们先用numpy

来做
import numpy as np
batch_size = 5
dim1 = 7
dim2 = 2
dim3 = 3

A = np.random.rand(batch_size, dim1, dim2)
B = np.random.rand(batch_size, dim2, dim3)  
C = np.zeros((batch_size, dim1, dim3)) 

for i in range(batch_size):                         
    c[i] = A[i].dot(B[i])
print(np.einsum('ikl,ilm->ikm',A,B)) - C # prints a zero array

现在让我们使用tensorflow

import tensorflow as tf
a = tf.constant(A) # reusing numpy arrays
b = tf.constant(B)
op = tf.einsum('ikl,ilm->ikm',a,b)
with tf.Session() as sess:       
    print(sess.run(op) - C) # prints a zero array

现在根据这个answer,看起来性能应该与天真的实现非常相似。