Expected tensor for argument #1 'input' to have the same device as tensor for argument #2 'weight';

解决了题目所述问题。
报错完整信息为:
RuntimeError: Expected tensor for argument #1 ‘input’ to have the same device as tensor for argument #2 ‘weight’; but device 1 does not equal 0 (while checking arguments for cudnn_convolution)

网上查到的这个错误不多,基本就是在使用pytorch加载模型训练时出现输入的数据和模型的参数不在同一个GPU上,这也是报错信息的意思。有大佬仔细分析了原因,广播什么的我也看不懂。
看了下他们的情况好像就是在定义方法时,多了一层嵌套,然后就报错了。把多的一层嵌套调用简化掉就好了。
但我的情况是这样:
报错:
Expected tensor for argument #1 'input' to have the same device as tensor for argument #2 'weight';
Expected tensor for argument #1 'input' to have the same device as tensor for argument #2 'weight';
decoder部分用到了torch.cat(),里面的t64,t128,t256在前面定义,注释掉之后不会报错,可以肯定错误出在三个变量的调用上。

将global注释掉,成功:
Expected tensor for argument #1 'input' to have the same device as tensor for argument #2 'weight';
加global是因为原本程序将self的方法都封在了encoder和decoder方法中,全局变量才能在不同方法中使用,为了试错,全搬了出来,发现了这个问题。