Hi,
Your advice really helped me a lot!
I removed all .to(device) and tried again locally and it works!
Then I deployed it on colab, the cpu version can be executed smoothly, but the gpu version still has the same problem.
In fact, this is my first time to use pytorch with gpu, so maybe there’s something wrong with my programming (espcially for variables on gpu), but now I realized that this error may be linked with variables on cpu and gpu or something else.
And thank you for your idea in using torch.nn and torch.optim. To tell you the truth, I’ve tried it before writing this notebook and succeeded. But I want to use scratch to make sure that I really understand all the principles. Personally I believe that it’s essential  for beginners to realize how the network works. Although modern deep learning frameworks can do a lot of work almost automatically, implementing them from scratch ensures that I really know what I’m doing.