2017-06-16 21 views
0

我有一个形状为(1)的浮点张量,其值在0.0到1.0之间。 我想“本”的范围内,这个张量,如:张量流中的多个if-else条件

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]) 

不知道怎么办呢!

回答

0

您还没有解释在边境点会发生什么(0.2,0.4,...),并没有显示出你想要什么为X> 0.6输出,所以我的假设是:

  • 关闭开放间隔;一个< X < = B
  • 相同的收集过程继续,直到1步0.2

因为如果其他条件(也将是缓慢的),你并不需要这么一个简单的例子。你可以用数学和铸造实现它:

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) 
0

尝试tg.logical_and 下面的例子可能有助于

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)