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

I want to have say, a list of all the allowable layers in pytorch. How do I get that info? Is it possible?

I am fine as a started for only sequential models…

I don’t know if there is a cleaner way but you can do:

from torch import nn
all_layers = []
for sym in dir(nn):
    attr = getattr(nn, sym)
    if type(attr).__name__ == 'type':
        all_layers.append(attr)
1 Like

nice! <3

Is there an easy way to sort out what is an activation layer vs other layers?

Ideally the more fine granularity the better e.g.

  • sequence layers
  • affine
  • activations
  • normalization
  • regularizations

etc but at the very least separate activations layers from the rest…

This would work:

activations = []
for l in all_layers:                             
    if 'torch.nn.modules.activation.' in repr(l):
        activations.append(l)                

Amazing! :smiley:

Btw, how do you know all this? how are you searching for this?

Well a nice thing about PyTorch is that you can easily use Python’s reflection, which complements the official docs. :slight_smile:
e.g. dir(module) the get the attributes of a module or help(module.func) to get directly the docstring associated to module.func.

Here you can find more information about it.

2 Likes

on a related note, how does one do this:

why are u using repr and not str?

In that specific case, repr and str will return the same string.

But in general repr should return all the important information on the object while str is meant to return only a formatted and human readable output.