Python 应为后端CPU的对象,但为参数#2'获取了后端CUDA;来源';
我尝试过其他答案,但错误没有消除。与我得到的另一个问题不同的是,错误中使用的最后一个术语是“source”,我在任何问题中都没有找到它。如果可能,请解释术语“来源””的错误。在没有CPU的情况下运行代码可以正常工作 我正在使用启用GPU的Google ColabPython 应为后端CPU的对象,但为参数#2'获取了后端CUDA;来源';,python,python-3.x,pytorch,Python,Python 3.x,Pytorch,我尝试过其他答案,但错误没有消除。与我得到的另一个问题不同的是,错误中使用的最后一个术语是“source”,我在任何问题中都没有找到它。如果可能,请解释术语“来源””的错误。在没有CPU的情况下运行代码可以正常工作 我正在使用启用GPU的Google Colab 导入火炬 从火炬进口 将syft作为sy导入 挂钩=同步火炬挂钩(火炬) device=torch.device(“cuda”如果torch.cuda.is_可用()否则为“cpu”) 模型=nn.顺序(nn.线性(784256), n
导入火炬
从火炬进口
将syft作为sy导入
挂钩=同步火炬挂钩(火炬)
device=torch.device(“cuda”如果torch.cuda.is_可用()否则为“cpu”)
模型=nn.顺序(nn.线性(784256),
nn.ReLU(),
nn.线性(256128),
nn.ReLU(),
nn.线性(128,64),
nn.ReLU(),
nn.线性(64,10),
nn.LogSoftmax(尺寸=1))
模型=模型到(设备)
输出:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-42-136ec343040a> in <module>()
8 nn.LogSoftmax(dim = 1))
9
---> 10 model = model.to(device)
3 frames
/usr/local/lib/python3.6/dist-packages/syft/frameworks/torch/hook/hook.py in data(self, new_data)
368
369 with torch.no_grad():
--> 370 self.set_(new_data)
371 return self
372
RuntimeError: Expected object of backend CPU but got backend CUDA for argument #2 'source'
---------------------------------------------------------------------------
运行时错误回溯(上次最近调用)
在()
8 nn.LogSoftmax(尺寸=1))
9
--->10型号=型号至(设备)
3帧
/数据中的usr/local/lib/python3.6/dist-packages/syft/frameworks/torch/hook/hook.py(self,new_数据)
368
369带火炬。无梯度()
-->370自我设置(新数据)
371回归自我
372
RuntimeError:应为后端CPU的对象,但为参数#2“源”获取了后端CUDA
此问题与PySyft
有关。如图所示,电流将设置为:
导入火炬
torch.set\U default\U tensor\U类型(torch.cuda.FLOTTENSOR)
就在导入火炬之后
代码:
导入火炬
从火炬进口
torch.set_default_tensor_type(torch.cuda.FloatTensor)#非常感谢!现在它开始工作了。花了4个小时在我的代码中找到问题。@RavikantSingh我们都遇到过:)
cuda
Sequential(
(0): Linear(in_features=784, out_features=256, bias=True)
(1): ReLU()
(2): Linear(in_features=256, out_features=128, bias=True)
(3): ReLU()
(4): Linear(in_features=128, out_features=64, bias=True)
(5): ReLU()
(6): Linear(in_features=64, out_features=10, bias=True)
(7): LogSoftmax()
)