LinkNeighborLoader error in cuda #7557
-
Hi, always thank you for your effort on maintanance of the package. While I run the example code, there was an error about the location of tensors. import torch
import numpy as np
import pandas as pd
from torch_geometric.utils import barabasi_albert_graph
from torch_geometric.loader import DataLoader, LinkNeighborLoader
from torch_geometric.transforms import RandomLinkSplit
from torch_geometric.data import Data
device = "cuda" if torch.cuda.is_available() else "cpu"
np.random.seed(seed=1234)
torch.manual_seed(1234)
torch.cuda.is_available()
# True # Example graph data generation
bagraph = barabasi_albert_graph(num_nodes=100, num_edges=80)
example_feature = torch.rand(100, 256)
bagraph = bagraph.to(device)
example_feature = example_feature.to(device)
print(bagraph.device)
print(example_feature.device)
# cuda:0
# cuda:0 example_graph = Data(edge_index=bagraph, x=example_feature)
example_graph = example_graph.to(device)
example_graph.is_cuda
# True transform = RandomLinkSplit(num_val=0.1, num_test=0.2, is_undirected=True, split_labels=True)
train_data, val_data, test_data = transform(example_graph)
print(train_data.is_cuda)
print(val_data.is_cuda)
print(test_data.is_cuda)
# True
# True
# True train_loader = LinkNeighborLoader(train_data, batch_size=16, shuffle=True, neg_sampling_ratio=1.0, num_neighbors=[5,5])
val_loader = LinkNeighborLoader(val_data, batch_size=16, shuffle=True, neg_sampling_ratio=1.0, num_neighbors=[5,5])
test_loader = LinkNeighborLoader(test_data, batch_size=16, shuffle=False, neg_sampling_ratio=1.0, num_neighbors=[5,5])
for batch in loader:
batch = batch.to(device)
print(batch)
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
This problem was solved by putting data in the loader into device instead of putting the initial data into cuda from the beginning. bagraph = barabasi_albert_graph(num_nodes=100, num_edges=80)
example_feature = torch.rand(100, 256)
example_graph = Data(edge_index=bagraph, x=example_feature)
loader = LinkNeighborLoader(example_graph, batch_size=16, shuffle=True, neg_sampling_ratio=1.0, num_neighbors=[5])
for batch in loader:
batch = batch.to(device)
print(batch)
# Data(x=[84, 256], edge_index=[2, 221], n_id=[84], e_id=[221], input_id=[16], edge_label_index=[2, 32], edge_label=[32])
# Data(x=[83, 256], edge_index=[2, 207], n_id=[83], e_id=[207], input_id=[16], edge_label_index=[2, 32], edge_label=[32])
# Data(x=[80, 256], edge_index=[2, 215], n_id=[80], e_id=[215], input_id=[16], edge_label_index=[2, 32], edge_label=[32])
# ... By the way, what is the problem in the original question? I guess that sampled negative edges from |
Beta Was this translation helpful? Give feedback.
-
Let me take a look :) |
Beta Was this translation helpful? Give feedback.
-
#7572 Should fix this. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
#7572 Should fix this.