I have edited my code as per your suggestion. But getting the following error -
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-7-60a90c59aa74> in <module>()
     30         loss = (output - y_batch_onehot).pow(2).mul(0.5).sum()
     31         total_loss += loss.data[0]
---> 32         loss.backward()
     33 #         pdb.set_trace()
     34 

/users/avijit.d/.local/lib/python3.6/site-packages/torch/autograd/variable.py in backward(self, gradient, retain_variables)
    144                     'or with gradient w.r.t. the variable')
    145             gradient = self.data.new().resize_as_(self.data).fill_(1)
--> 146         self._execution_engine.run_backward((self,), (gradient,), retain_variables)
    147 
    148     def register_hook(self, hook):

/users/avijit.d/.local/lib/python3.6/site-packages/torch/autograd/_functions/pointwise.py in backward(self, grad_output)
     16 
     17     def backward(self, grad_output):
---> 18         return self.saved_tensors[0] * grad_output
     19 
     20 

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation