Gradient propagation is none after torch.cat?

Wai_Tik_Chan: <p>Hi all,<br>
Currently, I am working on a model with the following setup :</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/4/e46c76332f1af7a9a11a96aa990a2f6cb3543be3.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/e46c76332f1af7a9a11a96aa990a2f6cb3543be3" title="Untitled 1"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/4/e46c76332f1af7a9a11a96aa990a2f6cb3543be3_2_690x487.jpeg" alt="Untitled 1" data-base62-sha1="wAJeY1KXvRRQLewC4l8hy0M7BHt" width="690" height="487" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/4/e46c76332f1af7a9a11a96aa990a2f6cb3543be3_2_690x487.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/e/4/e46c76332f1af7a9a11a96aa990a2f6cb3543be3_2_1035x730.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/e/4/e46c76332f1af7a9a11a96aa990a2f6cb3543be3.jpeg 2x" data-dominant-color="F0F3F8"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Untitled 1</span><span class="informations">1123×794 62 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>However, it is found that there is no weight.grad for those encoder layers (before torch.cat) in vgg16 from tensorboard as following screen capture :  (Picture 1)</p>
<p>In compare to those Conv2d layers after torch.cat  (Picture 2)</p>
<p>Is the torch.cat blocking the loss.backward working in the model ?</p>
<p>Thanks ~ Dick</p>
<p>Here is some code snap for reference :</p>
<p>Model :</p>
<pre><code class="lang-auto">class Vgg16PairInput(nn.Module):
    
    def weight_init(self,m):
        classname=m.__class__.__name__
        if classname.find('ConvTran')!=-1:
            m.weight.data.normal_(0,0.1)
    
    def __init__(self):
        super(Vgg16PairInput,self).__init__()
        self.pretrained_model = models.vgg16(pretrained=True)
        self.encoder = nn.Sequential(*list(self.pretrained_model.features.children())[:-1],
                                            nn.Conv2d(512,8,kernel_size=3,stride=1,padding=1),
                                            nn.Sigmoid()
                                            )
        self.compare=nn.Sequential(
                        nn.Conv2d(16,1,kernel_size=3,stride=1,padding=1),
                        nn.Sigmoid(),
                        nn.Conv2d(1,1,kernel_size=16,stride=1,padding=0)
                        #nn.Sigmoid()
        )
                        
        del self.pretrained_model
        
    def encode(self,images):
        code=self.encoder(images)
        return code
    
    
    
    def forward(self,leftimage,rightimage):
        lcode = self.encoder(leftimage)
        #print(lcode.shape)
        rcode = self.encoder(rightimage)
        #print(rcode.shape)
        combin = torch.cat((lcode,rcode),1)
        #print(combin.shape)
        result = self.compare(combin)
        return result.to(torch.double)

</code></pre>
<p>Main for for training :</p>
<pre><code class="lang-auto">lossfunction=nn.BCEWithLogitsLoss()
optimizer = torch.optim.Adam(model.parameters(),lr=0.1)


for epoch in range(epochs):
  
    print('epoch [{}/{}]'.format(epoch+1, epochs))
    loss_train = 0
    loss_val = 0
    totaldiff = 0
    model.train()

    for i, data in enumerate(trainloader):
        tampimg,maskimg,result=data
       
        output=model(tampimg.cuda(),maskimg.cuda())
        output=output.view(output.shape[0])
     
        loss=lossfunction(output,result.cuda())
        optimizer.zero_grad()
        loss.backward()
       
        optimizer.step()
       
        loss_train += loss.data.cpu()
      
        print("Training batch : {} , loss : {:.4f}, Output: {}, Target : {}".format(i,loss.data.cpu(),str(output.sigmoid()),str(result)))
       
        torch.cuda.empty_cache()
</code></pre>

Wai_Tik_Chan: <p>sorry , guys<br>
As new user can’t post more than 1 photo in a post , I have separated the screen cap in reply post</p>
<p>Picture 1 is the screen capture in tensorboard  for encoder layers<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/0/b0bcdd675061ab6cf270e612dfa792ebb97ba799.png" data-download-href="https://discuss.pytorch.org/uploads/default/b0bcdd675061ab6cf270e612dfa792ebb97ba799" title="Screenshot from 2020-07-08 16-50-20"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/0/b0bcdd675061ab6cf270e612dfa792ebb97ba799_2_322x500.png" alt="Screenshot from 2020-07-08 16-50-20" data-base62-sha1="pduFU7CA6n2nNvT083EACxeaTnX" width="322" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/0/b0bcdd675061ab6cf270e612dfa792ebb97ba799_2_322x500.png, https://discuss.pytorch.org/uploads/default/original/3X/b/0/b0bcdd675061ab6cf270e612dfa792ebb97ba799.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/b/0/b0bcdd675061ab6cf270e612dfa792ebb97ba799.png 2x" data-dominant-color="FBF7F5"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot from 2020-07-08 16-50-20</span><span class="informations">398×617 25.4 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

Wai_Tik_Chan: <p>Picture 2 is the screen capture in tensorboard for Conv2d layers after torch.cat<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/7/671db56ffa5ddc64f0286813eeb18e52caac2478.png" data-download-href="https://discuss.pytorch.org/uploads/default/671db56ffa5ddc64f0286813eeb18e52caac2478" title="Screenshot from 2020-07-08 16-58-19"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/6/7/671db56ffa5ddc64f0286813eeb18e52caac2478_2_288x500.png" alt="Screenshot from 2020-07-08 16-58-19" data-base62-sha1="eIcPt3702VcpFJs9A0MzsoppTOU" width="288" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/6/7/671db56ffa5ddc64f0286813eeb18e52caac2478_2_288x500.png, https://discuss.pytorch.org/uploads/default/original/3X/6/7/671db56ffa5ddc64f0286813eeb18e52caac2478.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/6/7/671db56ffa5ddc64f0286813eeb18e52caac2478.png 2x" data-dominant-color="F8E6DF"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot from 2020-07-08 16-58-19</span><span class="informations">370×642 28.8 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>Could you try to check the gradients by printing them (i.e. without TensorBoard) just for the same of debugging?</p>
<p>To do so, you could iterate the parameters using this code snippet:</p>
<pre><code class="lang-python">for name, param in model.named_parameters():
    print(name, param.grad)
</code></pre>
<p>If some <code>param.grad</code> attributes are returning <code>None</code>, then the computation graph is indeed detached (and I cannot find the line of code where this is happening <img src="https://discuss.pytorch.org/images/emoji/apple/confused.png?v=9" title=":confused:" class="emoji" alt=":confused:"> ).</p>

Wai_Tik_Chan: <p>Hi ptrblck,</p>
<p>Thanks for your reply. It really helps a lot.<br>
The problems turn out to be a silly mistake in the code as I have put the tensorboard code right after model.eval() with torch.no_grad() set, so, it is showing none.<br>
When i have apply your check in the main loop , it shows gradient for the whole model.</p>
<p>Thanks for you help again~~</p>
<p>Dick</p>