AttributeError: ‘int‘ object has no attribute ‘numel‘

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
. 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.

Appendix


I also found the following links useful. Contents regarding __getitem__ method helps me solve the problem.
1. python getitem()方法理解
2. 让你秒懂Python 类特殊方法__getitem__

你可能感兴趣的:(python)