Reputation: 21632
It looks like parameters
and children
show the same info, so what is the difference between them?
import torch
print('torch.__version__', torch.__version__)
m = torch.load('imagenet_resnet18.pth')
print(m.parameters)
print(m.children)
Upvotes: 12
Views: 6864
Reputation: 2901
The (only, during my writing) current answer is not to the point, and thus misleading in my own opinion. By the current docs(08/23/2022):
Returns an iterator over immediate children modules.
This should mean that it will stop at non-leaf node like torch.nn.Sequential
, torch.nn.ModuleList
, etc.
Returns an iterator over module parameters. This is typically passed to an optimizer.
"Passed to an optimizer" should imply that recursive cases are taken care by the team. Just pass the return value/object to the optimizer.
Recommend this PyTorch post Module.children() vs Module.modules() to see the output of children()
.
Upvotes: 2
Reputation: 147
model.parameters()
is a generator that returns tensors containing your model parameters.model.children()
is a generator that returns layers of the model from which you can extract your parameter tensors using <layername>.weight
or <layername>.bias
Visit this link for a simple tutorial on accessing and freezing model layers.
Upvotes: 12