[英]How to use tf.nn.top_k with dimension None
我想用tf.nn.top k
替換argsort
( numpy
)
但是tf.nn.top_k
似乎不接受None
維度
這是我的代碼
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: ?
從這里找到答案
尺寸仍然沒有,但有效,驚訝
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.