生成一维张量作为2D张量行的独特索引

发布于 2025-02-05 18:40:21 字数 291 浏览 2 评论 0原文

假设我们通过给出每个行不同索引,从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空心↖ 2025-02-12 18:40:21

您可以使用 /a>并返回为框外提供索引的反向:

>>> _, i = x.unique(dim=0, return_inverse=True)

>>> i # first example
tensor([0, 1, 2])

You can do so using torch.Tensor.unique and returning the inverse which provides the indices out of the box:

>>> _, i = x.unique(dim=0, return_inverse=True)

>>> i # first example
tensor([0, 1, 2])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文