RuntimeError: one_hot is only applicable to index tensor

# 首先看传入的tensor的size,传入的tensor的维度不够则需要添加维度
# 这里的labels的size是tensor([n])的,传入需要添加维度
# 如果出现上述问题,只需要在后面加上to.(torch.int64)即可解决
# n是要分的种类
labels = torch.nn.functional.one_hot(labels.unsqueeze(0).to(torch.int64), n)

亲测有效!!!

posted @ 2021-11-18 14:57  咖啡陪你  阅读(4069)  评论(0编辑  收藏  举报