0

我想输入 row = [0.160625, 0.967468297, 3.520480583, 0.862454481, -0.341933766] 作为浮点类型的条目并将其传递给转发模块。我用 python 试图翻译成 C++,我得到了语法错误。需要支持。谢谢!

    // run not okay
    // Create a vector of inputs.
    std::vector<torch::jit::IValue> inputs;
    row = [0.190625, 0.957468297, 4.520480583, 0.962454481, -0.241933766]
    inputs.push_back(torch::tensor(row));

    // Execute the model and turn its output into a tensor.
    at::Tensor output = module.forward(inputs).toTensor();
    std::cout << output.slice(/*dim=*/1, /*start=*/0, /*end=*/4) << '\n';

我想使用 row 作为实例并获取输出。当我使用诸如 torch::one({1, 5}) 之类的虚拟值时,应用程序运行正常。但是,当我将实际值作为行 - 浮点数组传递时,应用程序被中止。

    // run ok for this case 
    // Create a vector of inputs.
    std::vector<torch::jit::IValue> inputs;
    inputs.push_back(torch::ones({ 1, 5}));

    // Execute the model and turn its output into a tensor.
    at::Tensor output = module.forward(inputs).toTensor();
    std::cout << output.slice(/*dim=*/1, /*start=*/0, /*end=*/4) << '\n';
4

1 回答 1

1

您是否尝试过替换[]{}前面提到的?

float row[] = { 0.190625, 0.957468297, 4.520480583, 0.962454481, -0.241933766 };
于 2022-02-13T17:47:30.387 回答