what(): Expected object of type Variable but found type CUDALongType for argument #2 ‘index’ (checked_cast_variable at …/…/torch/csrc/autograd/VariableTypeManual.cpp:38)
问题描述:在使用libtorch函数torch::index_select(detections_class_left,0, index)时,运行报错如上,而进入index_select的定义显示:static inline Tensor index_select(const Tensor & self, int64_t dim, const Tensor & index)
问题分析:
通过报错提示argument #2 ‘index’ 应该是 Variable类型,而我使用的就是 tensor(数据类型为CUDALongType),因此一直报错。
问题解决:
1.搞清楚Variable类型和 tensor的区别,在这里引用另一篇文章的解读:知乎的文章;
2.把tensor转成Variable,使用函数:torch::autograd::make_variable(left_index, false);// tensot—> Variable