Effective_cellist
Effective_cellist

Reputation: 1128

Sum dot products and constant in tensorflow

I'm trying to do the following calculation in tensorflow

Y = X1*W1 + X2*W2 + X3*W3 + b

X's and W's have same shape, X*W is dot product. After playing around for a while I found that the following piece does the job

Y = tf.add(tf.add(tf.add(tf.reduce_sum(tf.multiply(X1,W1)),tf.reduce_sum(tf.multiply(X2,W2))),tf.reduce_sum(tf.multiply(X3,W3))),b)

What is a concise way to perform this operation in tensorflow?

Upvotes: 1

Views: 282

Answers (1)

rmeertens
rmeertens

Reputation: 4451

What about defining the dotproduct function?

def dotprod(X,W):
  return tf.reduce_sum(tf.multiply(X,W))
Y = dotprod(X1,W1) + dotprod(X2,W2) + dotprod(X3,W3) + b

And I believe that you can use the + to add tensors.

Let me know if this works!

Upvotes: 2

Related Questions