2D张量中的唯一独特的行索引作为带有pytorch的输出1D张量

发布于 2025-02-04 15:28:28 字数 639 浏览 2 评论 0原文

当我在pytorch(版本1.6)的内部多类分类中实现target时,我有以下问题。

我得到了一个变量d< class'torch.tensor'> (与标签描述有关)大小为torch.size([16,128]),即[data_size,token_id_size]。

最初的想法是生成target torch的张量。 [0,1,2,...,15],用于内部多类分类。

torch.arange(16))进行此操作

可以使用target = torch.longtensor ( 同样,d中的唯一行具有target中的唯一索引。例如,d具有row0,row1,row8相同的token_ids或向量,而其他行则彼此不同,然后目标应为[0,0,2,3,4, 5,6,0,8,9,10,11,12,13,14,15][0,0,0,1,2,3,4,5,0,6,7 ,8,9,10,11,12,13],前者仍在索引0-15(但第1和7号),后者在0-13中具有所有索引。

我该如何实施?

When I implement target in in-batch multi-class classification on PyTorch (version 1.6), I have the following problem.

I got a variable D <class 'torch.Tensor'> (related to label description) of size as torch.Size([16, 128]), i.e. [data_size,token_id_size].

The original idea was to generate a target tensor of torch.Size([16]), each value is unique, corresponding to the rows in D, from 0 to 16 as [0,1,2,...,15], for in-batch multi-class classification.

This can be done using target = torch.LongTensor(torch.arange(16))

But there maybe repeated, non-unique rows in D, so I would like that the same, unique row in D has the its unique index in target. For example D has row0, row1, row8 the same token_ids or vector and the other rows are all different from each other, then target should be [0,0,2,3,4,5,6,0,8,9,10,11,12,13,14,15] or [0,0,1,2,3,4,5,0,6,7,8,9,10,11,12,13], wher the former has still indexes 0-15 (but no 1 and 7) and the latter has indexes of all in 0-13.

How can I implement this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

新一帅帅 2025-02-11 15:28:28

请参阅简化问题的答案(i)generate 1D tensor as unique index of rows of an 2D tensor and (ii)

但是这些似乎对于改善对比度的多级分类没有用。

See answers of the simplified question (i) generate 1D tensor as unique index of rows of an 2D tensor and (ii) generate 1D tensor as unique index of rows of an 2D tensor (keeping the order and the original index), which address the problem of this question.

But these seem not useful to improve the contrastive multi-class classification.

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