Thanks for your quick reply.
I seem to get a run time error.

RuntimeError                              Traceback (most recent call last)
 in 
38         # Backward and optimize
39         optimizer.zero_grad()
—> 40         loss.backward()
41         optimizer.step()
42
~\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\tensor.py in backward(self, gradient, retain_graph, create_graph)
100                 products. Defaults to False.
101         “”"
–> 102         torch.autograd.backward(self, gradient, retain_graph, create_graph)
103
104     def register_hook(self, hook):
~\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\autograd_init_.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
88     Variable._execution_engine.run_backward(
89         tensors, grad_tensors, retain_graph, create_graph,
—> 90         allow_unreachable=True)  # allow_unreachable flag
91
92
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn