报错信息:
RuntimeError: expected scalar type Long but found Int
出错地方是损失函数部分
查阅资料:
- https://www.cnblogs.com/lfri/p/15492610.html
- https://blog.csdn.net/onlyforbest/article/details/114736779?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-114736779-blog-116709167.pc_relevant_multi_platform_whitelistv3&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-114736779-blog-116709167.pc_relevant_multi_platform_whitelistv3&utm_relevant_index=1
学习收获:
criterion(x,y.long()) 损失函式里面要求target(也就是y)的类型应该是long类型。
出错代码:
losses_adapt = self.criterion(pred_dict['s_logits'].reshape(2*n_support_train*n_query,2), batch_data['s_label'].repeat(n_query))
根据报错提示,这里target(也就是batch_data['s_label'].repeat(n_query))的数据类型是Int类型,不是long类型,所以要进行数据格式转换。下面是修改后代码:
losses_adapt = self.criterion(pred_dict['s_logits'].reshape(2*n_support_train*n_query,2), batch_data['s_label'].repeat(n_query).long())
也就是在target(也就是batch_data['s_label'].repeat(n_query))后面加上.long(),进行数据格式转换。