title: Pytorch showing the error: 'NoneType' object has no attribute 'zero_'
body: <p>I am using Python 3.8 and VSCode.</p>
  <p>I tried to create a basic Neural Network without activations and biases but because of the error, I'm not able to update the gradients of the weights.</p>
  <p>Matrix Details:</p>
  <p>Layer Shape: (1, No. of Neurons)</p>
  <p>Weight Layer Shape: (No. of Neurons in the previous layer, No. of Neurons in the next layer)</p>
  <p>Here's my code:</p>
  <pre class="lang-py prettyprint-override"><code># sample neural network
  
  # importing libraries
  import torch
  import numpy as np
  torch.manual_seed(0)
  
  # hyperparameters
  epochs = 100
  lr = 0.01 # learning rate
  
  # data
  X_train = torch.tensor([[1]], dtype = torch.float)
  y_train = torch.tensor([[2]], dtype = torch.float)
  
  '''
  Network Architecture:
      1 neuron in the first layer
      4 neurons in the second layer
      4 neurons in the third layer
      1 neuron in the last layer
  
      * I haven't added bias and activation.
  '''
  
  # initializing the weights
  weights = []
  weights.append(torch.rand((1, 4), requires_grad = True))
  weights.append(torch.rand((4, 4), requires_grad = True))
  weights.append(torch.rand((4, 1), requires_grad = True))
  
  # calculating y_pred
  y_pred = torch.matmul(torch.matmul(torch.matmul(X_train, weights[0]), weights[1]), weights[2])
  
  # calculating loss
  loss = (y_pred - y_train)**2
  
  # calculating the partial derivatives
  loss.backward()
  
  # updating the weights and zeroing the gradients
  with torch.no_grad():
      for i in range(len(weights)):
          weights[i] = weights[i] - weights[i].grad
          weights[i].grad.zero_()
  </code></pre>
  <p>It it showing the error:</p>
  <pre><code>File &quot;test3.py&quot;, line 43, in &lt;module&gt;
  weights[i].grad.zero_()
  AttributeError: 'NoneType' object has no attribute 'zero_'
  </code></pre>
  <p>I don't understand why it is showing this error. Can someone please explain?</p>