简体   繁体   中英

Tensorflow: Iterate tensor row-wise and perform elementwise multiplication

I've got two tensors,

x = shape(batchsize, 29, 64), 
y = shape(batchsize, 29, 29, 64)

I want to iterate row-wise over y and perform an elementwise multiplication with x, the result should be of a shape (batch size, 29, 64).

How I would program it sequentially:

for batchnr in range(x.shape[0]): 
    for elem in y[batchnr]:
        x[batchnr] = tf.multiply(x[batchnr], elem)

I tried several things using tf.scan, tf.map_fn, tf.while_loop. However, I can't figure out how to do it right and efficient.

If I understand your question properly, you would like to, for each example in a batch, do the multiplication of 29 matrices of shape (29, 64) in y[batchnr] , element-wise, then with x, also element-wise. If that is correct, then I think you can use tf.reduce_prod() .

For example,

# x = shape(batchsize, 29, 64), 
# y = shape(batchsize, 29, 29, 64)
# ...

z = tf.reduce_prod(y, axis=1)  # shape(batchsize, 29, 64), product of 29 matrices element-wise
r = tf.multiply(x, z)  # shape(batchsize, 29, 64)

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