如何在 TensorFlow 中的批处理中计算逐元素条件?
How can I compute element-wise conditionals on batches in TensorFlow?
我基本上在形状 [batch_size, layer_size]
的张量 A
中有一层的一批神经元激活。让B = tf.square(A)
。现在我想计算此批次中每个向量中每个元素的以下条件: if abs(e) < 1: e ← 0 else e ← B(e)
其中 e
是 B
中与 e
处于相同位置的元素.我可以通过单个 tf.cond
操作以某种方式向量化整个操作吗?
你可能想看看tf.where(condition, x, y)
针对您的问题:
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)
我基本上在形状 [batch_size, layer_size]
的张量 A
中有一层的一批神经元激活。让B = tf.square(A)
。现在我想计算此批次中每个向量中每个元素的以下条件: if abs(e) < 1: e ← 0 else e ← B(e)
其中 e
是 B
中与 e
处于相同位置的元素.我可以通过单个 tf.cond
操作以某种方式向量化整个操作吗?
你可能想看看tf.where(condition, x, y)
针对您的问题:
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)