Can I use nn.Sequential this way?

Hello guys,

So I have the following code. My question is is it possible to use nn.Sequential this way?

class CDH(nn.Module):
    def __init__(self, depth, growth_rate=12, reduction=.5, bottleneck=True, dropRate=0.0,
                 before_Trans=True, d1_before = None):
        super (CDH, self).__init__ ()

          if self.d1_before is None:
            self.d1_before = self.Block1_before (self.conv(self.out))

        self.d1_after = self.Block1_after (self.out)
        self.d2_before = self.Block2_before (self.d1_after.clone ())
        self.d2_after = self.Block2_after (self.d1_after)
        self.u1_before = self.Block3_before (self.d2_after.clone ())
        self.u1_after = self.Block3_after (self.d2_after)
        self.u2_before = self.Block4_before (self.u1_after.clone ())
        self.u2_after = self.Block4_after (self.u1_after)
        self.stack1 = nn.Sequential (self.d1_after, self.d2_before, self.d2_after,
                                     self.u1_before, self.u2_after)

        self.d21_before = self.Stack2_1_before (self.u2_after.clone ())
        self.d21_after = self.Stack2_1_after (self.u2_after)
        self.d22_before = self.Stack2_2_before (self.d21_after.clone ())
        self.d22_after = self.Stack2_2_after (self.d21_after)
        self.d23_before = self.Stack2_3_before (self.d22_after.clone ())
        self.d23_after = self.Stack2_3_after (self.d22_after)
        self.d24_before = self.Stack2_4_before (self.d23_before.clone ())
        self.d24_after = self.Stack2_4_after (self.d23_before)
        self.stack2 = nn.Sequential(self.d21_before, self.d21_after, self.d22_before, self.d22_after, self.d23_before,
                                    self.d23_after, self.d24_before, self.d24_after)

         self.conv = nn.Conv2d (...)
         self.Block1_before = (....)
        self.Block4_after = (....)


Then having the forward pass this way:

def forward(self, x):
       if self.out is None:
           self.out = x
       return self.stack2

Is this doable? Or am I doing it wrong? Thank you very much

Can anyone please look through this for me :persevere:? Many thanks!

No, I don’t think your code would work for multiple reasons:

  • it seems you are trying to pass an undefined tensor to a module during its initialization which is wrong:
self.d1_after = self.Block1_after (self.out)

since you would init the modules in the __init__ method and use them in the forward.
The same applies for all other blocks.

  • in the following blocks you are trying to .clone() an nn.Module which would also not work and I guess you expect to use a tensor

  • calling self.stack1() without any input would break

  • using return self.stack2 would return the module without any forward pass.

Look at this tutorial (and others) to get familiar with the nn.Module class.
In summary: initialize the modules in the __init__ and write the forward pass in the forward method.

Thank you for looking at my code.
I will go over the tutorial.