简体   繁体   中英

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. I would like to get their "outer product" tensor c as a tensor with shape (n, p, q) , such that its elements are

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?

你可以简单地做:

c = tf.expand_dims(a, 2) * b

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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