生成一维张量作为2D张量行的独特索引
假设我们通过给出每个行不同索引,从0
行 - 行数-1
,我们将2D张量转换为1D张量。
[[1,2],[1,3],[1,4]] -> [0,1,2]
但是,如果有相同的行,那么我们将重复索引,如下所示。
[[1,2],[1,2],[1,4]] -> [0,0,2]
[[1,2],[1,3],[1,2]] -> [0,1,0]
如何在Pytorch上实施此操作?
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,2],[1,3],[1,4]] -> [0,1,2]
But if there are same rows, then we repeate the index, like this below.
[[1,2],[1,2],[1,4]] -> [0,0,2]
[[1,2],[1,3],[1,2]] -> [0,1,0]
How to implement this on PyTorch?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 /a>并返回为框外提供索引的反向:
You can do so using
torch.Tensor.unique
and returning the inverse which provides the indices out of the box: