繁体   English   中英

如何使用尺寸为None的tf.nn.top_k

[英]How to use tf.nn.top_k with dimension None

我想用tf.nn.top k替换argsortnumpy
但是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.

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