I solve this problem. There was nothing wrong in the code, and pytorch and torchvision were up to date. I thought it may be caused by incompatibilities between packages, so I reinstall anaconda and its environment. Then it works. Thank you very much!