RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0! (when checking argument for argument mat1 in method wrapper_addmm)

Pierre1: <p>I know this question has been asnwered a lot of times but for some reason all the tips I got didn’t work. Here’s the code, I am trying to use PGexplainer from pytorch geometric to explain graphs using a already trained model. Here’s the code:<br>
from torch_geometric.nn import GCNConv<br>
from torch_geometric.nn import global_mean_pool<br>
import torch.nn.functional as F<br>
import torch.nn as nn</p>
<p>class GCN(nn.Module):<br>
def <strong>init</strong>(self, input_dim, hidden_channels):<br>
super(GCN, self).<strong>init</strong>()<br>
torch.manual_seed(42)<br>
self.conv1 = GCNConv(input_dim, hidden_channels)<br>
self.conv2 = GCNConv(hidden_channels, hidden_channels)<br>
self.conv3 = GCNConv(hidden_channels, hidden_channels)<br>
self.lin = nn.Linear(hidden_channels, 3)</p>
<pre><code>def forward(self, x, edge_index, batch):
    x = self.conv1(x, edge_index)
    x = F.relu(x)
    x = self.conv2(x, edge_index)
    x = F.relu(x)
    x = self.conv3(x, edge_index)
    x = gnn.global_mean_pool(x, batch)
    x = self.lin(x)
    return x
</code></pre>
<p>model = GCN(input_dim = 3, hidden_channels = 256)<br>
model.to(device)<br>
model.load_state_dict(torch.load(‘model1.pth’, map_location=device))</p>
<p>from torch_geometric.explain import Explainer, PGExplainer<br>
model.double()<br>
explainer = Explainer(<br>
model=model,<br>
algorithm=PGExplainer(epochs=10, lr=0.003),<br>
explanation_type=‘phenomenon’,<br>
edge_mask_type=‘object’,<br>
model_config=dict(<br>
mode=‘multiclass_classification’,<br>
task_level=‘graph’,<br>
return_type=‘raw’,<br>
),<br>
# Include only the top 10 most important edges:<br>
threshold_config=dict(threshold_type=‘topk’, value=10),<br>
)</p>
<h1>
<a name="pgexplainer-needs-to-be-trained-separately-since-it-is-a-parametric-1" class="anchor" href="#pgexplainer-needs-to-be-trained-separately-since-it-is-a-parametric-1"></a>PGExplainer needs to be trained separately since it is a parametric</h1>
<h1>
<a name="explainer-ie-it-uses-a-neural-network-to-generate-explanations-2" class="anchor" href="#explainer-ie-it-uses-a-neural-network-to-generate-explanations-2"></a>explainer i.e it uses a neural network to generate explanations:</h1>
<p>for epoch in range(10):<br>
for data in val_dataloader:<br>
# Move the data to the chosen device<br>
data = data.to(device)<br>
x = data.x<br>
x = x.to(device)</p>
<pre><code>    edge_index = data.edge_index
    edge_index = edge_index.to(device)

    target = data.y
    target = target.to(device)

    batch = data.batch
    batch = batch.to(device)

    model.to(x.device)

    # Pass the data to the model's train method
    loss = explainer.algorithm.train(epoch, model, x, edge_index, target=target, batch=batch)
</code></pre>
<p>The error always occurs at the ‘loss =…’</p>

ptrblck: <p>Is the error message showing the layer which is raising the issue?<br>
If not, could you try to isolate the layer by adding debug print statements or by stepping through the code with a debugger?<br>
I don’t see an obvious issue in your code, but am also not deeply familiar with PyG.</p>

Pierre1: <p>Got the answer from the pytorch geometric GitHub, the problem was from algorithm=PGExplainer. Should have been algorithm=PGExplainer.to(device)</p>