dfridman1
dfridman1

Reputation: 171

Setting all negative values of a tensor to zero (in tensorflow)

Here's my problem. I have a tensor X and I want to set all negative values to zero. In numpy, I would do the following np.maximum(0, X). Is there any way to achieve the same effect in tensorflow? I tried tf.maximum(tf.fill(X.get_shape(), 0.0), X), but this throws ValueError: Cannot convert a partially known TensorShape to a Tensor: (?,).

PS. X is a 1-D tensor of shape (?,).

Upvotes: 8

Views: 16441

Answers (4)

mrry
mrry

Reputation: 126154

As it happens, your problem is exactly the same as computing the rectifier activation function, and TensorFlow has a built-in operator, tf.nn.relu(), that does exactly what you need:

X_with_negatives_set_to_zero = tf.nn.relu(X)

Upvotes: 27

Florian
Florian

Reputation: 931

A simple solution is to use the cast function keras documentation (as suggested by @ldavid)

X = tf.cast(X > 0, X.dtype) * X

Moreover this can be adapted to any threshold level with :

X = tf.cast(X > threshold, X.dtype) * X

Upvotes: 4

Jayati Deshmukh
Jayati Deshmukh

Reputation: 79

You can use tf.clip_by_value function as follows:

t = tf.clip_by_value(t, min_val, max_val)

It will clip tensor t in the range [min_val, max_val]. Here you can set min_val to 0 to clip all negative values and set those to 0. More documentation about clip_by_value.

Upvotes: 6

mayk93
mayk93

Reputation: 1537

One possible solution could be this (although it's not the best):

class TensorClass(object):
    def __init__(tensor_values):
        self.test_tensor = tf.Variable(tensor_values, name="test_tensor")

test_session = tf.Session()
with test_session.as_default():
    tc = TensorClass([1, -1, 2, -2, 3])
    test_session.run(tf.initialize_all_variables())
    test_tensor_value = test_session.run(tc.test_tensor)
    print(test_tensor_value) # Will print [1, -1, 2, -2, 3]
    new_test_tensor_value = [element * int(element > 0) for element in test_tensor_value]
    test_tensor_value_assign_op = tf.assign(tc.test_tensor, new_test_tensor_value)
    test_session.run(test_tensor_value_assign_op)
    test_tensor_value = test_session.run(tc.test_tensor)
    print(test_tensor_value) # Will print [1 0 2 0 3]

While this does what you need, it's not done in tensorflow. We are pulling out a tensorflow variable, changing it, and putting it back again.

For performance critical things, don't use this because it's not very efficient.

Upvotes: -1

Related Questions