[英]keras/tensorflow broadcast for tensor multiplication
I have two tensors: tensor a
has shape (n, 1)
, and tensor b
has shape (p, q)
where both p
and q
are small but n
is large.我有两个张量:张量a
形状为(n, 1)
,张量b
形状为(p, q)
,其中p
和q
都很小,但n
很大。 I would like to get their "outer product" tensor c
as a tensor with shape (n, p, q)
, such that its elements are我想将他们的“外积”张量c
作为形状为(n, p, q)
的张量,这样它的元素是
c[i, j, k] = a[i, 0] * b[j, k]
How should I use tensorflow/keras broadcast or functions to achieve this without incurring a loop?我应该如何使用 tensorflow/keras 广播或函数来实现这一点而不会产生循环?
你可以简单地做:
c = tf.expand_dims(a, 2) * b
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.