2D张量中的唯一独特的行索引作为带有pytorch的输出1D张量
当我在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅简化问题的答案(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.