How can I calculate conditional conditional expressions in batches in TensorFlow?

Basically, I have a lot of activation of layer neurons in the tensor Aform [batch_size, layer_size]. Let B = tf.square(A). Now I want to calculate the following condition for each element in each vector of this batch: if abs(e) < 1: e ← 0 else e ← B(e)where eis the element in B, which is in the same position as e. Is there any way to draw an entire operation with a single operation tf.cond?

+4
source share
1 answer

You can see tf.where(condition, x, y)

For your problem:

A = tf.placeholder(tf.float32, [batch_size, layer_size])
B = tf.square(A)

condition = tf.less(tf.abs(A), 1.)

res = tf.where(condition, tf.zeros_like(B), B)
+10
source

All Articles