Hi 浚瑋!



 111504:

what changes need to be made to the code to achieve GPU computing.


Pytorch tensors can be “moved” to the gpu so that computations
occur – greatly accelerated – on the gpu.
You can created a copy of a cpu tensor that resides on the gpu with:
my_gpu_tensor = my_cpu_tensor.cuda()

If you have a model that is derived from torch.nn.Module, you can
have it move its weights to the gpu with:
my_model = torch.nn.Linear (5, 10)   # Linear is a kind of Module
# my_model is initially on the cpu
my_model.cuda()   # move the model weights to the gpu
# my_model is now on the gpu



I need to change the data back to the cpu,


If you have a tensor on the gpu – for example the output of your
model that is running on the gpu – you can make a copy of it on
the cpu with:
my_cpu_tensor = my_gpu_tensor.cpu()



from torch.autograd import Variable



Your use of Variable suggests that the example you are working
with is quite old and is written for an old version of pytorch.
(Variable is an old wrapper for Tensor that keeps track of the
information needed for autograd.  Newer versions of pytorch – that
are better in lots of ways – have this functionality built directly into
Tensor so Variable is now deprecated.)
Make sure you are using a reasonably up-to-date version of pytorch
and base your learning on up-to-date example code.
The main pytorch web site has a number of (up-to-date) tutorials 2
that would be a good place to start.
Good luck.
K. Frank