Afs*_*ooy 5 torch pytorch libtorch
在C ++版本的Libtorch中,我发现可以通过来获得float张量的值*tensor_name[0].data<float>(),在其中0我可以使用任何其他有效索引代替。但是,当我int通过at::kInt在张量创建中添加选项来定义张量时,我无法使用此结构来获取张量的值,即类似*tensor_name[0].data<at::kInt>()或*tensor_name[0].data<int>()不起作用的内容,调试器会不断说Couldn't find method at::Tensor::data<at::kInt>或Couldn't find method at::Tensor::data<int>。我可以通过获取值auto value_array = tensor_name=accessor<int,1>(),但是使用起来更容易*tensor_name[0].data<int>()。您能告诉我如何使用张data<>()量值int吗?
我的类型也有同样的问题bool。
谢谢,阿夫申
用于item<dtype>()从张量中获取标量。
int main() {
torch::Tensor tensor = torch::randint(20, {2, 3});
std::cout << tensor << std::endl;
int a = tensor[0][0].item<int>();
std::cout << a << std::endl;
return 0;
}
~/l/build ??? ./example-app
3 10 3
2 5 8
[ Variable[CPUFloatType]{2,3} ]
3
Run Code Online (Sandbox Code Playgroud)
以下代码打印0(在Linux上使用稳定的libtorch测试):
#include <torch/script.h>
#include <iostream>
int main(int argc, const char* argv[])
{
auto indx = torch::zeros({20},at::dtype(at::kLong));
std::cout << indx[0].item<long>() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1973 次 |
| 最近记录: |