C++ Error passing DataLoader batch into model

mitchellg: <p>My task is to convert a working Pytorch neural network from Python to C++, but I’m not well versed in C++ so I’m learning as I go. <strong>I’m having an issue passing my DataLoader batch data into the model.</strong> Like examples show <a href="https://pytorch.org/cppdocs/frontend.html" rel="noopener nofollow ugc">https://pytorch.org/cppdocs/frontend.html</a> I’m attempting to pass my batch data, “batch” into the model like so:</p>
<blockquote>
<p>for (size_t epoch = 0; epoch &lt;= num_epochs; ++epoch)<br>
{<br>
// Initialize running metrics<br>
double loss1 = 0.0;</p>
<pre><code>  for (auto&amp; batch: *train_dataloader1)
  {
  	//std::cout &lt;&lt; batch &lt;&lt; "\n";

  	optimizer.zero_grad();

  	// Forward pass
  	auto outputs = model-&gt;forward(batch.data); // Error here
  }
</code></pre>
<p>}</p>
</blockquote>
<p>The errors I get when I try to build are “E1866 attribute does not apply to any entity” and “C3867 ‘std::vector&lt;SingleExample,std::allocator&lt;_Ty&gt;&gt;::data’: non-standard syntax; use ‘&amp;’ to create a pointer to member.” From these errors I’ve gathered (possibly incorrectly) that I need to pass in model-&gt;forward(batch.data()), but then I have three errors: “E1866 attribute does not apply to any entity”, "E0415 no suitable constructor exists to convert from “torch::data::TensorExample *” to “at::Tensor”, and finally “C2664 ‘at::Tensor Autoencoder1Impl::forward(at::Tensor)’: cannot convert argument 1 from ‘_Ty *’ to ‘at::Tensor’”</p>
<p>So I’m stuck because every example I’ve found is passing in the batch data like above, but the error instructs (to my best knowledge) to pass in the data differently. Any help is greatly appreciated!</p>
<p>Here is my model:</p>
<blockquote>
<p><span class="hashtag-raw">#include</span> &lt;torch/torch.h&gt;<br>
<span class="hashtag-raw">#include</span> “Models.h”</p>
<p>Autoencoder1Impl::Autoencoder1Impl(int64_t num_vars, int64_t layer_size)<br>
: encoder(num_vars, layer_size), decoder(layer_size, num_vars)<br>
{<br>
register_module(“encoder”, encoder);<br>
register_module(“decoder”, decoder);<br>
}</p>
<p>torch::Tensor Autoencoder1Impl::forward(torch::Tensor x)<br>
{<br>
x = torch::sigmoid(encoder-&gt;forward(x));<br>
x = decoder-&gt;forward(x);<br>
return x;<br>
}</p>
</blockquote>

pianoman8: <p>This isn’t helpful, but this is  very similar to the question I asked a few hours ago.  The problem is there are no examples or on the web that use datasets that return more than one in a batch. (They’re all image dataloaders).  You’re not getting a tensor to pass through to the model, you’re getting a std::vector (C++'s way of spelling “list”) of examples, which are whatever’s returned by the <code>get()</code> function in your dataset class… which is probably type <code>Example</code>.</p>
<p>The only way I’ve been able to make progress so far is manually converting my “list of examples” into a tensor is manually for each batch, which has to be wildly inefficient.  In my case, each example from my dataset is a 2D tensor. the model expects a 3D tensor, so I was able to do this:</p>
<pre><code class="lang-auto">  		for (auto&amp; batch : *data_loader) {
			std::vector&lt;torch::Tensor&gt; Xs_list, Ys_list;
		    std::transform(batch.begin(), batch.end(), std::back_inserter(Xs_list), [](auto &amp;e) {return e.data;});
		    std::transform(batch.begin(), batch.end(), std::back_inserter(Ys_list), [](auto &amp;e) {return e.target;});
			torch::Tensor X = torch::stack(Xs_list);
			torch::Tensor Y = torch::stack(Ys_list);

            optimizer.zero_grad();
      		torch::Tensor prediction = model-&gt;forward(X);
</code></pre>
<p>Maybe that will help point you towards an answer.</p>
<p>EDIT: After more poking, I finally got my network to train.  The above code is wrong, in that the <code>torch::from_blob</code> lines should be replaces with <code>torch::stack(Xs_list)</code>.  I’m editing the code now to reflect that.</p>

mitchellg: <p>Thanks for the input. This snippet of code you provided got me through my error. Unfortunately I’m running into another error. If you have the the time to briefly look at it, the next error is an Unhandled Exception which occurs in my model here:</p>
<blockquote>
<p>x = torch::sigmoid(encoder-&gt;forward(x))</p>
<p>Unhandled exception at 0x00007FF8AAE8A799 in NeuralNetwork_cpp.exe: Microsoft C++ exception: c10::Error at memory location 0x0000007A173CB3A0. occurred.</p>
</blockquote>
<p>Thanks again</p>