Data loading with padding of an average number of nodes #2995
Unanswered
shrimonmuke0202
asked this question in
Q&A
Replies: 2 comments 9 replies
-
Can you give me an example on what you are trying to do exactly? If you want to have all graphs to be of size def fix_dimension(data):
if data.num_nodes < 43:
data.x = torch.cat([data.x, torch.zeros(43 - data.num_nodes, data.x.size(-1))], dim=0)
mask = (data.edge_index[0] < 43) & (data.edge_index[1] < 43)
data.edge_index = data.edge_index[:, mask]
elif data.num_nodes > 43:
data.x = data.x[:43]
return data
dataset = MyDataset(transform=fix_dimension) |
Beta Was this translation helpful? Give feedback.
9 replies
-
Thank you this code works.
…On Fri, Aug 20, 2021 at 9:26 PM Matthias Fey ***@***.***> wrote:
Oh yeah, I should not write code without testing it :D The correct code is
import torchfrom torch_geometric.data import DataLoaderfrom torch_geometric.datasets import TUDatasetfrom torch_geometric.utils import to_dense_batch, to_dense_adj
N = 22
def fix_dimension(data):
if data.num_nodes < N:
data.x = torch.cat(
[data.x, torch.zeros(N - data.num_nodes, data.x.size(-1))], dim=0)
elif data.num_nodes > N:
data.x = data.x[:N]
mask = (data.edge_index[0] < N) & (data.edge_index[1] < N)
data.edge_index = data.edge_index[:, mask]
data.num_nodes = N
print(data.edge_index.max())
return data
dataset = TUDataset('/tmp/MUTAG', name='MUTAG', transform=fix_dimension)loader = DataLoader(dataset, batch_size=32, drop_last=True)
for data in loader:
x, _ = to_dense_batch(data.x, data.batch)
adj = to_dense_adj(data.edge_index, data.batch)
print(x.shape, adj.shape)
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#2995 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AQ7MTCRHFWTLOEECOTUGOWDT5Z3JVANCNFSM5CNU22HQ>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email>
.
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
How can I fix the padding of edge_index in the data loader for a fixed dimension?
For e.g: if my maximum number of nodes in the graph is 46 but I want all the edge indexes with an average number of nodes which is around 43. In this case, how can I fix the padding length of each batch with a fixed size 43?
Beta Was this translation helpful? Give feedback.
All reactions