libtorch中 tensot---> Variable

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值