简体   繁体   中英

How to use tf.nn.top_k with dimension None

I want to use tf.nn.top k to replace argsort ( numpy )
but it seem that tf.nn.top_k doesn't accept None dimension
here is my code

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: ?

Find the answer from here
dimension still None but it works, surprised

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM