文章目录
RuntimeError: scatter(): Expected dtype int64 for index
scatter要求数据是int64类型,在定义tensor时写的是torch.Tensor(x),应该写成torch.LongTensor(x),指定为int64类型。其他数据类型的定义方法可参考:
解决方案:
创建的过程中变量直接设置为LongTensor()或者在创建后用下列方法转换成LongTensor类型
data = data.long() #得到的是longtensor值
AttributeError: module ‘torchtext.data’ has no attribute
'Field’官方在0.9.0版本中将Field 等函数放进了legacy中,在最新版的0.12.0中移除了这个文件夹。
具体网站-> https://github.com/pytorch/text/releases
解决方法
如果你的torchtext版本在(0.9.0-0.12.0)之间可以用这个方法直接解决
from torchtext.legacy import data
或者考虑将torchtext适当降级
pip install torchtext==(适配的版本)
torch和torchtext版本对应关系:
当然也可以考虑使用新的方法
在新版本中的操作可以看官方给出的示例:https://github.com/pytorch/text
相对路径出错
如果觉得是路径问题,可以先使用os进行路径检查,如果返回True
,证明路径存在;我这边是返回False
import os
os.path.exists(test_file.txt)
解决方法:
使用os.chdir(sys.path[0])把当前py文件所在路径设置为当前运行路径.
import os
import sys
print(f"vscode current run path is {os.getcwd()}")
os.chdir(sys.path[0])
print(f"set py path as current path ")
print(f"vscode current run path is {os.getcwd()}")