pytorch loss = loss_func(output, label) 报错
发布日期:2021-05-06 23:49:09 浏览次数:10 分类:技术文章

本文共 424 字,大约阅读时间需要 1 分钟。

在运行损失函数时 loss = loss_func(output, label) 时报了两个错 : 

 

1.

RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #2 'target'

这是因为label必须是 LongTensor 类型, 之前是 :

label = t.tensor(float(image[1]))  # image[1]为str类型

改为 :

label = t.tensor(float(image[1])).long()

 

2. 

Assertion `cur_target >= 0 && cur_target < n_classes’ failed

这是因为我的数据集类别是从1开始,需要依次减一,所以要改为 :

label = t.tensor(float(image[1])-1).long()

 

 

上一篇:linux 常用工具
下一篇:Codeforces Round #441 (Div. 2, by Moscow Team Olympiad)

发表评论

最新留言

表示我来过!
[***.240.166.169]2025年03月31日 14时49分36秒