基于 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得到:

问题是出在Ipython上,博客上说用命令行和pycharm没问题,在jupyter中会报错,解决的办法是:

(注释的为原句),在()中传入参数即可
问题二:nltk下载问题:
运行代码是出现:

要用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


在对应的函数中添加横线部分即可。
问题四: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的解决办法,这里:

将横线部分设为只用CPU即可
若采用的是GPU的解决办法:

在上图的函数中按照横线的部分添加即可
本文汇总了使用PyTorch实现Seq2Seq模型进行中英文神经机器翻译时遇到的问题及解决方法。问题涉及UserWarning、nltk下载、设备不兼容错误等,解决方案包括更新或降低torch版本、更改设备设置、处理输入数据类型等。
1910

被折叠的 条评论
为什么被折叠?



