2017-01-12 47 views
0

我想用tf.nn.top k更換argsortnumpy
但它似乎tf.nn.top_k不接受None尺寸
這裏是我的代碼如何使用tf.nn.top_k與尺寸無

cond1 = tf.greater_equal(ws, min_size) # assume shape is (100,) 
cond2 = tf.greater_equal(hs, min_size) # assume shape is (100,) 
cond = cond1 & cond2 # shape is (100) 

# cause I don't give x and y, so tf.where return index of True element 
# but number of True is unknow now 
keep = tf.where(cond) # so shape is (?,1) 
keep = tf.reshape(keep, [-1]) # shape is (?,) 

val = tf.gather(val, keep) # shpae is (?,) 
argsort = tf.nn.top_k(val, val.get_shape()[0]) 
# ValueError: Cannot convert an unknown Dimension to a Tensor: ? 

回答

0

查找here
尺寸仍然沒有,但它的工作原理的答案,驚訝

val = tf.gather(val, keep) # shape is (?,) 
shape_list = tf.unpack(tf.shape(val)) 
argsort = tf.nn.top_k(val, shape_list[0]) # it works