I have confusions about the autograd of a customed network. Here are two examples:
Example 1
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.linear1 = nn.Linear(5, 10)
        self.linear2 = nn.Linear(10, 5)

    def forward(self, x):
        x = self.linear1(x)
        x = self.linear2(x)
        return x

net = Net()
net1. net = net
net1.fc = nn.Linear(5,10)

Then it’s optimize func. Both net1. net and net1.fc will be sent as the params.
Example 2
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.linear1 = nn.Linear(5, 10)
        self.linear2 = nn.Linear(10, 5)
        self.fc = nn.Linear(5,10)

    def forward(self, x):
        x = self.linear1(x)
        x = self.linear2(x)
        x = self.fc(x)
        return x

net = Net()

Then it’s optimize func.
Which way is correct? It seems if you don’t write the module in the forward func, you will have a wrong gradient.