Correct way to save a sub-struct of torch::nn::Module in LibTorch

JamesDickens: <p>I am trying to figure what the proper way to save a substruct of torch::nn::Module.</p>
<p>The following snippet:</p>
<pre><code class="lang-auto">#include &lt;torch/torch.h&gt;
#include &lt;iostream&gt;

using namespace std;

struct example_module:torch::nn::Module {
	example_module() {
		first_l = register_module("conv1", torch::nn::Conv2d(3, 3, 1));
	}
	torch::nn::Conv2d first_l = nullptr;
};

int main() {
	string save_fp = "/localmodel";
	example_module model = example_module();
	torch::save(model, save_fp);
}
</code></pre>
<p>Gives me a compilation error:</p>
<blockquote>
<p>1&gt;C:\Libtorch\libtorch-win-shared-with-deps-debug-1.10.2+cu113\libtorch\include\torch\csrc\api\include\torch/serialize.h(44,1): error C2679: binary ‘&lt;&lt;’: no operator found which takes a right-hand operand of type ‘const Value’ (or there is no acceptable conversion)<br>
1&gt;        with<br>
1&gt;        [<br>
1&gt;            Value=example_module<br>
1&gt;        ]<br>
1&gt;C:\Libtorch\libtorch-win-shared-with-deps-debug-1.10.2+cu113\libtorch\include\torch\csrc\api\include\torch/serialize/tensor.h(7,34): message : could be ‘torch::serialize::OutputArchive &amp;torch::operator &lt;&lt;(torch::serialize::OutputArchive &amp;,const at::Tensor &amp;)’<br>
1&gt;C:\Libtorch\libtorch-win-shared-with-deps-debug-1.10.2+cu113\libtorch\include\c10/util/logging_is_not_google_glog.h(232,22): message : or       ‘std::ostream &amp;std::operator &lt;&lt;(std::ostream &amp;,const std::nullptr_t &amp;)’ [found using argument-dependent lookup]<br>
1&gt;C:\Libtorch\libtorch-win-shared-with-deps-debug-1.10.2+cu113\libtorch\include\torch\csrc\api\include\torch/nn/module.h(556,34): message : or       ‘std::ostream &amp;torch::nn::operator &lt;&lt;(std::ostream &amp;,const torch::nn::Module &amp;)’ [found using argument-dependent lookup]<br>
1&gt;C:\Libtorch\libtorch-win-shared-with-deps-debug-1.10.2+cu113\libtorch\include\torch\csrc\api\include\torch/nn/module.h(605,37): message : or       ‘torch::serialize::OutputArchive &amp;torch::nn::operator &lt;&lt;(torch::serialize::OutputArchive &amp;,const std::shared_ptr<a>torch::nn::Module</a> &amp;)’ [found using argument-dependent lookup]<br>
1&gt;C:\Libtorch\libtorch-win-shared-with-deps-debug-1.10.2+cu113\libtorch\include\torch\csrc\api\include\torch/serialize.h(44,1): message : while trying to match the argument list ‘(torch::serialize::OutputArchive, const Value)’<br>
1&gt;        with<br>
1&gt;        [<br>
1&gt;            Value=example_module<br>
1&gt;        ]<br>
1&gt;sample_module.cpp(16): message : see reference to function template instantiation ‘void torch::save&lt;example_module,std::string&amp;&gt;(const Value &amp;,std::string &amp;)’ being compiled<br>
1&gt;        with<br>
1&gt;        [<br>
1&gt;            Value=example_module<br>
1&gt;        ]<br>
1&gt;Done building project “TorchProjectExample.vcxproj” – FAILED.<br>
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========</p>
</blockquote>
<p>What am I doing wrong?</p>

Yuichiro_Shishido: <aside class="quote no-group" data-username="JamesDickens" data-post="1" data-topic="158091">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jamesdickens/48/45317_2.png" class="avatar"> JamesDickens:</div>
<blockquote>
<p><code>struct example_module:torch::nn::Module {</code></p>
</blockquote>
</aside>
<p>please try</p>
<p>//wrong<br>
struct example_module:torch::nn::Module</p>
<p>//right<br>
struct example_module:public torch::nn::Module</p>