字面问题应该是计算的张量不在同一个设备,一个在cpu上另一个在cuda上。查了一些解决方案要么是错误信息不一致,也有说什么版本不支持的,可我觉得这应该是个很容易解决的问题)

了解了一下使用cuda的方法和问题,结合自己的问题报错信息:

应该重点关注问题的定位信息,发现应该是最后这个函数complex_dropout2d()的计算过程出现了问题,然后打开这个函数的定义文件:


def complex_dropout2d(input, p=0.5, training=True):
    # need to have the same dropout mask for real and imaginary part, 
    # this not a clean solution!
    mask = torch.ones(*input.shape, dtype = torch.float32)
    mask = dropout2d(mask, p, training)*1/(1-p)  
    mask.type(input.dtype)
    return mask*input

既然报错信息说是这个计算的张量不在同一个设备,input应该是在模型定义的时候就随着模型进了cuda的,所以我猜测是mask这个tensor不在cuda里而在cpu上,所以加了一句:
    mask=mask.to(device=torch.device('cuda' if torch.cuda.is_available() else 'cpu'))

使mask的数据加载到cuda中。

def complex_dropout2d(input, p=0.5, training=True):
    # need to have the same dropout mask for real and imaginary part, 
    # this not a clean solution!
    mask = torch.ones(*input.shape, dtype = torch.float32)
    mask = dropout2d(mask, p, training)*1/(1-p)  
    mask=mask.to(device=torch.device('cuda' if torch.cuda.is_available() else 'cpu'))
    mask.type(input.dtype)
    return mask*input

保存。重新运行main.py:

问题解决!

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐