首页 > 编程知识 正文

AttributeError ‘int‘ object has no attribute ‘numel‘

时间:2023-05-03 05:17:33 阅读:184725 作者:1028

Problem
I was trying to enumerate through a torch.utils.data.DataLoaderobject, where it takes self-defined dataset_ as a parameter. The dataset looks like this: dataset_.append((img, raw_label, target_label, is_backdoor, acc, conf)).
Printing print(type(img),type(raw_label),type(target_label),is_backdoor,type(is_clean_acc),type(conf))
yields
<class 'torch.Tensor'> <class 'torch.Tensor'> <class 'int'> <class 'bool'> <class 'bool'> <class 'float'>. Note, however, that the class of target_label ( int) is inconsistent with the class of raw_label ( tensor). Solution
Inspired by the blog Here, I convert the target label by applying torch.tensor(target_label) and re-append it in the dataset. Then everything goes fine. I also found the following links useful. Contents regarding __getitem__ method helps me solve the problem.
1. python getitem()方法理解
2. 让你秒懂Python 类特殊方法__getitem__

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。