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
本文介绍了解决使用libtorch函数torch::index_select时出现的类型错误问题,即期望获得Variable类型却得到了CUDALongType类型的index。文中详细解释了Variable与Tensor的区别,并提供了一种将Tensor转换为Variable的方法。
523

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



