有没有办法做张量矩阵乘法?

时间:2018-10-21 20:19:36

标签: python tensorflow

有没有办法获得张量(具有批处理尺寸)的乘积行为,类似于批处理尺寸等于1的2D矩阵之间的tf.matmul?

具体来说,我想执行2D矩阵(6,255)和张量(2,255,255,1)(批处理尺寸等于2),其中:

import tensorflow as tf
import numpy as np

im = np.random.rand(255, 255)
A = np.random.rand(6, 255)
B = np.array([im,im]).reshape([-1,255, 255,1])

batch_size = 2
a = tf.placeholder(tf.float64,shape=(6, 255))
b = tf.placeholder(tf.float64,shape=(batch_size,255, 255,1))
out_mat = tf.matmul(a,b) #Didn't work 
with tf.Session() as sess:
    sess.run(out_mat, feed_dict={a: A, b: B})

,结果应为(2,6,255,1)(谢谢@rvinas)形状。

注意:在张量流中,matmul只能处理2D矩阵,而batch_matmul可以 只能用(...,n,p)来做(...,m,n),其中A和B中的...相同。

1 个答案:

答案 0 :(得分:2)

这是使用隐式广播和tf.reduce_sum的一种方法:

class externalClass {
  testData = new dataModel()

  testing(){
    this.testData.data.test1 = "WW"; // i'm able to set value which i shouldn't (data is a getter)
    this.testData.data = {test1:"WW",data:{} // this will trow error as expected
  }
}

以及使用tf.matmultf.reshapetf.transpose的另一种方式:

import tensorflow as tf
import numpy as np

batch_size = 2
dim_1 = 3
dim_2 = 4
dim_3 = 5
dim_4 = 32

im = np.arange(dim_2 * dim_3).reshape(dim_2, dim_3)
A = np.arange(dim_1 * dim_2).reshape(dim_1, dim_2)
B = im
C = np.matmul(A, B)
print('NumPy result (for batch_size=1):\n {}'.format(C))

B = np.repeat(B[None, ..., None], batch_size, axis=0)
B = np.repeat(B, dim_4, axis=3)
print(B.shape)  # B shape=(batch_size, dim_2, dim_3, dim_4)

a = tf.placeholder(tf.float64, shape=(dim_1, dim_2))
b = tf.placeholder(tf.float64, shape=(batch_size, dim_2, dim_3, dim_4))
a_ = a[None, :, :, None, None]  # Shape=(1, dim_1, dim_2, 1, 1)
b_ = b[:, None, :, :, :]  # Shape=(batch_size, 1, dim_2, dim_3, dim_4)
out_mat = tf.reduce_sum(a_ * b_, axis=2)

with tf.Session() as sess:
    c = sess.run(out_mat, feed_dict={a: A, b: B})
    print('TF result (for batch_size={}):\n {}'.format(batch_size, c))
    assert c.shape == (batch_size, dim_1, dim_3, dim_4)

对于您的特定示例,您将设置b_ = tf.transpose(b, [1, 0, 2, 3]) # Shape=(dim_2, batch_size, dim_3, dim_4) b_ = tf.reshape(b_, [dim_2, -1]) # Shape=(dim_2, batch_size * dim_3 * dim_4) matmul = a @ b_ # Shape=(dim_1, batch_size * dim_3 * dim_4) matmul_ = tf.reshape(matmul, [dim_1, batch_size, dim_3, dim_4]) out_mat = tf.transpose(matmul_, [1, 0, 2, 3]) # Shape=(batch_size, dim_1, dim_3, dim_4) batch_size=2dim_1=6dim_2=255dim_3=255