[Python] How to extract a pytorch network container class name

Discussão em 'Python' iniciado por Stack, Março 11, 2025.

  1. Stack

    Stack Membro Participativo

    Assume I have a network created as follows:

    p = torch.nn.Sequential(torch.nn.Linear(self.inputSize, self.outputSize))


    I know that I can print the network with:

    print(p)


    and get:

    Sequential(
    (0): Linear(in_features=22, out_features=3, bias=True)
    )


    I want to pretty print the network thus, I can use

    for name, module in p.named_children():
    print(f'{name:>10} {module}')


    to print each network layer's name. For the example network above I'd get:

    0 Linear(in_features=22, out_features=3, bias=True)


    But how to I get the 'Sequential' part? It's the nn module container class so is the only method to do this to dissect the class name returned by type (<class 'torch.nn.modules.container.Sequential'>)?

    Continue reading...

Compartilhe esta Página