The difference between nn.ModuleList() in pytorch and python's list

For details, please check: Detailed explanation of ModuleList and Sequential in PyTorch

nn.ModuleList, it is a container that stores different modules and automatically adds the parameters of each module to the network. You can add any subclass of nn.Module (such as nn.Conv2d, nn.Linear, etc.) to this list. The method is the same as that of Python's own list, which is nothing more than operations such as extend and append.

But different from the general list, the module added to nn.ModuleList will be automatically registered to the entire network, and the parameters of the module will also be automatically added to the entire network.

If you use python's list, there will be problems!
Convolutional layers and their parameters added using Python's list are not automatically registered with our network. Of course, we can still use forward to calculate the output. However, if the instantiated network is used for training, because the parameters of these layers are not in the entire network, the network parameters will not be updated, that is, it cannot be trained.

Guess you like

Origin blog.csdn.net/qq_41915623/article/details/131045421