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 (?,).
we can modify a tensor by using the assignment operator. Assigning a new value in the tensor will modify the tensor with the new value.
You can use tf. math. count_nonzero() to check whether the tensor has all zeros or not.
Tensors are the basic data structures in TensorFlow, and they represent the connecting edges in a dataflow graph. A tensor simply identifies a multidimensional array or list. The tensor structure can be identified with three parameters: rank, shape, and type. Rank: Identifies the number of dimensions of the tensor.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With