Error building github example

Hello and thanks for taking the time,

As a first attempt to utilize libTorch, I am trying to build the following example made available here:

However, using the latest libTorch library I have downloaded recently, I get this error:

error: cannot convert ‘at::Tensor’ to ‘c10::optional<std::tuple<at::Tensor, at::Tensor> >’

for this line:

auto rnn_output = time_serie_detector->forward(i_tmp, s_tmp);

The API has clearly changed since 2020, but I am entirely unsure how to fix this issue without affecting correctness. Could somebody please advise me how to do it right?

Thanks in advance!

It looks like you would need to separate the hidden state into h_0 and c_0 like in the Python API rather than having it packed together in a single tensor:

torch::Tensor state = torch::zeros({2, kSequenceLen, kHiddenDim});

should probably become something like

torch::Tensor h_0 = torch::zeros({kSequenceLen, kHiddenDim});
torch::Tensor c_0 = torch::zeros({kSequenceLen, kHiddenDim});

And then the forward method should be passed a tuple of h_0, c_0 as the second argument e.g.,

auto rnn_output = time_serie_detector->forward(i_tmp, std::make_tuple(h_0, c_0));

Note that rnn_output has changed to be a tuple as well, so the prints should also be changed to e.g.,

  pretty_print("rnn_output/output: ", std::get<0>(rnn_output));
  pretty_print("rnn_output/state: ", std::get<0>(std::get<1>(rnn_output)));

For more details you might want to check this PR which I believe contains the API change, and I’m not 100% sure I got the indicies of the tuples correct

Thank you very much!