Skip to content

Instantly share code, notes, and snippets.

@MSWon
Created January 19, 2019 15:46
Show Gist options
  • Save MSWon/c637c8f3482dd6b7ac0d59ebea6e9fca to your computer and use it in GitHub Desktop.
Save MSWon/c637c8f3482dd6b7ac0d59ebea6e9fca to your computer and use it in GitHub Desktop.
tf.where_example
import tensorflow as tf
# Constants (3-element arrays).
a = tf.constant([100, 200, 300])
b = tf.constant([1, 2, 3])
# Use placeholder for predicate to where.
# ... We pass in an array of 3 bools to fill the placeholder.
j = tf.placeholder(tf.bool, [3])
# Use where to apply 1 of 2 methods based on each predicate.
# ... First argument is the predicate (contains bools).
# Second argument is run when true.
# Third argument is run when false.
x = tf.where(j, a + 5000, a + b)
# Run with 3 bools in placeholder.
array_temp = [False, True, False]
result = tf.Session().run(x, {j: array_temp})
# For false, add 2 elements toe get her.
# ... For true, add 5000 to first element.
print(result)
## Output
## [ 101 5200 303]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment