Can I use nn.Sequential this way?

exponential: <p>Hello guys,</p>
<p>So I have the following code. My question is is it possible to use nn.Sequential this way?</p>
<pre><code class="lang-auto">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)
        #Stack1
        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)
        
        #Stack2
        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 = (....)


   
</code></pre>
<p>Then having the forward pass this way:</p>
<pre><code class="lang-auto">def forward(self, x):
       if self.out is None:
           self.out = x
       self.stack1()
       return self.stack2
</code></pre>
<p>Is this doable? Or am I doing it wrong? Thank you very much</p>

exponential: <p>Can anyone please look through this for me  <img src="https://discuss.pytorch.org/images/emoji/apple/persevere.png?v=12" title=":persevere:" class="emoji" alt=":persevere:" loading="lazy" width="20" height="20">? Many thanks!</p>

ptrblck: <p>No, I don’t think your code would work for multiple reasons:</p>
<ul>
<li>it seems you are trying to pass an undefined tensor to a module during its initialization which is wrong:</li>
</ul>
<pre><code class="lang-python">self.d1_after = self.Block1_after (self.out)
</code></pre>
<p>since you would init the modules in the <code>__init__</code> method and use them in the <code>forward</code>.<br>
The same applies for all other blocks.</p>
<ul>
<li>
<p>in the following blocks you are trying to <code>.clone()</code> an <code>nn.Module</code> which would also not work and I guess you expect to use a tensor</p>
</li>
<li>
<p>calling <code>self.stack1()</code> without any input would break</p>
</li>
<li>
<p>using <code>return self.stack2</code> would return the module without any forward pass.</p>
</li>
</ul>
<p>Look at <a href="https://pytorch.org/tutorials/beginner/examples_nn/polynomial_module.html">this tutorial</a> (and others) to get familiar with the <code>nn.Module</code> class.<br>
In summary: initialize the modules in the <code>__init__</code> and write the forward pass in the <code>forward</code> method.</p>

exponential: <p>Thank you for looking at my code.<br>
I will go over the tutorial.</p>