2017-06-16 46 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)