Dhruv Ramani
Dhruv Ramani

Reputation: 2643

Tensorflow : Update Certain Tensor Indices On a Condition

I am using Tensorflow and I want to apply the condition below to my tensor:

if(value > 0):
   value = log(value)
else:
   value = -log(-value)

Currently, I am doing the following.

minone = tf.constant(-1, dtype=tf.float32)
condition = tf.less(X, minone)
idx = tf.where(condition)
portion = tf.gather_nd(X, idx)
log = -tf.log(-portion)
X = tf.scatter_nd_update(X, idx, log)

However, this only works when X is of type tf.Variable and not of a normal Tensor type.

How would I achieve a working condition for a variable of Tensor type?

Upvotes: 0

Views: 373

Answers (1)

Vladimir Bystricky
Vladimir Bystricky

Reputation: 1330

value = tf.sign(value) * tf.log(tf.abs(value))

Upvotes: 2

Related Questions