网络编程
位置:首页>> 网络编程>> Python编程>> pytorch实现onehot编码转为普通label标签

pytorch实现onehot编码转为普通label标签

作者:swuxyj  发布时间:2023-04-18 10:19:21 

标签:pytorch,onehot,label

label转onehot的很多,但是onehot转label的有点难找,所以就只能自己实现以下,用的topk函数,不知道有没有更好的实现


one_hot = torch.tensor([[0,0,1],[0,1,0],[0,1,0]])
print(one_hot)
label = torch.topk(one_hot, 1)[1].squeeze(1)
print(label)

tensor([[0, 0, 1],
[0, 1, 0],
[0, 1, 0]])

tensor([2, 1, 1])

来源:https://www.cnblogs.com/youmuchen/p/11108247.html

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com