Is there a way to directly loop through the submodules (all layers) of the network?)

Wang_Kesen: <p>What I see is that the method modules or named_modules wrap all the layers inside shared_ptr and store them in a different address. How can I directly access these modules without calling the attribute name?</p>
<p>This is in libtorch. What I wanted to do is like the named_modules or modules in python, where the modules are directly the copies of the submodules.</p>

LogicPy: <p>In libtorch (the C++ version of PyTorch), you can access the modules in a similar way you’d do in Python. However, instead of using <code>.named_modules()</code> or <code>.modules()</code> like in Python, you’ll have to use the <code>.children()</code> or <code>.named_children()</code> function which returns an iterator over immediate child modules.</p>
<p>Here’s a small example:</p>
<pre><code class="lang-cpp">auto net = torch::nn::Sequential(torch::nn::Linear(10, 50),
                                 torch::nn::ReLU(),
                                 torch::nn::Linear(50, 2));

for (auto&amp; module : net-&gt;named_children()) {
    std::cout &lt;&lt; module.key() &lt;&lt; ": " &lt;&lt; module.value() &lt;&lt; std::endl;
}
</code></pre>
<p>In this example, <code>named_children()</code> returns an iterator over immediate child modules, providing both the name of the module and the module itself. You can also use <code>children()</code> if you don’t care about the names and just want the modules.</p>
<p>For accessing a specific module directly, you can use something like this:</p>
<pre><code class="lang-cpp">auto linear_layer = net-&gt;ptr(torch::nn::Linear);
</code></pre>
<p>This will give you a shared pointer to the first <code>Linear</code> module in your network.</p>
<p>If you need to access all the modules (including nested ones), you might have to write a recursive function to traverse the module hierarchy. As of now, there isn’t a direct libtorch equivalent to PyTorch’s <code>.modules()</code> or <code>.named_modules()</code>.</p>

Wang_Kesen: <p>Thank you so much for your reply I just checked the children method. It still returns this type shared_ptr<a>torch::nn::Module</a>. It seems like the original module (torch::nn::Linear or others alike) are upcasted to the base struct torch::nn::Module.</p>
<p>struct Net : torch::nn::Module {</p>
<p>Net():fc0(torch::nn::Conv2dOptions(1,1,2)),<br>
fc1(torch::nn::LinearOptions(2,3).bias(true))<br>
,fc2(torch::nn::LinearOptions(3,32).bias(true)),<br>
fc3(torch::nn::LinearOptions(32,10).bias(true))<br>
{<br>
// Construct and register two Linear submodules.<br>
register_module(“fc1”, fc1);<br>
register_module(“fc2”, fc2);<br>
register_module(“fc3”, fc3);<br>
register_module(“fc0”, fc0);<br>
}</p>
<p>torch::Tensor forward(torch::Tensor x) {<br>
// Use one of many tensor manipulation functions.<br>
x = torch::relu(fc1-&gt;forward(x.reshape({x.size(0), 784})));<br>
x = torch::dropout(x, /<em>p=</em>/0.5, /<em>train=</em>/is_training());<br>
x = torch::relu(fc2-&gt;forward(x));<br>
x = torch::log_softmax(fc3-&gt;forward(x), /<em>dim=</em>/1);<br>
return x;<br>
}<br>
for(auto &amp; module : test_net.children()){<br>
cout&lt;&lt;module&lt;&lt;endl;<br>
}<br>
cout&lt;&lt;&amp;test_net.fc1&lt;&lt;endl;</p>
<p>The returned address of the for loop is 0x2d6abf0, 0x2d6eca0, 0x2d6f680.</p>
<p>Whereas the  cout&lt;&lt;&amp;test_net.fc1&lt;&lt;endl returns 0x7ffe2d70b668. Therefore I believe the address has been changed. Also, to access the weight attributes etc, torch::nn::Module does not provide those. I think the shared_ptr should be converted using shared_ptr-&gt;as<a>torch::nn::Linear</a>(). Then, it is a new address again. I was wondering how to directly access fc1, fc2, fc3, etc using a for loop without calling test_net.fc1 explicitly?</p>