Ray.R.Chua
Ray.R.Chua

Reputation: 777

How to do row-wise element multiplication in tensorflow

Given a matrix [[x1, x2, x3], [y1, y2, y2], [z1, z2, z3]]

How can I do [x1*y1*z1 , x2*y2*z2, x3*y3*z3] in tensorflow?

Upvotes: 0

Views: 748

Answers (2)

Salvador Dali
Salvador Dali

Reputation: 222531

You need to use tf.reduce_prod(x, 0), because you multiply numbers along the columns:

import tensorflow as tf
a = tf.constant([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])
b = tf.reduce_prod(a, 0)

with tf.Session() as sess:
    print sess.run(b)

Upvotes: 1

Ray.R.Chua
Ray.R.Chua

Reputation: 777

I think tf.reduce_prod was what I was looking for. https://www.tensorflow.org/api_docs/python/tf/reduce_prod

Upvotes: 0

Related Questions