Stack trace looks like :

RuntimeError                              Traceback (most recent call last)
 in 
17         h = np.append(h, loss.item())
18         optimizer.zero_grad()
—> 19         loss.backward()
20         optimizer.step()
21         mean_loss = np.mean(h)
/data/anaconda/envs/vimal_pytorch/lib/python3.7/site-packages/torch/tensor.py in backward(self, gradient, retain_graph, create_graph)
105                 products. Defaults to False.
106         “”"
–> 107         torch.autograd.backward(self, gradient, retain_graph, create_graph)
108
109     def register_hook(self, hook):
/data/anaconda/envs/vimal_pytorch/lib/python3.7/site-packages/torch/autograd/init.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
91     Variable._execution_engine.run_backward(
92         tensors, grad_tensors, retain_graph, create_graph,
—> 93         allow_unreachable=True)  # allow_unreachable flag
94
95
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn