Error building github example

username: tamirmichael
cooked: <p>Hello and thanks for taking the time,</p>
<p>As a first attempt to utilize libTorch, I am trying to build the following example made available here:</p>
<aside class="onebox githubblob" data-onebox-src="https://github.com/Maverobot/libtorch_examples/blob/master/src/time_serie_prediction.cpp">
  <header class="source">

      <a href="https://github.com/Maverobot/libtorch_examples/blob/master/src/time_serie_prediction.cpp" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/Maverobot/libtorch_examples/blob/master/src/time_serie_prediction.cpp" target="_blank" rel="noopener nofollow ugc">Maverobot/libtorch_examples/blob/master/src/time_serie_prediction.cpp</a></h4>


      <pre><code class="lang-cpp">#include &lt;torch/torch.h&gt;

template &lt;typename T&gt;
void pretty_print(const std::string&amp; info, T&amp;&amp; data) {
  std::cout &lt;&lt; info &lt;&lt; std::endl;
  std::cout &lt;&lt; data &lt;&lt; std::endl &lt;&lt; std::endl;
}

int main(int /*argc*/, char* /*argv*/[]) {
  // Use GPU when present, CPU otherwise.
  torch::Device device(torch::kCPU);
  if (torch::cuda::is_available()) {
    device = torch::Device(torch::kCUDA);
    std::cout &lt;&lt; "CUDA is available! Training on GPU." &lt;&lt; std::endl;
  }

  const size_t kSequenceLen = 1;
  const size_t kInputDim = 1;
  const size_t kHiddenDim = 5;
  const size_t kOuputDim = 1;
</code></pre>



  This file has been truncated. <a href="https://github.com/Maverobot/libtorch_examples/blob/master/src/time_serie_prediction.cpp" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>However, using the latest libTorch library I have downloaded recently, I get this error:</p>
<blockquote>
<p>error: cannot convert ‘at::Tensor’ to ‘c10::optional&lt;std::tuple&lt;at::Tensor, at::Tensor&gt; &gt;’</p>
</blockquote>
<p>for this line:</p>
<blockquote>
<p>auto rnn_output = time_serie_detector-&gt;forward(i_tmp, s_tmp);</p>
</blockquote>
<p>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?</p>
<p>Thanks in advance!</p>

username: eqy
cooked: <p>It looks like you would need to separate the hidden state into <code>h_0</code> and <code>c_0</code> like in the Python API rather than having it packed together in a single tensor:</p>
<pre><code class="lang-auto">torch::Tensor state = torch::zeros({2, kSequenceLen, kHiddenDim});
</code></pre>
<p>should probably become something like</p>
<pre><code class="lang-auto">torch::Tensor h_0 = torch::zeros({kSequenceLen, kHiddenDim});
torch::Tensor c_0 = torch::zeros({kSequenceLen, kHiddenDim});
</code></pre>
<p>And then the forward method should be passed a tuple of <code>h_0</code>, <code>c_0</code> as the second argument e.g.,</p>
<pre><code class="lang-auto">auto rnn_output = time_serie_detector-&gt;forward(i_tmp, std::make_tuple(h_0, c_0));
</code></pre>
<p>Note that <code>rnn_output</code> has changed to be a tuple as well, so the prints should also be changed to e.g.,</p>
<pre><code class="lang-auto">  pretty_print("rnn_output/output: ", std::get&lt;0&gt;(rnn_output));
  pretty_print("rnn_output/state: ", std::get&lt;0&gt;(std::get&lt;1&gt;(rnn_output)));
</code></pre>
<p>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</p><aside class="onebox githubcommit" data-onebox-src="https://github.com/pytorch/pytorch/commit/bdd7dbfd4b75e66a88d393993b41c77f576f74fc">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/commit/bdd7dbfd4b75e66a88d393993b41c77f576f74fc" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Commit">
    <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path fill-rule="evenodd" d="M10.86 7c-.45-1.72-2-3-3.86-3-1.86 0-3.41 1.28-3.86 3H0v2h3.14c.45 1.72 2 3 3.86 3 1.86 0 3.41-1.28 3.86-3H14V7h-3.14zM7 10.2c-1.22 0-2.2-.98-2.2-2.2 0-1.22.98-2.2 2.2-2.2 1.22 0 2.2.98 2.2 2.2 0 1.22-.98 2.2-2.2 2.2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/commit/bdd7dbfd4b75e66a88d393993b41c77f576f74fc" target="_blank" rel="noopener nofollow ugc">[C++ API] RNN / GRU / LSTM layer refactoring (#34322)</a>
    </h4>

    <div class="github-info">
      <div class="date">
        committed <span class="discourse-local-date" data-format="ll" data-date="2020-03-16" data-time="00:48:29" data-timezone="UTC">12:48AM - 16 Mar 20 UTC</span>
      </div>

      <div class="user">
        <a href="https://github.com/yf225" target="_blank" rel="noopener nofollow ugc">
          <img alt="yf225" src="https://avatars.githubusercontent.com/u/4063635?v=4" class="onebox-avatar-inline" width="20" height="20">
          yf225
        </a>
      </div>

      <div class="lines" title="changed 11 files with 950 additions and 458 deletions">
        <a href="https://github.com/pytorch/pytorch/commit/bdd7dbfd4b75e66a88d393993b41c77f576f74fc" target="_blank" rel="noopener nofollow ugc">
          <span class="added">+950</span>
          <span class="removed">-458</span>
        </a>
      </div>
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">Summary:
This PR refactors RNN / GRU / LSTM layers in C++ API to exactly match t<span class="show-more-container"><a href="https://github.com/pytorch/pytorch/commit/bdd7dbfd4b75e66a88d393993b41c77f576f74fc" target="_blank" rel="noopener nofollow ugc" class="show-more">…</a></span><span class="excerpt hidden">he implementation in Python API.

**BC-breaking changes:**
- Instead of returning `RNNOutput`, RNN / GRU forward method now returns `std::tuple&lt;Tensor, Tensor&gt;`, and LSTM forward method now returns `std::tuple&lt;Tensor, std::tuple&lt;Tensor, Tensor&gt;&gt;`, matching Python API.
- RNN / LSTM / GRU forward method now accepts the same inputs (input tensor and optionally hidden state), matching Python API.
- RNN / LSTM / GRU layers now have `forward_with_packed_input` method which accepts `PackedSequence` as input and optionally hidden state, matching the `forward(PackedSequence, ...)` variant in Python API.
- RNN / LSTM / GRU layers no longer have these fields: `w_ih` / `w_hh` / `b_ih` / `b_hh`. Instead, to access the weights and biases of the gates, users should do e.g. `rnn-&gt;named_parameters()["weight_ih_l0"]`, which mirrors the Python API `rnn.weight_ih_l0`.
- In `RNNOptions`
    - `tanh()` / `relu()` / `activation` are removed. Instead, `nonlinearity` is added which takes either `torch::kTanh` or `torch::kReLU`
    - `layers` -&gt; `num_layers`
    - `with_bias` -&gt; `bias`
- In `LSTMOptions`
    - `layers` -&gt; `num_layers`
    - `with_bias` -&gt; `bias`
- In `GRUOptions`
    - `layers` -&gt; `num_layers`
    - `with_bias` -&gt; `bias`

The majority of the changes in this PR focused on refactoring the implementations in `torch/csrc/api/src/nn/modules/rnn.cpp` to match the Python API. RNN tests are then changed to reflected the revised API design.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/34322

Differential Revision: D20458302

Pulled By: yf225

fbshipit-source-id: ffff2ae1ddb1c742c966956f6ad4d7fba03dc54d</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


username: tamirmichael
cooked: <p>Thank you very much!</p>