I am learning tensorflow, I know in numpy I can do
a = np.random.randn(1,2,3)
a[a<0.5] = -1
How can I do the same in tensorflow? Thank you!
Make use of tf.less and tf.where.
t1 = tf.Variable(np.random.randn(1,2,3), dtype = tf.float32)
t2 = tf.less(t1, 0.5)
t3 = tf.where(t2, tf.fill((1,2,3), -1.0), t1)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(t1))
print(sess.run(t3))
Output:
[[[-2.36796331 -0.29641244 1.46340346]
[-0.38756183 -0.39763084 -0.34627825]]]
[[[-1. -1. 1.46340346]
[-1. -1. -1. ]]]