Return a pair/tuple in C++ nn.Module forward method

I saw almost all of the examples in the libtorch tutorial have returned a single Tensor in the forward method.

struct Net : torch::nn::Module {
   torch::Tensor forward(torch::Tensor x) {
     x = torch::relu(batch_norm1(conv1(x)));
     x = torch::relu(batch_norm2(conv2(x)));
     x = torch::relu(batch_norm3(conv3(x)));
     x = torch::tanh(conv4(x));
     return x;

However, I’m confused about how can we let the forward method return a tuple/pair? I’m currently doing a reinforcement learning task, and I need the network forward method to return both the policy and the position value. How can I translate the following code to C++? Is it possible to do it without the JIT stuff?

def forward(self, state_input):
        x = self.common_layer(state_input)
        x_action = self.policy_layer(x)
        x_value = torch.tanh(self.value_layer(x))
        return x_action, x_value

Thanks a lot!

Hi. I do not use libtorch for training so I may be completly off, but what happens if you replace the return line in the forward with:

return std::make_tuple(x_action, x_value);

at inference, retrieve the tuple with:

auto outputs = model.forward(inputs).toTuple();
at::Tensor action= outputs->elements()[0].toTensor();
at::Tensor value= outputs->elements()[1].toTensor();
1 Like