How does one download all possible layers allowable in Pytorch into one data structure?

pinocchio: <p>I want to have say, a list of all the allowable layers in pytorch. How do I get that info? Is it possible?</p>
<p>I am fine as a started for only sequential models…</p>

spanev: <p>I don’t know if there is a cleaner way  but you can do:</p>
<pre><code class="lang-auto">from torch import nn
all_layers = []
for sym in dir(nn):
    attr = getattr(nn, sym)
    if type(attr).__name__ == 'type':
        all_layers.append(attr)
</code></pre>

pinocchio: <p>nice! &lt;3</p>
<p>Is there an easy way to sort out what is an activation layer vs other layers?</p>
<p>Ideally the more fine granularity the better e.g.</p>
<ul>
<li>sequence layers</li>
<li>affine</li>
<li>activations</li>
<li>normalization</li>
<li>regularizations</li>
</ul>
<p>etc but at the very least separate activations layers from the rest…</p>

spanev: <p>This would work:</p>
<pre><code class="lang-auto">activations = []
for l in all_layers:                             
    if 'torch.nn.modules.activation.' in repr(l):
        activations.append(l)                
</code></pre>

pinocchio: <p>Amazing! <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>
<p>Btw, how do you know all this? how are you searching for this?</p>

spanev: <p>Well a nice thing about PyTorch is that you can easily use Python’s reflection, which complements the official docs. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"><br>
e.g. <code>dir(module)</code> the get the attributes of a module or <code>help(module.func)</code> to get directly the docstring associated to <code>module.func</code>.</p>
<p><a href="https://www.bnmetrics.com/blog/introspection-reflection-in-python" rel="nofollow noopener">Here</a> you can find more information about it.</p>

pinocchio: <p>on a related note, how does one do this:</p>
<aside class="quote quote-modified" data-post="1" data-topic="53305">
  <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/pinocchio/48/22093_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/is-it-possible-to-extract-automatically-all-the-arguments-to-pytorch-layers-functions-modules/53305">Is it possible to extract automatically all the arguments to Pytorch layers/functions/modules?</a> 
  </div>
  <blockquote>
    I asked a similar question where I want to download all the layers into pytorch: 


but I also want to have a list of all the hyperparameters/arguments for each of them. Is it possible to extract all of those automatically? (at the very least as string representations or something like that)
  </blockquote>
</aside>


pinocchio: <p>why are u using <code>repr</code> and not <code>str</code>?</p>

spanev: <p>In that specific case, <code>repr</code> and <code>str</code> will return the same string.</p>
<p>But in general <code>repr</code> should return all the important information on the object while <code>str</code> is <a href="https://www.geeksforgeeks.org/str-vs-repr-in-python/" rel="nofollow noopener">meant</a> to return only a formatted and human readable output.</p>