【发布时间】:2020-11-12 20:59:53
【问题描述】:
首先我知道我应该使用top_k,但是让 k-max 池难以(在 TF 中实现)的原因在于它必须保持顺序。
到目前为止我所拥有的:
import tensorflow as tf
from tensorflow.contrib.framework import sort
sess = tf.Session()
a = tf.convert_to_tensor([[[5, 1, 10, 2], [3, 11, 2, 6]]])
b = sort(tf.nn.top_k(a, k=2)[1])
print(tf.gather(a, b, axis=-1).eval(session=sess))
已经接近了,但还没有
我得到了什么:
[[[[[ 5, 10], [ 1, 2]]], [[[ 3, 2], [11, 6]]]]]
我想要什么:
[[[5, 10], [11, 6]]]
我几乎百分百确定需要 gather_nd,但我无法弄清楚,而且我是 pytorch 用户,在那里真的很容易
import torch
a = torch.LongTensor([[[5, 1, 10, 2], [3, 11, 2, 6]]])
b = a.topk(2, dim = -1)[1].sort(dim = -1)[0]
print(a.gather(-1, b))
哦,而且我发现的每个代码都不是一个保留顺序的代码(这在语义上是错误的)
【问题讨论】:
标签: tensorflow keras