Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the apply(fn) function in pytorch work with a function without return statement as argument?

I have some questions about the following code fragments:

>>> def init_weights(m):
        print(m)
        if type(m) == nn.Linear:
            m.weight.data.fill_(1.0)
            print(m.weight)

>>> net = nn.Sequential(nn.Linear(2, 2), nn.Linear(2, 2))
>>> net.apply(init_weights)

apply() is part of the pytorch.nn package. You find the code in the documentation of this package. The final questions: 1. Why does this code sample work, although there is no argument or brackets added to init_weights() when it is given to apply()? 2. Where does the function init_weights(m) gets its argument m from, when it's given as a parameter to the function apply() without brackets and an m?

like image 685
JaMoin Avatar asked Jul 16 '26 06:07

JaMoin


1 Answers

We find the answers to your questions in said documentation of torch.nn.Module.apply(fn):

Applies fn recursively to every submodule (as returned by .children()) as well as self. Typical use includes initializing the parameters of a model (see also torch-nn-init).

  1. Why does this code sample work, althouh there is no argument or brackets added to init_weights() when it is given to apply()?
    • The given function init_weights isn't called prior to the apply call, precisely because there are no parentheses, rather a reference to init_weights is given to apply, and only from within apply later on init_weights is called.
  2. Where does the function init_weights(m) gets its argument m from, when it's given as a parameter to the function apply() without brackets and an m?
    • It gets its argument with each call within apply, and, as the documentation tells, it is called for m iterating over every submodule of (in this case) net as well as net itself, due to the method call net.apply(…).
like image 55
Armali Avatar answered Jul 18 '26 21:07

Armali