张量流两个矩阵元素乘以轴= 0,(M,k)*(M,l) - > (M,K * L)

时间:2017-07-08 07:01:09

标签: tensorflow elementwise-operations

我有两个矩阵,F(形状=(4000,64))和M(形状=(4000,9)) 并希望得到具有shape =(4000,64 * 9)

的结果

我可以用下面的代码(理想的)来考虑for循环

<div class="wrapper">
  <div class="overlay"></div>
  <div class="parent">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
  </div>
</div>

但我知道For循环不支持tensorflow

是否有一个函数执行与上面代码相​​同的功能?

编辑)

我想出了一个主意。 F,M重复形状(4000,64 * 9)[在MATLAB中的liek repmat]和元素乘法。 你有没有其他想法?

2 个答案:

答案 0 :(得分:2)

如果您将输入重新设置为tf.matmulF(shape = (4000, 64, 1)),则可以使用M(shape=(4000,1, 9))。一个例子,

F = tf.Variable(tf.random_uniform(shape=(4000, 64, 1)))
M = tf.Variable(tf.random_uniform(shape=(4000, 1, 9)))
C = tf.matmul(F, M)
C = tf.reshape(C, (4000, -1))
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
print(C.eval().shape)

#Output: (4000, 576)

答案 1 :(得分:1)

您可以使用

tf.reshape(M[:,tf.newaxis,:] * F[...,tf.newaxis], [4000,-1])