Can I change the order of layers in __init__()?

Normally we use pytorch like the following code.

class MyNet(nn.Module):
  def __init__(self):
    super().__init__()
    self.layer1 = nn.Conv2d(3, 32, 3)
    self.layer2 = nn.Conv2d(32, 64, 3)
    self.layer3 = nn.Conv2d(64, 2, 3)
  def forward(self, x):
    out = self.layer1(x)
    out = self.layer2(out)
    out = self.layer3(out)
    return out

My question is: is the order of layers in init() function matter?
Is the following code right?

class MyNet(nn.Module):
  def __init__(self):
    super().__init__()
    self.layer3 = nn.Conv2d(64, 2, 3)
    self.layer2 = nn.Conv2d(32, 64, 3)
    self.layer1 = nn.Conv2d(3, 32, 3)
  def forward(self, x):
    out = self.layer1(x)
    out = self.layer2(out)
    out = self.layer3(out)
    return out
1 Like

It doesn’t matter. forward is which defines the real order. The only difference is they will be displayed differently when you print the model or save it.

1 Like