生成一维张量作为2D张量行的独特索引(保持订单和原始索引)
假设我们通过给出每个行,不同行的不同索引,从0
行数-1 。
[[1,4],[1,3],[1,2]] -> [0,1,2]
但是,如果有相同的行,我们将重复索引,如下所示,“原始”索引为k-1
,对于k
-
[[1,4],[1,2],[1,2]] -> [0,1,1]
如果没有重复该行(如下面的第三行),其索引应为其原始索引,它是k-1
的k
-th Row(例如2 for 2 for [1,4])。
[[1,3],[1,3],[1,4]] -> [0,0,2]
一个更长的例子:
[[1,2],[4,3],[1,4],[1,4],[4,3],[1,2],[5,6],[7,8]] -> [0,1,2,2,1,0,6,7]
如何在pytorch上实施它?
This question is an updated version of generate 1D tensor as unique index of rows of an 2D tensor
Let's say we transform a 2D tensor to a 1D tensor by giving each, different row a different index, from 0
to the number of rows - 1
.
[[1,4],[1,3],[1,2]] -> [0,1,2]
But if there are same rows, we repeat the index, like this below, the "original" index is k-1
for the k
-th row
[[1,4],[1,2],[1,2]] -> [0,1,1]
Also if there is no repeat for the row (like the third row below), its index should be its original index, which is k-1
for the k
-th row (for example 2 for [1,4]).
[[1,3],[1,3],[1,4]] -> [0,0,2]
A longer example:
[[1,2],[4,3],[1,4],[1,4],[4,3],[1,2],[5,6],[7,8]] -> [0,1,2,2,1,0,6,7]
How to implement this on PyTorch?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下解决方案是矢量化的,但依赖于Numpy的
unique
函数。The following solution is vectorized, but relies on NumPy's
unique
function.请参阅@michael的非矢量化解决方案,
另一个非矢量化解决方案是
See the non-vectorized solution from @Michael
Another non-vectorized solution is