Thanks for your reply! One more thing I did to make the code running is to add
requires_grad=True
in the Variable().
Now I got zero  compilation error and the code could be executed.
However, I found that the parameters do not change after I did the
optimizer.zero_gradient()
loss.backward() 
optimizer.step() 

process. This means the gradient the code calculated is zero. Do you think this is to do with how I got the return value y which somehow makes the optimizer treat it as a constant?