My model parameters are not getting updated

Anjali_Raj: <p>I am very new to this pytorch and neural networks.I am stuck in training one model since last 1 week. My model paramters are not getting updated after each epoch. Also, ‘’‘list(model.paramteres()[-1].grad)’’’ returns ‘’‘None’’’.<br>
This is the code I wrote.</p>
<pre><code class="lang-auto"> for epoch in range(10):
    running_loss = 0.0
    print(list(net.parameters())[6].grad )
    outputs = net(inputs.float())
    a=torch.max(outputs,1).indices
    a=a.float()
    labels=labels.float()
    print(list(net.parameters())[6].grad )

    loss = criterion(a,labels.squeeze(1))
  
    loss = torch.tensor(loss, requires_grad = True)
    print(list(net.parameters())[6].grad )
    optimizer.zero_grad()
    
    loss.backward()
    
    optimizer.step()
    print(list(net.parameters())[6].grad )        

print('Finished Training')
</code></pre>
<p>also, this is the optimizer I have used,</p>
<pre><code class="lang-auto">import torch.optim as optim
criterion = nn.BCELoss()
optimizer = optim.Adam(net.parameters(), lr=0.001,weight_decay=0.0001)
from torch.autograd import Variable
</code></pre>
<p>Basically, what I did is I tried to train the model on only one batch of batch size=16 in order to see if my weights are getting updated or not.And I found that my loss, and the weights all remain same for 100 iterations.I really can’t figure out why is this happening.</p>
<p>I also have another question as well. My dataset contains grayscale images.And I need to perform a binary classification on them. This is the model I have made.It takes as an input tensor of shape (N,1,64,64) where N is the batch_size.</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()

        self.conv1 = nn.Conv2d(1, 16, 5)
        self.conv2 = nn.Conv2d(16, 32, 7)

        self.dropout1 = nn.Dropout2d(0.25)
        self.dropout2 = nn.Dropout2d(0.5)
        self.fc1 = nn.Linear(4608,128)  
        self.fc2 = nn.Linear(128,16)
        self.fc3 = nn.Linear(16, 2)


    def forward(self, x):
        # Max pooling over a (2, 2) window
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        # If the size is a square, you can specify with a single number
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        x = self.dropout1(x)
        
        
       # x = F.max_pool2d(F.relu(self.conv3(x)), 2)
        x = torch.flatten(x, 1) # flatten all dimensions except the batch dimension
        x = F.relu(self.fc1(x))
        
        x = F.relu(self.fc2(x))
        x = self.dropout2(x)
        x =self.fc3(x)
        #print(x,torch.max(x))
        return x


net = Net()
net = net.float()
print(net)
</code></pre>

gphilip: <aside class="quote no-group" data-username="Anjali_Raj" data-post="1" data-topic="130840">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/anjali_raj/48/29960_2.png" class="avatar"> Anjali_Raj:</div>
<blockquote>
<p>Basically, what I did is I tried to train the model on only one batch of batch size=16 in order to see if my weights are getting updated or not.</p>
</blockquote>
</aside>
<p>Try adding</p>
<pre><code class="lang-auto">net.train()
</code></pre>
<p>before the training loop. This sets the net to the training mode, where it can learn.</p>

Anjali_Raj: <p><a class="mention" href="/u/gphilip">@gphilip</a> This did not work .I am still facing the same issue.<br>
I also tried to use <code>.clone()</code> for the parameters,but still it didn’t work.</p>
<pre><code class="lang-auto">
for epoch in range(10):

    a=net(inputs.float())
    A=list(net.parameters())[-1].clone()
    a=a.float()
    labels=labels.float()
    print(list(net.parameters())[6].grad)
    
  #  print(outputs.shape,labels.squeeze(1).shape)
    loss = criterion(a,labels.squeeze(1))
    print(list(net.parameters())[-1])

    loss.requires_grad=True
    optimizer.zero_grad()
    
    loss.backward()
    
    optimizer.step()
    B=list(net.parameters())[-1].clone()
    print(torch.equal(B.data,A.data))
    print()
print('Finished Training')'
</code></pre>
<p>This is the output I am getting.</p>
<pre><code class="lang-auto">None
Parameter containing:
tensor([ 0.0768, -0.1130], requires_grad=True)
True

None
Parameter containing:
tensor([ 0.0768, -0.1130], requires_grad=True)
True

None
Parameter containing:
tensor([ 0.0768, -0.1130], requires_grad=True)
True

None
Parameter containing:
tensor([ 0.0768, -0.1130], requires_grad=True)
True

None
Parameter containing:
tensor([ 0.0768, -0.1130], requires_grad=True)
True

</code></pre>

gphilip: <aside class="quote no-group" data-username="Anjali_Raj" data-post="3" data-topic="130840">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/anjali_raj/48/29960_2.png" class="avatar"> Anjali_Raj:</div>
<blockquote>
<p>I also tried to use <code>.clone()</code> for the parameters,but still it didn’t work.</p>
</blockquote>
</aside>
<p>Why did you think this would make it work?</p>
<p>Why are you calling <code>.float()</code> on many of the tensors? What is this supposed to do?</p>

AlphaBetaGamma96: <p>Firstly, I’d say remove this line as the <code>loss</code> should have a gradient by default, and if it doesn’t before this line, then the gradient will be 0 for all parameters as there’s no <code>grad_fn</code> connecting your loss to your parameters.</p>
<aside class="quote no-group" data-username="Anjali_Raj" data-post="3" data-topic="130840">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/anjali_raj/48/29960_2.png" class="avatar"> Anjali_Raj:</div>
<blockquote>
<pre><code class="lang-auto">    loss.requires_grad=True
</code></pre>
</blockquote>
</aside>
<p>The next thing I’d do is check to see if all your weights/biases have <code>requires_grad</code> eqaul to <code>True</code>. You can check this via,</p>
<pre><code class="lang-auto">for name, param in net.named_parameters():
  print(name, param.requires_grad)
</code></pre>
<p>Also, as <span class="mention">@gphillip</span> mentioned,  is it necessary to keep changing the outputs to <code>float</code>? Could you perhaps try your model without using <code>.float</code> on your output <code>a</code> and your <code>labels</code>! (This could be a problem with your gradients)</p>