简体   繁体   English

用于张量乘法的 keras/tensorflow 广播

[英]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) ,其中pq都很小,但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.

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