How can I access layers in a pytorch module by index?

后端 未结 2 1603
轻奢々
轻奢々 2021-01-24 05:08

I am trying to write a pytorch module with multiple layers. Since I need the intermediate outputs I cannot put them all in a Sequantial as usual. On the other hand, since there

2条回答
  •  無奈伤痛
    2021-01-24 05:40

    It is possible to list all layers on neural network by use

    list_layers = model.named_children()
    

    In the first case, you can use:

    parameters = list(Model1.parameters())+ list(Model2.parameters())
    optimizer = optim.Adam(parameters, lr=1e-3)
    

    In the second case, you didn't create the object, so basically you can try this:

    model = VAE()
    optimizer = optim.Adam(model.parameters(), lr=1e-3)
    

    By the way, you can start from modifying the VAE example provided by Pytorch.

    Perhaps you miss the initial function or initialize the model in a wrong way. See the init function here.

提交回复
热议问题