Thank you for your reply, I have solved this problem with your help! I have another problem, it is when I try to run the model in C++ it comes up:
Warning: RNN module weights are not part of single contiguous chunk of memory. This means they need to be compacted at every call, possibly greatly increasing memory usage. To compact weights again call flatten_parameters(). (_cudnn_impl at /pytorch/aten/src/ATen/native/cudnn/RNN.cpp:1266)
I find that in my python code it has the same warning information like this:RuntimeWarning: RNN module weights are not part of single contiguous chunk of memory. This means they need to be compacted at every call, possibly greatly increasing memory usage. To compact weights again call flatten_parameters()., but if I don’t use scripting method and just run the model,  the warning information does not appear.