Error: Expected all tensors to be on the same device, While all are on same device
Error: Expected all tensors to be on the same device, While all are on same device
我不知道为什么我会收到这个错误,而我的所有张量都在同一个设备“cuda”上。
这是代码
def train(self):
self.model.train()
self.optimizer.zero_grad() # Clear gradients.
print("X is",self.XT.get_device())
print("edge_index_ is", self.edge_index_.get_device())
print("R_train_mask is", self.R_train_mask.get_device())
print("datas.y is", self.datas.y.get_device())
print("edge_index_ is", self.edge_index_.get_device())
out = self.model(self.XT, self.edge_index_) # Perform a single forward pass.
loss = self.criterion(out[self.R_train_mask],
self.datas.y[self.R_train_mask])
这里是错误
X is 0
edge_index_ is 0
R_train_mask is 0
datas.y is 0
edge_index_ is 0
Traceback (most recent call last):
File "/home/adnan/GNNPaperCodes/PyGCL/examples/GCAPaperAd.py", line 126, in <module>
main()
File "/home/adnan/GNNPaperCodes/PyGCL/examples/GCAPaperAd.py", line 91, in main
topfeatures=tf.TopFeaturesFind()
File "/home/adnan/GNNPaperCodes/PyGCL/examples/PatchFinder.py", line 64, in TopFeaturesFind
loss = self.train()
File "/home/adnan/GNNPaperCodes/PyGCL/examples/PatchFinder.py", line 154, in train
out = self.model(self.XT, self.edge_index_) # Perform a single forward pass.
File "/home/adnan/.conda/envs/forGit/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "/home/adnan/GNNPaperCodes/PyGCL/examples/PatchFinder.py", line 211, in forward
x = self.conv1(x, edge_index_)
File "/home/adnan/.conda/envs/forGit/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "/home/adnan/.conda/envs/forGit/lib/python3.9/site-packages/torch_geometric/nn/conv/gcn_conv.py", line 181, in forward
x = self.lin(x)
File "/home/adnan/.conda/envs/forGit/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "/home/adnan/.conda/envs/forGit/lib/python3.9/site-packages/torch_geometric/nn/dense/linear.py", line 103, in forward
return F.linear(x, self.weight, self.bias)
File "/home/adnan/.conda/envs/forGit/lib/python3.9/site-packages/torch/nn/functional.py", line 1848, in linear
return torch._C._nn.linear(input, weight, bias)
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument mat2 in method wrapper_mm)
很明显,如果我将设备更改为 'cpu'
,一切正常
您忘记将 self.model
传输到设备。实际上,模型具有必须传输到正确设备的权重,因为它们与您的输入交互。
你可以用 self.model.to("cuda")
来完成。
我不知道为什么我会收到这个错误,而我的所有张量都在同一个设备“cuda”上。 这是代码
def train(self):
self.model.train()
self.optimizer.zero_grad() # Clear gradients.
print("X is",self.XT.get_device())
print("edge_index_ is", self.edge_index_.get_device())
print("R_train_mask is", self.R_train_mask.get_device())
print("datas.y is", self.datas.y.get_device())
print("edge_index_ is", self.edge_index_.get_device())
out = self.model(self.XT, self.edge_index_) # Perform a single forward pass.
loss = self.criterion(out[self.R_train_mask],
self.datas.y[self.R_train_mask])
这里是错误
X is 0
edge_index_ is 0
R_train_mask is 0
datas.y is 0
edge_index_ is 0
Traceback (most recent call last):
File "/home/adnan/GNNPaperCodes/PyGCL/examples/GCAPaperAd.py", line 126, in <module>
main()
File "/home/adnan/GNNPaperCodes/PyGCL/examples/GCAPaperAd.py", line 91, in main
topfeatures=tf.TopFeaturesFind()
File "/home/adnan/GNNPaperCodes/PyGCL/examples/PatchFinder.py", line 64, in TopFeaturesFind
loss = self.train()
File "/home/adnan/GNNPaperCodes/PyGCL/examples/PatchFinder.py", line 154, in train
out = self.model(self.XT, self.edge_index_) # Perform a single forward pass.
File "/home/adnan/.conda/envs/forGit/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "/home/adnan/GNNPaperCodes/PyGCL/examples/PatchFinder.py", line 211, in forward
x = self.conv1(x, edge_index_)
File "/home/adnan/.conda/envs/forGit/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "/home/adnan/.conda/envs/forGit/lib/python3.9/site-packages/torch_geometric/nn/conv/gcn_conv.py", line 181, in forward
x = self.lin(x)
File "/home/adnan/.conda/envs/forGit/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "/home/adnan/.conda/envs/forGit/lib/python3.9/site-packages/torch_geometric/nn/dense/linear.py", line 103, in forward
return F.linear(x, self.weight, self.bias)
File "/home/adnan/.conda/envs/forGit/lib/python3.9/site-packages/torch/nn/functional.py", line 1848, in linear
return torch._C._nn.linear(input, weight, bias)
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument mat2 in method wrapper_mm)
很明显,如果我将设备更改为 'cpu'
,一切正常您忘记将 self.model
传输到设备。实际上,模型具有必须传输到正确设备的权重,因为它们与您的输入交互。
你可以用 self.model.to("cuda")
来完成。