Gilfoyle
Gilfoyle

Reputation: 3616

Replace all values in tensor greater than zero

I have a tensor X. If I just want to keep all values larger than zero I can use

X = tf.nn.relu(X)

But what do I do in the opposite case? I only can think of this solution:

X = tf.multiply(-1.0, tf.nn.relu(tf.multiply(-1.0, X)))

I wondered if there is a more sophisticated way to do that.

Any suggestions?

Upvotes: 1

Views: 1593

Answers (1)

nuric
nuric

Reputation: 11225

So you want to keep all the values less than zero. You can reverse the relu operation and use tf.minimum(X, 0) which "returns the min of x and y (i.e. x < y ? x : y) element-wise" and supports broadcasting, doc.

Upvotes: 3

Related Questions