2021-06-15

基于 PyTorch 的 Seq2Seq 的中英文神经机翻译 问题汇总

  • 基于 PyTorch 的 Seq2Seq 的中英文神经机翻译 问题汇总
    • 问题一:UserWarning: To exit: use ‘exit‘, ‘quit‘, or Ctrl-D. warn(“To exit: use ‘exit‘, ‘quit‘, or Ctrl-D.
    • 问题二:nltk下载问题:
    • 问题三:‘lengths‘ argument should be a 1D CPU int64 tensor, but got 1D cuda:0 Long tensor
    • 问题四:Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

基于 PyTorch 的 Seq2Seq 的中英文神经机翻译 问题汇总

参考链接:基于 PyTorch 的 Seq2Seq 的中英文神经机翻译

问题一:UserWarning: To exit: use ‘exit‘, ‘quit‘, or Ctrl-D. warn(“To exit: use ‘exit‘, ‘quit‘, or Ctrl-D.

报错

按照所提示的运行%tb得到:
2021-06-15_第1张图片
问题是出在Ipython上,博客上说用命令行和pycharm没问题,在jupyter中会报错,解决的办法是:
在这里插入图片描述
(注释的为原句),在()中传入参数即可

问题二:nltk下载问题:

运行代码是出现:
2021-06-15_第2张图片
要用nltk下载punkt,运行该命令显示失败,本地运行的方法是离线下载,装到本地,华为云上需要更新pip版本,然后多运行几次

问题三:‘lengths‘ argument should be a 1D CPU int64 tensor, but got 1D cuda:0 Long tensor

这是说这个参数要用CPU训练,解决这个问题的方法决定了你之后遇到的问题和你训练的形式(CPU还是GPU)
选择CPU
调整torch的版本,华为云默认的版本是1.8.1,降低torch版本到1.6.0这个问题就不会出现,但1.6.0中对于除号“/"要用torch.floor_divide否则报错:
在这里插入图片描述
降低版本到1.5.0可解决以上全部问题

选择GPU
2021-06-15_第3张图片

2021-06-15_第4张图片
在对应的函数中添加横线部分即可。

问题四:Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

即在训练是参数有在CPU上的也有在GPU上的,这里的解决办法取决于上一步,如果采用的是CPU的解决办法,这里:
2021-06-15_第5张图片
将横线部分设为只用CPU即可

若采用的是GPU的解决办法:
2021-06-15_第6张图片
在上图的函数中按照横线的部分添加即可

你可能感兴趣的:(自然语言处理,pytorch)