nn.ModuleList.apply()?

It’s weight initialization code below:

import torch.nn.init as init

def xavier(param):
    init.xavier_uniform(param)
def weights_init(m):
    if isinstance(m, nn.Conv2d):
        xavier(m.weight.data)
        m.bias.data.zero_()

if VGG is a nn.ModuleList, and:

    VGG.apply(weights_init)

dosen’t throw an error.

But I just can’t find any documentation or statement about this .apply() function. Where could I find it?
Thanks a lot.

The documentation seems to be missing (i’ve made a note to add it), but apply loops over each child Module and applies a function to it.

3 Likes

just for closure of this thread, documentation is here: http://pytorch.org/docs/master/nn.html?highlight=modulelist#torch.nn.Module.apply

2 Likes

It seems the above link is outdated. As of Dec '21, the documentation is here:
https://pytorch.org/docs/master/generated/torch.nn.Module.html?highlight=apply#torch.nn.Module.apply

3 Likes