简体   繁体   English

不同等级的张量批量tf.matmul

[英]Batch tf.matmul of tensors with different ranks

Given a Tensor of shape (A, B, C, D, E) and a tensor of shape (A, B, E) , I would like to do a batch multiplication with automatic broadcasting of the second tensor, such that: 给定一个形状(A, B, C, D, E)的张量和一个形状(A, B, E)的张量,我想对第二张量进行自动广播以进行批量乘法,使得:

    In [1]: X = tf.placeholder(dtype=tf.float32, shape=[A, B, C, D, E])
    In [2]: Y = tf.placeholder(dtype=tf.float32, shape=[A, B, E])
    In [3]: # DO SOMETHING LIKE tf.matmul(X, Y)
    Out[3]: <tf.Tensor 'MatMul_1:0' shape=(A, B, C, D) dtype=float64>

Is it even possible? 可能吗?

Thanks in advance 提前致谢

Broadcasting is mostly supported for those operations which makes use of element-wise computation. 对于那些使用逐元素计算的操作,大多数情况下都支持广播。 tf.matmul is not element-wise operation but tf.multiply is element-wise based. tf.matmul不是基于元素的操作,而是tf.multiply是基于元素的。

Also, Tensorflow may or may not do automatic broadcasting for higher order tensors even in those operations where broadcasting is supported. 同样,即使在支持广播的操作中,Tensorflow可能也可能不会针对高阶张量进行自动广播。 In those cases, you will manually have to do it. 在这种情况下,您将必须手动执行此操作。

I have written another answer which does automatic broadcasting for higher order tensors. 我写了另一个答案 ,它会自动广播高阶张量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM