Yeah, I have used MSELoss() function in place of NLLLoss. However, there is still a problem with loss.backward(). It throws this error.

RuntimeError                              Traceback (most recent call last)
 in 
38         # Backward and optimize
39         optimizer.zero_grad()
—> 40         loss.backward()
41         optimizer.step()
42
~/yes/lib/python3.7/site-packages/torch/tensor.py in backward(self, gradient, retain_graph, create_graph)
193                 products. Defaults to False.
194         “”"
–> 195         torch.autograd.backward(self, gradient, retain_graph, create_graph)
196
197     def register_hook(self, hook):
~/yes/lib/python3.7/site-packages/torch/autograd/init.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
97     Variable._execution_engine.run_backward(
98         tensors, grad_tensors, retain_graph, create_graph,
—> 99         allow_unreachable=True)  # allow_unreachable flag
100
101
RuntimeError: expected dtype Float but got dtype Long