【问题标题】:Give two inputs to torch::jit::script::Module forward method给 torch::jit::script::Module forward 方法两个输入
【发布时间】:2022-01-09 14:47:06
【问题描述】:

我正在尝试使用 pytorch 在 python 中构建和训练网络。我的 forward 方法需要两个输入,如下所示:

def forward(self, x1, x2):

我在 python 中训练了这个模型并使用 torch.jit.script 保存。 然后我使用torch::jit::load 在 C++ 中加载这个模型。 我现在如何在 c++ 中将输入传递给模型?

如果我尝试将两个单独的张量传递给 forward 方法,如下所示

    std::vector<torch::jit::IValue> inputs1{tensor1};
    std::vector<torch::jit::IValue> inputs2{tensor2};
    at::Tensor output = module.forward(inputs1,inputs2).toTensor();

然后我收到一条错误消息,指出方法 forward 需要 1 个参数,提供 2 个。

我也不能连接这两个张量,因为形状在所有轴上都不同。

【问题讨论】:

标签: python c++ pytorch


【解决方案1】:

问题在于连接两个张量并将连接的张量作为模型的输入。然后在 forward 方法中,我们可以使用 concatenated tensor 创建两个单独的 tensor,分别用于输出计算。

为了使串联工作,我在张量后面附加了 0,这样它们在所有轴上的大小都相同,除了要进行串联的那个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-01
    • 2018-08-29
    • 2020-06-13
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    相关资源
    最近更新 更多