如何使用tf.nn.top_k与维度Non

2024-06-13 19:06:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用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: ?

Tags: sizeistftopvalnnequalmin
1条回答
网友
1楼 · 发布于 2024-06-13 19:06:18

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

相关问题 更多 >