0

I have a float tensor with shape (1) whose value lies between 0.0 and 1.0. I want to 'bin' the range in this tensor, as in:

if 0.0 < x < 0.2: 
  return tf.Constant([0])
if 0.2 < x < 0.4: 
  return tf.Constant([1])
if 0.4 < x < 0.6: 
  return tf.Constant([2])
if 0.6 < x: 
  return tf.Constant([3])

No idea how to do it!

2 Answers 2

1

You have not explained what will happen in the border points (0.2, 0.4, ...) and have not shown what do you want to output for x > 0.6, so my assumptions are:

  • closed open interval; a < x <= b
  • the same bin procedure continues till 1 with a step 0.2

For such a simple case you do not need if else condition (also it will be slow). You can achieve it with math and casting:

import tensorflow as tf

x = tf.constant(0.25)
res = tf.cast(5 * x, tf.int32)
with tf.Session() as sess:
    print sess.run(res)
Sign up to request clarification or add additional context in comments.

Comments

0

try tg.logical_and the following example might help

b = tf.constant([5,2,-3,1])
c1 = tf.greater(b,0) # b>0
c2 = tf.less(b,5) # b<5
c_f = tf.logical_and(c1, c2) # 0 < b < 5
sess=tf.Session()
sess.run(c_f)

1 Comment

how do you return a value(tensor)? In the question, the code is returning a constant converted to tensor value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.