🐏

【QA】When is add_module helpful in PyTorch?

2024/06/04に公開

Summary of this discussion.

add_module in PyTorch

I got a question: what is add_module of pytorch?

The answer is this:

  1. Those examples are the same process.
    Example 1: Initialize submodules as Member Variabels

    class Net(nn.Module):
        def __init__(self):
    	super(Net, self).__init__()
    	self.conv1   = nn.Conv2d(3, 16, 5, padding=2)
    	self.pool    = nn.MaxPool2d(2, 2)
    	self.dropout = nn.Dropout2d(p=0.5)
    	self.conv2   = nn.Conv2d(16, 16, 5, padding=2)
    	self.conv3   = nn.Conv2d(16, 400, 11, padding=5)
    	self.conv4   = nn.Conv2d(400, 200, 1)
    	self.conv5   = nn.Conv2d(200, 1, 1)
    

    Example 2: Initialize submodules using add_module

    class Net(nn.Module):
        def __init__(self):
        super(Net, self).__init__()
        self.add_module("conv1", Conv2d(3, 16, 5, padding=2))
        self.add_module("pool", MaxPool2d(2, 2))
        self.add_module("dropout", Dropout2d(p=0.5))
        self.add_module("conv2", Conv2d(16, 16, 5, padding=2))
        self.add_module("conv3", Conv2d(16, 400, 11, padding=5))
        self.add_module("conv4", Conv2d(400, 200, 1))
        self.add_module("conv5", Conv2d(200, 1, 1))
    
  2. when better using add_module

    class Net(nn.Module):
        def __init__(self):
    	super(Net, self).__init__()
            modules = [...]  # some list of modules
            for module in modules:
                self.add_module(...)
    

This answer is make sence for me.

Reference

[1] When to use add_module function?, PyTorch

Discussion