mrgloom
mrgloom

Reputation: 21632

What is the difference between parameters and children?

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

Answers (2)

NeoZoom.lua
NeoZoom.lua

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):

  • children():

    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.

  • parameters(recurse=True):

    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

backtothemoon
backtothemoon

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

Related Questions