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.
Appendix
I also found the following links useful. Contents regarding
__getitem__ method helps me solve the problem.
1. python getitem()方法理解
2. 让你秒懂Python 类特殊方法__getitem__
这篇博客讨论了一个Python编程中遇到的错误,即尝试对整数类型对象调用'numel'属性时出现AttributeError。解决方案和相关补充信息在文章中详细阐述。
2519

被折叠的 条评论
为什么被折叠?



